Skip to content

Latest commit

 

History

History

adapter

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Adapter Pattern

This is the seventh chapter of "Head First Design Patterns" book.

The problem

Accidentally a Turkey's egg falls among that of Ducks and it tries hard to "adapt" with its duck siblings. How should it adapt?

Simplifying the huge list of behavourial changes the Turkey needs to undergo to two.

  1. Mallard Duck:

    • It can fly.
    • It makes a quack sound.
  2. Wild Turkey:

    • It can fly but for shorter distances
    • It makes a gobble sound.

Thought process

  • We need to create a new implementation such that it emulates the behaviour of a Duck but uses underlying behaviour of the Turkey itself.

UML Class Diagram

Class Diagram You can think the Client as the mother (matriarch) which tries to teach the ways of duck (Target interface) to the turkey Adapter on top of its true self Adaptee.

Takeaways

The book defines,

The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets class work together that couldn't otherwise because of incompatible interfaces.

Language specifics

  • Since there's no concept of multiple inheritance in C#, we are using object adapters.

Output

The Turkey says...
Gobble gobble
I'm flying a short distance

The Duck says...
Quack
I'm flying

The TurkeyAdapter says...
Gobble gobble
I'm flying a short distance
I'm flying a short distance
I'm flying a short distance

Pop culture analogies

One interesting analogy can be that of ugly duckling here! Ugly duckling

Standardized test making everyone of different abilities to pass the same test. Standardized test