The Decorator Pattern also known as: Wrapper in German: - - PowerPoint PPT Presentation

the decorator pattern
SMART_READER_LITE
LIVE PREVIEW

The Decorator Pattern also known as: Wrapper in German: - - PowerPoint PPT Presentation

The Decorator Pattern also known as: Wrapper in German: Dekorierer The Problem A bar serves several drinks: Coffee Espresso Every drink can be modified using: Whip Milk So there are a lot of


slide-1
SLIDE 1

The Decorator Pattern

also known as: Wrapper in German: Dekorierer

slide-2
SLIDE 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, …

slide-3
SLIDE 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"; } }

slide-4
SLIDE 4

Subclass everything

slide-5
SLIDE 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“; } }

slide-6
SLIDE 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“; } }

slide-7
SLIDE 7

The Decorator Pattern

slide-8
SLIDE 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; } }

slide-9
SLIDE 9

The Decorator Pattern

Drink drink = new MilkDecorator(new WhipDecorator(new Coffee())); System.out.println( drink.cost() ); // Output: 1.6

slide-10
SLIDE 10

The Decorator Pattern

public class Bar { public static void main(String[] args) { Drink basicDrinks[] = {new Espresso(), new Coffee()}; for (Drink basicDrink:basicDrinks) { Drink drink1 = basicDrink; System.out.println(drink1.getName() + " costs " + drink1.cost()); Drink drink2 = new MilkDecorator(basicDrink); System.out.println(drink2.getName() + " costs " + drink2.cost()); Drink drink3 = new WhipDecorator(basicDrink); System.out.println(drink3.getName() + " costs " + drink3.cost()); Drink drink4 = new WhipDecorator(new MilkDecorator(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

slide-11
SLIDE 11

The Decorator Pattern

slide-12
SLIDE 12

GUI Decorators

slide-13
SLIDE 13

GUI Decorators

slide-14
SLIDE 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.

slide-15
SLIDE 15

Java Streams

  • !
slide-16
SLIDE 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);

slide-17
SLIDE 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
  • bject; a strategy lets you change the guts. These

are two alternative ways of changing an object.

slide-18
SLIDE 18

Questions