Tecniche di Progettazione: Design...

19
Tecniche di Progettazione: Design Patterns GoF: Adapter Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica. 1

Transcript of Tecniche di Progettazione: Design...

Page 1: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Tecniche di Progettazione:

Design Patterns

GoF: Adapter

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.1

Page 2: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Adapters in real life (anglo-centric….)

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 3: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Object-Oriented Adapters

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 4: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

public interface Duck {

public void display();

public void swim(); }

public class Duckling implements Duck {

public void display() {

System.out.println(“I’m a pretty duckling");

}

public void swim() {

System.out.println("I'm learning…");

}

}

public interface Swan{

public void show();

public void swim(); }

public class HuglyDuckling implements Swan{

public void show() {

System.out.println(“I’m large and hugly");

}

public void swim() {

System.out.println("I'm swimming!”");

}

}

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 5: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.5

Page 6: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.6

Page 7: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

SwanAdapter makes a swan look like a duck

public class SwanAdapter implements Duck {Swan swan;public SwanAdapter(Swan swan) {

this.swan = swan;}

public void display() {swan.show ();

}

public void swim() {swan.swim();

}}

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 8: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Duck test drivepublic class DuckTest {

public static void main(String[] args) {

Duck duck = new Duckling();

Swan hg= new HuglyDuckling();

Duck swanAdapter = new SwanAdapter(hg);

System.out.println("The HuglyDuckling says...");

hg.show();

hg.swim();

System.out.println("\nThe Duck says...");

testDuck(duck);

System.out.println("\nThe SwanAdapter says...");

testDuck(swanAdapter);

}

static void testDuck(Duck duck) {

duck.display();

duck.swim();

}

}Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 9: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Test run – swan that behaves like a

duck

The HuglyDuckling says...I’m large and huglyI'm swimming!”

The Duck says...I'm a pretty little ducklingI'm learning…

The SwanAdapter says...I’m large and huglyI'm swimming!”

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 10: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Adapter Pattern explained

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 11: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

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.

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 12: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Adapter pattern

� Delegation is used to bind an Adapter to an Adaptee

� Interface inheritance is use to specify the interface of the Adapter class.

� Target and Adaptee (usually called legacy system) pre-exist the Adapter.

� Target may be realized as an interface in Java.Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Page 13: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Participants

� Target: Defines the application-specific interface that clients use.

� Client: Collaborates with objects conforming to the target interface.

� Adaptee: Defines an existing interface that needs adapting.

� Adapter: Adapts the interface of the adaptee to the target interface.

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.13

Page 14: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Adapter with multiple inheritance

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.14

Page 15: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Two-way adapter

� What if we want to have an adapter that acts as a Target or an Adaptee?

� Such an adapter is called a two-way adapter.

� One way to implement two-way adapters is to use multiple inheritance, but we can't do this in Java

� But we can have our adapter class implement two different Java interfaces

� Twin pattern offeres a solution with delegation

� Btw...do we really need it ?

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.15

Page 16: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Multiple Adapters for different Adaptees

� Multiple implementations of adapters for different adaptees.

� Assume we need a turkey adapter.

� Façade if the two adapters (and adaptees) cohexist

� Startegy if Client refers to only one Adapter per time

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.16

Page 17: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Adapter Pattern and Strategy Pattern

� There are many cases when the adapter can play the role of the Strategy Pattern. If we have several modules implementing the same functionality and we wrote adapters for them, the adapters are implementing the same interface. We can simply replace the adapters objects at run time because they implements the same interface.

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.17

Page 18: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Façade

� (For assume it is possible to plug this)

� Forwards requests to many adaptees at a time

� Next lecture

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.18

Page 19: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistrale... · 2016. 10. 26. · Extend the ugly duckling example to adapt turkeys too. Using

Homework

Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.19

� Extend the ugly duckling example to adapt turkeys too.

� Using strategy

� Using Façade (see in a while)