Joe’s Problem

In the book Joe is the programmer of the SimUDuck application.  His problem starts when his boss asks him to make the ducks actually fly in the application.    The initial designers of the system used standard OO techniques and created one Duck superclass from which all other duck types inherit.  Joe makes his mistake when he implements a fly() method into the duck superclass.  The problem is that after he added the fly() method he didn’t take into account that not all of the ducks were supposed to fly like the rubber ducks in the simulation.  To fix the flying rubber ducks problem Joe decides that he will just override the fly() method in the RubberDuck class.  Again Joe runs into trouble because he realizes that he is going to have override many more methods in the future for other classes of ducks that will be added to the application like wooden decoy ducks.  Although overriding the methods is valid use of shadowing he realized that it would become a very tedious way of handling the problem as more and more functionality is added to the program.  This is where the design pattern comes into play.  What they do is encapsulate the parts that vary in the program so that later they can alter the parts that vary without affectiong those that don’t.  By using the design pattern implemented in the book the expected pay off is to save themselves the hassel of tediously overriding methods.  The basic idea of the design pattern is they pull out the things that vary like the flying behaviors and quacking behaviors and encapsulate them into a new class called duck behaviors. The original duck class is still the superclass but now the duck behaviors live in a separate class that implements a particular behavior interface. The advantage with the new design pattern is that other types of objects can reuse the fly and quack behaviors because they are no longer hidden away in the Duck superclass.  Now they can add new behaviors without modifying any of the existing behavior classes i.e. continuously overriding uneeded methods.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: