Take an example of Duck.

Where duck can fly and quake with different ways. Use Duck abstract class and FlyBehavior + QuakeBehaviour interface and there different implementations including with Do noting implementation.

And dynamically adding implementations into the Duck implementations.

In this we have Duck with has a flyBehavior and quakeBehavior.

public abstract class Duck {

    FlyBehavior flyBehavior;

    QuackBehavior quackBehavior;







Design Points to Remember 

  • And Remember you have to coding at interfaces not at concrete classes at all. Understand this point.
  • Now after this see if your Duck object changes needs to notify someone. Not here. But take an example of weather where if it changes we need to inform the display or subscribers. So here we will integrate  an Observer pattern.
  • Classes should be open for extension but closed for modification.
  • Remove if-else to abstract method and use implementation and configuration to decide.
  • Divide the design into Creator classes and product classes. Now we have different creator and products. Create an interface for Creator and then concrete classes out of it. similarly for products.
  • While Code  review or refactor there should be no ” new A()” where A is any class. One must use a factory for instating any class. Otherwise it has to be considered as a hard coding. Because this will take you to an OOPS. like u will create a common interface for all the related classes. For example : anywhere you have used a new A() change it to interface and make a Factory to get you instance of specific type. Hence more OOPS. Now to debate this we use many times String s =””; But when we ever we do this we must know that we are violating for valid reasons. like we know String cannot be changed.
  • Number one Rule of Good Design is that you just talk in language of interfaces and interface has to be dependent on interfaces and abstract classes only. Where ever you need some implementation use it with abstraction. By Using a abstract class.