msfanboy
msfanboy

Reputation: 5291

Design pattern for communication from parent to its derived classes

I extra said derived classes and NOT children classes.

I have a base class and many derived classes. One derived class should call a method on the base class which again is calling a method on each derived class.

How is that possible? What design pattern should I go for implemention a communication

"channel" between my Controllers driving the UI using MVVM design pattern? I know of mediator

pattern but do not like it much as it blurs the borders of an architecture.

Upvotes: 2

Views: 1352

Answers (4)

Dhruv
Dhruv

Reputation: 61

You can use Events to communicate between the viewmodels. OR

MVVM Mediator Pattern

Upvotes: -2

mikemanne
mikemanne

Reputation: 3575

For "delegating" responsibility from a superclass to its subclass(es), you might be looking for the Template Method Pattern.

It's basically formalizing the concept of using abstract functions (which subclasses must implement, of course) to allow a base class to call a function on its concrete subclass.

Upvotes: 1

veda_ram
veda_ram

Reputation: 1

I think Command Pattern suits best for your problem. If you scenario is this:

  • You have a base class which executes a method(a command) in all derived classes(clients)
  • One of the derived class invokes that execution (an invoker)

then the best pattern is Command Pattern.

This article can give you some idea

But my suggestion is to use interfaces instead of base-derived classes.

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61599

It's not really a design pattern, but really an understanding of virtual method calls. Let's say I have something:

public abstract class Foo
{
    protected void DoFoo()
    {
        DoFooInternal();
    }

    protected abstract void DoFooInternal();
}

And I have a derived class:

public class Bar : Foo
{
    protected override void DoFooInternal()
    {
        // Something here
    }
}

In the above example, any call to DoFoo on the base class makes a virtual call to the DoFooInternal method of the derived class. I could also define my DoFooInternal as virtual, if I wanted to provide a baseline implementation:

protected virtual void DoFooInternal()
{
   // Baseline implementation here
}

In the case of your controllers, this would be the same, you can specify some common shared logic in a ControllerBase instance and derive a child controller, e.g. PeopleController which can despatch method calls to the base class, which can in turn despatch calls back to virtual methods in the derived class...

Upvotes: 2

Related Questions