Adapter Design Patterns

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Design Patterns in Java

Adapter Pattern
August 2nd, 2018
What is Design Patterns
In software engineering, a design pattern is a general reusable solution to a
commonly occurring problem within a given context in software design. A design
pattern is not a finished design that can be transformed directly
into source or machine code. It is a description or template for how to solve a
problem that can be used in many different situations. Patterns are formalized
best practices that the programmer must implement themselves in the
application. Object-oriented design patterns typically show relationships
and interactions between classes or objects, without specifying the final
application classes or objects that are involved. Patterns that imply object-
orientation or more generally mutable state, are not as applicable in functional
programming languages.
Design patterns reside in the domain of modules and interconnections. At a
higher level there are architectural patterns that are larger in scope, usually
describing an overall pattern followed by an entire system

By: http://en.wikipedia.org/wiki/Software_design_pattern
What type of Design Patterns
There are many types of design patterns, for instance
•Algorithm strategy patterns addressing concerns related to high-level
strategies describing how to exploit application characteristics on a computing
platform.
•Computational design patterns addressing concerns related to key
computation identification.
•Execution patterns that address concerns related to supporting application
execution, including strategies in executing streams of tasks and building
blocks to support task synchronization.
•Implementation strategy patterns addressing concerns related to
implementing source code to support
• program organization, and
• the common data structures specific to parallel programming.
•Structural design patterns addressing concerns related to high-level
structures of applications being developed.

By: http://en.wikipedia.org/wiki/Software_design_pattern
Adapters in real life

Page 236 – Head First Design Patterns


Object-Oriented Adapters

Page 237 Head First Design Patterns


Turkey that wants to be a duck
example
public interface Duck {
public void quack();
public void fly();
}
Subclass of a duck – Mallard
Duck
public class MallardDuck implements Duck {
public void quack() {
System.out.println("Quack");
}
public void fly() {
System.out.println("I'm flying");
}
}
Turkey Interface
public interface Turkey {
public void gobble();
public void fly();
}
An instance of a turkey
public class WildTurkey implements Turkey {
public void gobble() {
System.out.println("Gobble gobble");
}
public void fly() {
System.out.println("I'm flying a short distance");
}
}
Turkey adapter – that makes a
turkey look like a duck
public class TurkeyAdapter implements Duck {
Turkey turkey;
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
public void quack() {
turkey.gobble();
}
public void fly() {
for(int i=0; i < 5; i++) {
turkey.fly();
}
}
}
Duck test drive
public class DuckTestDrive {
public static void main(String[] args) {
MallardDuck duck = new MallardDuck();
WildTurkey turkey = new WildTurkey();
Duck turkeyAdapter = new TurkeyAdapter(turkey);
System.out.println("The Turkey says...");
turkey.gobble();
turkey.fly();
System.out.println("\nThe Duck says...");
testDuck(duck);
System.out.println("\nThe TurkeyAdapter says...");
testDuck(turkeyAdapter);
}
static void testDuck(Duck duck) {
duck.quack();
duck.fly();
}
}
Test run – turkey that behaves
like a duck
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
I'm flying a short distance
I'm flying a short distance
Adapter Pattern explained

Page 241 – Head First Design Patterns


Adapter Pattern defined

The Adapter Pattern converts the interface of a


class into another interface the clients expect.
Adapter lets classes work together that couldn’t
otherwise because of incompatible interfaces.
Adapter Pattern

Page 243 – Head First Design Patterns


Summary so far..

You might also like