Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best solution for the situation I found is to use a Double-Dispatch/Visitor pattern. I describe a situation where base class A is abstract, and concrete classes B and C inherit from A. Also, by making the DoBehavior method in the base class A abstract, we are forcing ourselves to make an implementation for it wherever we would need it, so if we expand this to add more types, we won't forget to add it's DoBehavior methods (seems unlikely that one would forget, but this behavior may be insignificant to the rest of the new type you add, and may be overlooked - especially if there are many of these behavior patterns)</p> <pre><code>interface IVisitor { void DoBehavior(B item); void DoBehavior(C item); } abstract class A { abstract void DoBehavior(IVisitor visitor); } class B : A { override void DoBehavior(IVisitor visitor) { //can do some internal behavior here visitor.DoBehavior(this); //external processing } } class C : A { override void DoBehavior(IVisitor visitor) { //can do some internal behavior here visitor.DoBehavior(this); //external processing } } class Manager: IVisitor //(or executor or whatever. The external processing class) { public static void ProcessAll(List&lt;A&gt; items) { foreach(A item in items) { item.DoBehavior(this); } } void DoBehavior(B item) { } void DoBehavior(C item); { } } </code></pre> <p>Thanks for contributing, everyone. Learnt a lot and got some good ideas from you all (it's worth it to read all the answers if you face a similar situation).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload