Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns. General reusable solution to a commonly occurring problem in software design software design – Not a finished design that can be transformed.

Similar presentations

Presentation on theme: "Design Patterns. General reusable solution to a commonly occurring problem in software design software design – Not a finished design that can be transformed."— Presentation transcript:

1 Design Patterns

2 General reusable solution to a commonly occurring problem in software design software design – Not a finished design that can be transformed directly into code.code – A description or template for how to solve a problem that can be used in many different situations. – Show relationships and interactions between classes or objects,interactionsclassesobjects Not specifying the final application classes or objects that are involved. Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems Algorithms computationaldesign Not all software patterns are design patterns – Design patterns deal specifically with problems at the level of software design – Other kinds of patterns, such as architectural patterns, describe problems and solutions that have alternative scopesarchitectural patterns

3 Classification Originally grouped design patterns into the categories – Creational Patterns Creational Patterns – Structural Patterns Structural Patterns – Behavioral Patterns Behavioral Patterns Object-oriented designs - use inheritance, interface, and polymorphisminheritance interfacepolymorphism

4 Creational Patterns Deal with object creation mechanismsobject creation – Create objects in a manner suitable to the situation Some examples: – Singleton pattern: restrict instantiation of a class to one object Singleton pattern – Prototype pattern: used when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application Prototype pattern – Factory method pattern: centralize creation of an object of a specific type choosing one of several implementations Factory method pattern – Abstract factory pattern: centralize decision of what factory to instantiate Abstract factory patternfactory – Builder pattern: separate the construction of a complex object from its representation so that the same construction process can create different representations Builder pattern – Lazy initialization pattern: delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed Lazy initialization pattern – Object pool: avoid expensive acquisition and release of resources by recycling objects that are no longer in use Object pool

5 Structural Patterns Ease the design by identifying a simple way to realize relationships between entities Some examples: – Decorator pattern: add additional functionality to a class at runtime where subclassing would result in an exponential rise of new classes Decorator pattern – Composite pattern: a tree structure of objects where every object has the same interface Composite pattern – Adapter pattern: 'adapts' one interface for a class into one that a client expects Adapter pattern – Bridge pattern: decouple an abstraction from its implementation so that the two can vary independently Bridge pattern – Facade pattern: create a simplified interface of an existing interface to ease common tasks Facade pattern – Flyweight pattern: a high quantity of objects share a common properties object to save space Flyweight pattern – Proxy pattern: a class functioning as an interface to another thing Proxy pattern – Pipes and filters: a chain of processes where the output of each process is the input of the next Pipes and filters

6 Behavioral Patterns Identify common communication patterns between objects and realize these patterns. – increase flexibility in carrying out this communication. Some examples: – Iterator pattern: Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation Iterator pattern – Observer pattern: aka Publish/Subscribe or Event Listener. Objects register to observe an event which may be raised by another object Observer pattern – Command pattern: Command objects encapsulate an action and its parameters Command pattern – Chain of responsibility pattern: Command objects are handled or passed on to other objects by logic-containing processing objects Chain of responsibility pattern – Mediator pattern: Provides a unified interface to a set of interfaces in a subsystem Mediator pattern – Memento pattern: Provides the ability to restore an object to its previous state (rollback) Memento pattern – State pattern: A clean way for an object to partially change its type at runtime State pattern – Strategy pattern: Algorithms can be selected on the fly Strategy pattern – Template method pattern: Describes the program skeleton of a program Template method patternprogram skeleton – Visitor pattern: A way to separate an algorithm from an object Visitor pattern

7 Creational Patterns Factory Method Abstract Factory Builder Lazy Initialization

8 Factory Method Any method that constructs and returns an Object – Allow several subclasses to have different implementations – Allow multiple constructors with same arguments but different semantics

9 Factory Method class Complex { public static Complex fromCartesian(double real, double imag) { return new Complex(real, imag); } public static Complex fromPolar(double modulus, double angle) { return new Complex(modulus * cos(angle), modulus * sin(angle)); } private Complex(double a, double b) { //... } Complex c = Complex.fromPolar(1, pi); // Same as fromCartesian(-1, 0)

10 Factory Method public interface ImageReader { public DecodedImage getDecodedImage(); } public class GifReader implements ImageReader { public GifReader( InputStream in ) { // check that it's a gif, throw exception if it's not, then if it is // decode it. } public DecodedImage getDecodedImage() { return decodedImage; } public class JpegReader implements ImageReader { //.... }

11 Abstract Factory

12 Builder Separate the construction of a complex object from its representation – same construction process can create different representations

13 Builder Example /** "Product" */ class Pizza { private String dough = ""; private String sauce = ""; private String topping = ""; public void setDough(String dough) { this.dough = dough; } public void setSauce(String sauce) { this.sauce = sauce; } public void setTopping(String topping) { this.topping = topping; }

14 Builder Example /** "Abstract Builder" */ abstract class PizzaBuilder { protected Pizza pizza; public Pizza getPizza() { return pizza; } public void createNewPizzaProduct() { pizza = new Pizza(); } public abstract void buildDough(); public abstract void buildSauce(); public abstract void buildTopping(); }

15 Builder Example /** "ConcreteBuilder" */ class HawaiianPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("cross"); } public void buildSauce() { pizza.setSauce("mild"); } public void buildTopping() { pizza.setTopping("ham+pineapple"); }

16 Builder Example /** "Director" – Pizza always constructed the same way – dough, sause, topping*/ class Cook { private PizzaBuilder pizzaBuilder; public void setPizzaBuilder(PizzaBuilder pb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.createNewPizzaProduct(); pizzaBuilder.buildDough(); pizzaBuilder.buildSauce(); pizzaBuilder.buildTopping(); }

17 Builder Example /** A given type of pizza being constructed. */ public class BuilderExample { public static void main(String[] args) { Cook cook = new Cook(); PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder(); cook.setPizzaBuilder(hawaiianPizzaBuilder); cook.constructPizza(); Pizza pizza = cook.getPizza(); }

18 Another Builder Use Extract records from a DB (say courses) and creates several outputs – XML, binary, text, … – A director reads the DB records, and knows how to construct the document – Although the general generated document has the same structure, the components differ

19 Lazy Initialization Creation of an object takes a long time – Download resources, heavy computation Many possible objects required – We want to delay the creation until we know for sure we really need the object

20 Lazy Initialization Example public class Fruit { private static final Map types = new HashMap (); private final String type; // using a private constructor to force use of the factory method. private Fruit(String type) { // This presumably takes a lot of time this.type = type; } /** * Lazy Factory method, gets the Fruit instance associated with a certain type. */ public static synchronized Fruit getFruit(String type) { Fruit f = types.get(type); // get the instance for that type if (f == null) { f = new Fruit(type); // lazy initialization types.put(type,f); } return f; }

21 Structural Patterns Composite Proxy Facade

22 Composite Allow a group of objects to be treated in the same way as a single instance of an object – Compose objects into tree structures of part-whole hierarchies – Ease discrimination between a leaf and a branch Example: Swing GUI containers (JPanel)

23 Composite Example /** "Component" */ interface Graphic { public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { private List mChildGraphics = new ArrayList (); public void print() { for (Graphic graphic : mChildGraphics) { graphic.print(); } public void add(Graphic graphic) { mChildGraphics.add(graphic); } public void remove(Graphic graphic) { mChildGraphics.remove(graphic); } class Ellipse implements Graphic { public void print() { System.out.println("Ellipse"); }

24 Composite Example /** Client */ public class Program { public static void main(String[] args) { Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); }

25 Proxy Generally, a class functioning as an interface to another thing – Could be anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate Examples – Remote proxy: reference to object located in a different address or different machine – Virtual proxy: Allows the creation of a memory intensive object on demand. The object will not be created until it is really needed. – Cache proxy: Provides temporary storage of the results of expensive target operations so that multiple clients can share the results. – Smart reference proxy: Provides additional actions whenever a target object is referenced, such as counting the number of references to the object

26 Proxy Delegates to a real subject that implements the same interface, but adds functionality

27 Proxy Example interface Image { public void displayImage(); } class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() { // Potentially expensive operation } public void displayImage() { System.out.println("Displaying "+filename); } } class ProxyImage implements Image { private String filename; private Image image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if (image == null) { image = new RealImage(filename); // load only on demand } image.displayImage(); }

28 Facade Provides a simplified interface to a larger body of code, such as a library. A facade can: – Make library easier to use and understand Has convenient methods for common tasks; – Make code that uses the library more readable – Reduce dependencies of outside code on the inner workings of a library Most code uses the façade – Wrap a poorly designed collection of APIs with a single well-designed API (As per task needs).

29 Façade Example /** "Facade" * */ class UserfriendlyDate { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public UserfriendlyDate(String isodate_ymd) throws ParseException { Date date = sdf.parse(isodate_ymd); cal.setTime(date); } public void addDays(int days) { cal.add(Calendar.DAY_OF_MONTH, days); } public String toString() { return sdf.format(cal.getTime()); } class FacadePattern { public static void main(String[] args) throws ParseException { UserfriendlyDate d = new UserfriendlyDate(" "); System.out.println("Date: " + d.toString()); d.addDays(20); System.out.println("20 days after: " + d.toString()); }

30 Behavioral Patterns (Iterator) (Observer) Template Method Strategy

31 Template Method Allow a general algorithm to be defined, where some steps are only defined in deriving classes – A class provides the basic steps of an algorithm, where some steps may have several implementations. Steps are implemented using abstract methods. – Subclasses change the abstract methods to implement real actions – The general algorithm is saved in one place but the concrete steps may be changed by the subclasses.

32 Template Method Example abstract class Game { private int playersCount; abstract void initializeGame(); abstract void makePlay(int player); abstract boolean endOfGame(); abstract void printWinner(); final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()){ makePlay(j); j = (j + 1) % playersCount; } printWinner(); } class Monopoly extends Game { void initializeGame() { //... } void makePlay(int player) { //... }

33 Strategy Allow used algorithm to be changed dynamically – Algorithm can be selected at runtime Provides means to define a family of algorithms – Encapsulate each one as an object – Make them interchangeable Lets the algorithms vary independently from clients that use them Example – Choose the sorting algorithm (strategy) at runtime – Choose a different opponent strategy in a game

34 Strategy Example public class StrategyExample { public static void main(String[] args) { Context context; context = new Context(new ConcreteStrategyA()); context.execute(); } interface IStrategy { void execute(); } class ConcreteStrategyA implements IStrategy { public void execute() { System.out.println( "Called ConcreteStrategyA.execute()" ); } class ConcreteStrategyB implements IStrategy { public void execute() { System.out.println( "Called ConcreteStrategyB.execute()" ); } class Context { IStrategy strategy; public Context(IStrategy strategy) { this.strategy = strategy; } public void execute() { this.strategy.execute(); }

Download ppt "Design Patterns. General reusable solution to a commonly occurring problem in software design software design – Not a finished design that can be transformed."

Similar presentations

Ads by Google