Design patterns III : Structural Patterns J.Serrat 102759 Software Design September 29, 2013 Index Composite 1 Adapter 2 Decorator 3 Bridge 4 Proxy 5 Fa¸ cade 6 Flyweight 7
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Overview Behavioural patterns characterize the ways in which classes or objects interact and distribute responsibility Creational patterns concern the process of object creation Structural patterns deal with the composition of classes or objects to form larger structures (composite, bridge) or to realize new functionality (adapter . . . ) 3 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Composite See part I 4 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter Motivating example a company A buys company B, A’s payroll application must be expanded with B’s employees luckily, both applications are written in same OO language of course, names of classes, methods and signatures are different A: Employee , getName() , salary(Date) B: Worker , getFirstName() , getLastName() , wage(int month, int year) at the moment, reuse legacy code of B payroll application with minimum effort 5 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter 6 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter 7 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Structure Object adapter : relies on object composition 8 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Structure Class adapter : relies on multiple inheritance 9 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter An object adapter lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once. makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself. 10 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter A class adapter adapts Adaptee to Target by committing to a concrete Adapter class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses. lets Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee. introduces only one object, and no additional pointer indirection is needed to get to the adaptee. 11 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter Applicability want to use an existing class but its interface does not match the one you need want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces (object adapter only) you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one 12 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Adapter When no Target interface is present we have a wrapper method: method delegation: call a second method with little or no additional computation adapts an existing class or object to a different interface for reusing existing code 13 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Motivating example 1 Starbuzz Coffee started selling a few coffee beverage types: house blend, dark roast, decaf, espresso a few classes could then represent their offerings later on, more and more condiments you can optionally order were introduced : steamed milk, soy milk, mocha, caramel, whip . . . a customer can order none or any number of them served with a beverage Starbuzz charges a bit for each of these, so they really need to get them built into their order system 1 Head-first design patterns 14 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 15 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 16 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 17 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 18 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 19 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Problems: price changes for condiments force changes in Beverage class a new condiment − → new method in Beverage and change cost() by adding a new if what if we do not allow some condiments for a beverage (iced tea + whip) ? how to represent double mocha ? 20 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Solution decorator: “decorate” = wrap a beverage object with one or more condiments objects make a list of condiments and put at the end a beverage each condiment knows its own price and that it has to be added to the price of beverage or another condiment it contains , recursively 21 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 22 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator 23 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Second example 2 a GUI toolkit should let you add properties like borders or behaviours like scrolling to any visual component inheritance is not convenient because user can not choose whether to add or not: the visual component class has already inherited it we may want to add scrollbars or borders at run time 2 Gamma et al. 24 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator it is possible to decorate any VisualComponent with one or more decorators Decorator subclasses extend a VisualComponent’s draw() by drawing themselves and forward draw request 25 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Intent Attach additional responsibilities to an individual object dynamically, not to an entire class (inheritance). Decorators provide a flexible alternative to subclassing for extending functionality. Applicability add responsibilities to individual objects dynamically and without affecting other objects for responsibilities that can be withdrawn when extension by subclassing is impractical because would produce explosion of subclasses 26 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Decorator Structure 27 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Bridge Motivating example an industrial plant has machines of several types: press, cutter, injection molding, conveyor belt machines start/stop is software controlled at the moment every machine is thus directly operated by calling its start() and stop() methods start/stop is different depending on the machine type: presses start their associated feeder and output conveyors, cutters wait for a worker to press a confirmation button, etc. any machine can be asked for the number of processed items since last start operation 28 / 44
Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Bridge An abstraction, Machine , has one of several possible implementations, Press , Cutter which may override or extend start() and stop() 29 / 44 Overview Composite Adapter Decorator Bridge Proxy Fa¸ cade Flyweight You should know. . . Bridge Later on, new press, cutters, conveyor belts are bought which can be programmed to start and stop at given times, and even to do it periodically We are forced to add many new classes: every combination of { non-programmable, programmable } × { press, cutter, injection molding, conveyor belt } because start/stop are different and may have or not schedule() capability Inheritance binds an implementation to the abstraction permanently , which makes it difficult to modify, extend, and reuse abstractions and implementations independently 30 / 44
Recommend
More recommend