• Keine Ergebnisse gefunden

The Decorator Pattern

N/A
N/A
Protected

Academic year: 2022

Aktie "The Decorator Pattern"

Copied!
18
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

The Decorator Pattern

also known as: Wrapper in German: Dekorierer

(2)

The Problem

A bar serves several drinks:

Coffee

Espresso

Every drink can be modified using:

Whip

Milk

So there are a lot of possible combinations:

Coffe, Coffee with Milk, Coffe with Whip, Espresso with Milk and Whip, …

(3)

Subclass everything

class Coffee extends Drink { double cost() {

return 1.0;

}

public String getName() { return "Coffee";

} }

class CoffeeWithMilk extends Drink { double cost() {

return 1.2;

}

public String getName() { return "Coffee with milk";

} }

(4)

Subclass everything

(5)

Subclass everything

class Coffee extends Drink { double cost() {

return 1.0;

}

public String getName() { return "Coffee";

} }

class CoffeeWithMilk extends Coffee { double cost() {

return super.cost() + 0.2;

}

public String getName() {

return super.getName() +

„with milk“;

} }

(6)

The first Decoration

class Coffee extends Drink { double cost() {

return 1.0;

}

public String getName() { return "Coffee";

} }

class MilkDecorator extends Drink { Drink drink;

public DrinkDecorator(Drink drink) { this.drink = drink;

}

double cost() {

return drink.cost() + 0.2;

}

public String getName() {

return drink.getName() + „ with milk“;

} }

(7)

The Decorator Pattern

Drink

Coffee DrinkDecorator

MilkDecorator WhipDecorator

(8)

The Decorator Pattern

class Coffee extends Drink {

public double cost() { return 1.0; } public String getName() {

return "Coffee";

} }

class MilkDecorator extends DrinkDecorator { public MilkDecorator(Drink drink) {

super(drink);

}

public double cost() { return drink.cost() + 0.2; } public String getName() {

return drink.getName() + „ with milk“;

} }

abstract class Drink {

public abstract String getName();

public abstract double cost();

}

abstract class DrinkDecorator extends Drink { Drink drink;

public DrinkDecorator (Drink drink) { this.drink = drink;

} }

(9)

The Decorator Pattern

Drink drink = new MilkDecorator(new WhipDecorator(new Coffee()));

System.out.println( drink.cost() ); // Output: 1.6

Client MilkDecorator WhipDecorator Coffee

cost()

cost()

cost()

1.0 1.4

1.6

(10)

The Decorator Pattern

public class Bar {

public static void main(String[] args) {

Drink basicDrinks[] = {newEspresso(), newCoffee()};

for (Drink basicDrink:basicDrinks) { Drink drink1 = basicDrink;

System.out.println(drink1.getName() + " costs " + drink1.cost());

Drink drink2 = newMilkDecorator(basicDrink);

System.out.println(drink2.getName() + " costs " + drink2.cost());

Drink drink3 = newWhipDecorator(basicDrink);

System.out.println(drink3.getName() + " costs " + drink3.cost());

Drink drink4 = newWhipDecorator(newMilkDecorator(basicDrink));

System.out.println(drink4.getName() + " costs " + drink4.cost());

} } }

Espresso costs 1.5

Espresso with milk costs 1.7 Espresso with whip costs 1.9

Espresso with milk with whip costs 2.1 Coffee costs 1.0

Coffee with milk costs 1.2 Coffee with whip costs 1.4

Coffee with milk with whip costs 1.6

(11)

The Decorator Pattern

Drink

Coffee DrinkDecorator

MilkDecorator WhipDecorator +operation()

Component

+operation()

ConcreteComponent

+operation() Decorator

+operation()

ConcreteDecoratorA

+operation()

ConcreteDecoratorB

super.operation();

// something else component.operation();

(12)

GUI Decorators

VisualComponent

TextView Decorator

ScrollDecorator BorderDecorator Component

ConcreteComponent Decorator

ConcreteDecoratorA ConcreteDecoratorB

(13)

GUI Decorators

(14)

Use Decorator

More flexibility than static inheritance.

Avoids feature-laden classes high up in the hierarchy.

A decorator and ist component aren‘t identical.

Lots of little objects.

(15)

Java Streams

InputStream

FileInputStream FilterInputStream

InflaterInputStream BufferedInputStream ObjectInputStream

(16)

Java Streams

Read data from file:

InputStream in = new FileInputStream(“file.gz”);

in.read(bytes);

Read data from file using a buffer:

InputStream in = new BufferedInputStream(

new FileInputStream(“file.gz”));

in.read(bytes);

Read gzip compressed data from file:

InputStream in = new GZIPInputStream(

new FileInputStream(“file.gz”));

in.read(bytes);

Read gzip compressed data from socket:

InputStream in = new GZIPInputStream(

socket.getInputStream());

in.read(bytes);

(17)

Related Patterns

Adapter: A decorator is different from an adapter in that a decorator only changes an object‘s

responsibilities, not ist interface; an adapter will give an object a completely new interface.

Composite: A decorator can be viewed as a

degenerate composite with only one component.

However, a decorator adds additional

responsibilities — it isn‘t intended for object aggregation.

Strategy: A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.

(18)

Questions

Referenzen

ÄHNLICHE DOKUMENTE

There are many other possible targets, such as limits to atmos- pheric greenhouse gas (GHG) concentrations, energy uptake, sea- level rise, ocean acidification, rates of

Modules do not only contain other modules, but also pieces of learning material called atoms described by exactly one term of the ontology and typically one resource type.. Given

Online Motion Planning MA-INF 1314 General rays!. Elmar Langetepe University

If Norman was just a little bit smarter, he would see that we would do much better by both charging the high price, each getting 5000 schillings each day, rather than both setting

as its contribution to a climate change deal: long-term secure access to African clean and low-carbon energy resources with investment in infrastructure that will also

• The G20 growth strategy should move away from the current MAP focus on rebalancing global growth, and embrace a plan for lifting potential growth in all countries –

Three major statements defined the Gillard government’s approach to foreign policy and national security: the Australia in the Asian century White Paper (November 2012), Strong

Close bilateral and multilateral defence cooperation with Baltic and Nordic countries, the United States of America (the USA), Poland and other Allies allows a