Design Patterns Станкевич Ф.В. Design Patterns Creational Design Patterns • Singleton • Factory & Factory method • Abstract Factory • Builder • Prototype Structural Design Patterns • Decorator • Adapter • Façade • Bridge • Proxy • Composite Behavioral Design Patterns • Iterator • Strategy • State • Observer • Command • Memento • Chain of Responsibility • Mediator 2 Creational Design Patterns 3 Factory 4 Design principle: Program to an interface, not an implementation. But every time you use new, that’s exactly what you are doing. 5 What’s wrong with “new”? 1. The code is not closed to modification! 2. The code is changed as new as new classes are added 6 Factory Method Example The code is still parameterized 7 Factory Method UML class Factory Pattern Creator Product + + FactoryMethod() AnOperation() ConcreteCreator ConcreteProduct «instantiate» + FactoryMethod() return new ConcreteProduct 8 Builder Pattern UML The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations class Builder Pattern Director + Construct() for all objects in structure { builder->BuildPart() } Builder -builder + BuildPart() ConcreteBuilder + + Product BuildPart() GetResult() : void 9 Example: Vacation Builder class Builder Pattern AbstractBuilder Client + main(String[]) : void + + + + + + buildDay(String) : void addHotel(String) : void addReservation(String) : void addSpecialEvent(String) : void addTickets(String) : void getVacationPlanner() : void VactionBuilder + + + + + + addHotel(String) : void addReservation(String) : void addSpecialEvent(String) : void addTickets(String) : void getVacationPlanner() : void buildDay(String) : void 10 Singleton Pattern class Singleton Singleton - uniqueInstance singletonData + Instance() return uniqueInstance SingletonOperation() GetSingletonData() + + 11 Behavioral Design Patterns 12 Strategy UML 13 Strategy Example 14 Strategy 15 State Allows an object to alter its behavior when its internal state changes. 16 State UML 17 State 18 Template method UML 19 Example: Template Method 20 Structural Design Patterns 21 Adapter Pattern 22 Problem to solve Your Existing System Vendor Class The interfaces doesn’t match 23 Adapter Your Existing System Vendor Class Adapter 24 Adapter Your Existing System No changes Vendor Class Adapter No changes New code 25 Adapter UML 26 Adapter Example Use adapter Proxy Client Proxy Real Subject 29 Proxy UML 30 Proxy 31 Façade 32 Façade UML 33 Façade Example Литература • С.Стелтинг, О.Маассен - Применение шаблонов JAVA • М.Гранд - Шаблоны проектирования в Java • Э.Фримен, Э.Фримен – Паттерны проектирования 35 Observer 36 Command 37 Abstract Factory UML class Families of ingredients AbstractFactory Client + + CreateProductA() CreateProductB() AbstractProductA ProductA2 ConcreteFactory1 ProductA1 ConcreteFactory2 AbstractProductB + + CreateProductA() CreateProductB() + + CreateProductA() CreateProductB() ProductB2 ProductB1 38 Design Patterns Станкевич Ф.В.