Skip to main content
added 49 characters in body
Source Link
WDUK
  • 2.1k
  • 3
  • 16
  • 24

So a simple example i have:

public interface IFollow{ Transform Target {get;} void LateUpdate(); } public SomeClassA : Monobehaviour , IFollow { public Transform Target {get; set;} public void LateUpdate(){ //follow the target   logic } } public B : Monobehaviour , IFollow {  public Transform Target {get; set;} public void LateUpdate(){ //follow the target [duplicate code as seen in A] } 

When you have lots of different objects that follow a target with the same logic the implemented interface forces you to write a lot of duplicate code.

It also doesn't make much sense to do it via a base class since not all objects will follow.

What's a cleaner way to do this without so much duplication ?

Encase any one wondered, Monobehaviour is a Unity3D thing, though not overly relevant to the question but thought i'd mention it.

So a simple example i have:

public interface IFollow{ Transform Target {get;} void LateUpdate(); } public SomeClass : Monobehaviour , IFollow { public Transform Target {get; set;} public void LateUpdate(){ //follow the target logic } } 

When you have lots of different objects that follow a target with the same logic the implemented interface forces you to write a lot of duplicate code.

It also doesn't make much sense to do it via a base class since not all objects will follow.

What's a cleaner way to do this without so much duplication ?

Encase any one wondered, Monobehaviour is a Unity3D thing, though not overly relevant to the question but thought i'd mention it.

So a simple example i have:

public interface IFollow{ Transform Target {get;} void LateUpdate(); } public A : Monobehaviour , IFollow { public Transform Target {get; set;} public void LateUpdate(){ //follow the target    } } public B : Monobehaviour , IFollow {  public Transform Target {get; set;} public void LateUpdate(){ //follow the target [duplicate code as seen in A] } 

When you have lots of different objects that follow a target with the same logic the implemented interface forces you to write a lot of duplicate code.

It also doesn't make much sense to do it via a base class since not all objects will follow.

What's a cleaner way to do this without so much duplication ?

Encase any one wondered, Monobehaviour is a Unity3D thing, though not overly relevant to the question but thought i'd mention it.

Source Link
WDUK
  • 2.1k
  • 3
  • 16
  • 24

C# How to avoid duplicate code with interfaces where objects have the same behaviour

So a simple example i have:

public interface IFollow{ Transform Target {get;} void LateUpdate(); } public SomeClass : Monobehaviour , IFollow { public Transform Target {get; set;} public void LateUpdate(){ //follow the target logic } } 

When you have lots of different objects that follow a target with the same logic the implemented interface forces you to write a lot of duplicate code.

It also doesn't make much sense to do it via a base class since not all objects will follow.

What's a cleaner way to do this without so much duplication ?

Encase any one wondered, Monobehaviour is a Unity3D thing, though not overly relevant to the question but thought i'd mention it.