the decorator pattern
play

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


  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 { 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"; } } } }

  4. Subclass everything

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

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

  7. The Decorator Pattern ����� ������ �������������� ������������� �������������

  8. 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“; } } } }

  9. The Decorator Pattern Drink drink = new MilkDecorator( new WhipDecorator( new Coffee())); System.out.println( drink.cost() ); // Output: 1.6 ������ ������������� ������������� ������ ������ ������ ������ ��� ��� ���

  10. 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()); } } }

  11. The Decorator Pattern ��������� ����� ���������������������� ������������ ����������������� ��������� ������ �������������� ������������ ������������ ������������������ ������������������ ������������� ������������� ������������ ������������ ������������������ ����������� �����

  12. GUI Decorators ��������� ��������������� ����������������� ��������� �������� ��������� ������������������ ������������������ ��������������� ���������������

  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 ����������� � !�������������� ��������������� ����������������� ������������������� �������������������

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend