What is Design Pattern

Tram Ho

Start with the simple Duck simulation application

Nam works for a game simulation company called SimUDuck. Game shows many different states of ducks about swimming behavior and vocalization.

The original design used object-oriented (OO) by creating a Duck class as the parent class for the inherited subclasses. alt text

At the end of the year, due to severe pressure on competitors. After a week of careful consideration, the director decided to make a new breakthrough.

The ducks need to be able to fly

Flying ducks are the key to a breakthrough to help beat the “duckling” competitors. And of course, Nam’s manager OK this problem, and said it would be done in a week.

And this is Nam’s job: I just need to add fly () and Duck (parent class) methods to all inherited subclasses now! alt text

But something went wrong

Director of the phone for Nam: “You’re kidding me, I’m running your demo: How can rubber ducks fly!”

Nam forgot one thing that rubber ducks can’t fly. And he realized that inheritance didn’t do much to reuse or maintain code.

Along review about inheritance

Because rubber ducks have different noises from ordinary ducks, and rubber ducks cannot fly. We have to override the quack () function (the cry) and the fly () (fly) function of the rubber duck. alt text But what about wooden ducks (WoodenDuck), why can’t they call or fly?

What is the interface like?

Nam realized that inheritance would cause his code to duplicate (duplicate code). And not suitable for changing code regularly every 3-6 months.

Where the fly () and quack () (calls) functions will be affected by the new request.

Nam decided to smash his code by taking the fly () and quack () functions out of the Duck class and create the Flyable interface, the fly () and the Quackable and Quack () functions.

Only ducks can implement the Flyable interface, and ducks that can make a noise will implement the Quackable interface.

alt text

But resolved in this way the results are even worse.

The legacy way: you only need to override a few functions.

Interface way: you have to override all the ducks that can fly and make a sound, now we have 70 ducks like that …

What would you do if you were a male?

We know that not all subclasses will inherit the fly () and quack () functions, so inheritance is not the correct answer.

As for the interface: There will be many classes that implement the fly, quack () function of the Flyable and Quackable interfaces. This will limit code reuse.

And that’s why we need the Design Pattern.

Design principle

Identify the aspects of your application that vary and separate them from what stays the same.

Provisions are: Find out the changes in the application and package them, so as not to affect the general part of the system.

Design behavior for ducks

Nam realized that the fly () and quack () functions change frequently so they will be packed separately.

alt text

alt text

With this design, objects can reuse the fly and quack functions.

We can add new behaviors without affecting existing objects.

Integrated behavior for Duck class

Instead of calling fly () and quack () in Duck class. Nam will declare FlyBehavior and QuackBehavior as follows:

alt text

Nam adds two attributes to the Duck class, flyBehavior and quackBehavior. Declared as interface type (not a specific implementation class).

With flyBehavior and quackBehavior we can change how we fly or how we make a sound during runtime.

Duck implementation function

With this executable function. We do not care what kind of execution it is. What we care about is whether they function properly or not.

Function implemented for subclasses

Because flyBehavior and quackBehavior are interfaces, MallardDuck’s constuctor must initialize the executable object for flyBehavior and quackBehavior . And MallarldDuck is a real duck, so it can fly, and make a real cry.

General code

Results displayed

I am the real duck!

Fly by wings

Change behavior at runtime

We add the following two functions to the Duck class

Create a new layer of duck

Create 1 more new behavior for flying

I am a model duck!

I can not fly

I fly like a rocket!

As you saw after calling the setFlyBehavior function, the modelDuck’s flight will change.

The overall picture of Strategy Design Pattern

alt text

Contribute

Please give me a minute to help me. Please leave your comments to help the latter to read and understand better.

Thank you for your interest in this article. I wish you a good day! 😃

References from: Head First Design Pattern (Eric Freeman & Elisabeth Freeman)

Blessed

Related posts

Learn Facade Design Pattern through stories

Share the news now

Source : Viblo