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 possible combinations: Coffe, Coffee with Milk, Coffe with Whip, Espresso with Milk and Whip, …
Subclass everything class Coffee extends Drink { class CoffeeWithMilk extends Drink { double cost() { double cost() { return 1.0; return 1.2; } } public String getName() { public String getName() { return "Coffee"; return "Coffee with milk"; } } } }
Subclass everything
Subclass everything class Coffee extends Drink { class CoffeeWithMilk extends Coffee { double cost() { double cost() { return 1.0; return super.cost() + 0.2; } } public String getName() { public String getName() { return "Coffee"; return super.getName() + „with milk“; } } } }
The first Decoration class MilkDecorator extends Drink { Drink drink ; public DrinkDecorator(Drink drink) { this.drink = drink; class Coffee extends Drink { } double cost() { double cost() { return 1.0; return drink.cost() + 0.2; } } public String getName() { public String getName() { return "Coffee"; return drink.getName() + „ with milk“; } } } }
The Decorator Pattern ����� ������ �������������� ������������� �������������
The Decorator Pattern abstract class Drink { abstract class DrinkDecorator extends Drink { public abstract String getName(); Drink drink; public abstract double cost(); public DrinkDecorator (Drink drink) { } this.drink = drink; } } class Coffee extends Drink { class MilkDecorator extends DrinkDecorator { public MilkDecorator(Drink drink) { super(drink); } public double cost() { return 1.0; } public double cost() { return drink.cost() + 0.2; } public String getName() { public String getName() { return "Coffee"; return drink.getName() + „ with milk“; } } } }
The Decorator Pattern Drink drink = new MilkDecorator( new WhipDecorator( new Coffee())); System.out.println( drink.cost() ); // Output: 1.6 ������ ������������� ������������� ������ ������ ������ ������ ��� ��� ���
The Decorator Pattern public class Bar { public static void main(String[] args) { Drink basicDrinks[] = { new Espresso(), new Coffee()}; for (Drink basicDrink:basicDrinks) { Espresso costs 1.5 Espresso with milk costs 1.7 Drink drink1 = basicDrink; Espresso with whip costs 1.9 System. out .println(drink1.getName() + " costs " + drink1.cost()); Espresso with milk with whip costs 2.1 Coffee costs 1.0 Drink drink2 = new MilkDecorator(basicDrink); Coffee with milk costs 1.2 System. out .println(drink2.getName() + " costs " + drink2.cost()); Coffee with whip costs 1.4 Drink drink3 = new WhipDecorator(basicDrink); Coffee with milk with whip costs 1.6 System. out .println(drink3.getName() + " costs " + drink3.cost()); Drink drink4 = new WhipDecorator( new MilkDecorator(basicDrink)); System. out .println(drink4.getName() + " costs " + drink4.cost()); } } }
The Decorator Pattern ��������� ����� ���������������������� ������������ ����������������� ��������� ������ �������������� ������������ ������������ ������������������ ������������������ ������������� ������������� ������������ ������������ ������������������ ����������� �����
GUI Decorators ��������� ��������������� ����������������� ��������� �������� ��������� ������������������ ������������������ ��������������� ���������������
GUI Decorators
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.
Java Streams ����������� � !�������������� ��������������� ����������������� ������������������� �������������������
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);
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.
Questions
Recommend
More recommend