KEMBAR78
Adapter Design Pattern | PPTX
ADAPTER DESIGN PATTERN
MHD SALEM KABBANI
HOW ADAPTER WORKS
UK power plugEU socket
Adapter
PROBLEM
• Client should works with specific interface (Interface A), but
the logic is under another interface (Interface B) for some
reason (e.g. external library).
Interface A Syste
m
Interface B
Syste
m
ADAPTER PATTERN
• Adapter pattern is one of the structural design patterns.
• It works as a bridge between two incompatible interfaces.
• This pattern involves a single class which is responsible to join
functionalities of independent or incompatible interfaces.
• Adapter doesn’t add new behavior to our interface.
SOLUTION
• We create adapter class that implements our Interface A and
has a reference to Interface B as a member.
• In the implemented methods from Interface A, we call Interface
B methods from its reference.
Interface B
Adapter
(implements
Interface A)
Syste
m
UML
IMPLEMENTATION
IMPLEMENTATION
EnemySoldier Does a
Damage
EnemySoldier moves
-----------------
EnemyWizard casts a
spell
EnemyWizard teleports
-----------------
EnemyWizard casts a
PROS. VS CONS.
• The overall complexity of the
code increases because you
need to add a set of new
interfaces and classes.
• Single Responsibility
Principle: separate data
conversion code from the
primary business logic of the
program.
• Open/Closed Principle: add
new types of adapters into
the program without
modifying the existing client
code.

Adapter Design Pattern

  • 1.
  • 2.
    HOW ADAPTER WORKS UKpower plugEU socket Adapter
  • 3.
    PROBLEM • Client shouldworks with specific interface (Interface A), but the logic is under another interface (Interface B) for some reason (e.g. external library). Interface A Syste m Interface B Syste m
  • 4.
    ADAPTER PATTERN • Adapterpattern is one of the structural design patterns. • It works as a bridge between two incompatible interfaces. • This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces. • Adapter doesn’t add new behavior to our interface.
  • 5.
    SOLUTION • We createadapter class that implements our Interface A and has a reference to Interface B as a member. • In the implemented methods from Interface A, we call Interface B methods from its reference. Interface B Adapter (implements Interface A) Syste m
  • 6.
  • 7.
  • 8.
    IMPLEMENTATION EnemySoldier Does a Damage EnemySoldiermoves ----------------- EnemyWizard casts a spell EnemyWizard teleports ----------------- EnemyWizard casts a
  • 9.
    PROS. VS CONS. •The overall complexity of the code increases because you need to add a set of new interfaces and classes. • Single Responsibility Principle: separate data conversion code from the primary business logic of the program. • Open/Closed Principle: add new types of adapters into the program without modifying the existing client code.

Editor's Notes

  • #10 The Single Responsibility Principle — Classes should have a single responsibility and thus only a single reason to change. Open/Closed Principle  — Classes and other entities should be open for extension but closed for modification. ------------ because they work with the adapters through the client interface