Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)

Similar presentations


Presentation on theme: "Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)"— Presentation transcript:

1 Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)

2 Why Use Design Patterns? Used to speed up development process. ◦No “reinvention of the wheel”. Improve code readability for coders and maintainers. Creates a starting point for design.

3 Façade Pattern: Introduction Façade means “frontage”, or “face” in French. ◦Generally used to describe the front of a building (architectural façade). Façade ◦An object that provides a simplified interface to a larger body of code. Structural Design Pattern ◦Ease the identification of relationship between entities

4 Façade Pattern: Main Idea The community need an easy way to use a complex system. ◦Create a simplified interface to interact with the system.

5 Façade Pattern: Motivation Allows for software Library ease of use and testing. Make code more readable and maintainable. ◦Structures code. Promotes usability for software users. ◦Usually known as: subroutines, methods, or functions.

6 Façade Pattern: Limitations Façade is in-between. ◦Interface between the client and the software resources/library. ◦Façade Pattern will limit the features and flexibility that overall affect the power of the user. Solution. ◦Give the user a power options (command prompt, expert options).

7 Façade Patterns: Limitations Too many methods. ◦Each method is a Façade Design pattern, this is a lot of patterns! Solution. ◦Limit the number of methods the user must interact with.

8 Façade Patterns: Limitations Useless information. ◦The information from the system resources may be meaningless to the user. Solution. ◦The interface may have to translate the meaning of the information (must do work).

9 Façade Patterns: Example From Wikipedia

10 Façade Pattern: Works Cited "Design Patterns - the Facade and Adapter Pattern." Scribd. Web. 24 Jan. 2011.. "Facade Design Pattern." Design Patterns and Refactoring. Web. 24 Jan. 2011.. "Facade Pattern." Wikipedia, the Free Encyclopedia. Web. 24 Jan. 2011..

11

12 Design Pattern: Singleton Singleton Creational Design Pattern David Archer

13 Design Pattern: Singleton

14

15 Singleton =? Global Variable

16 Design Pattern: Singleton Is the singleton design pattern a good idea?

17 Design Pattern: Singleton http://en.wikipedia.org/wiki/Singleton_pattern http://en.wikipedia.org/wiki/Dependency_injection http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29 http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 References:

18

19 Definition Creational design pattern that lets you define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

20 Diagram Image Credit: http://www.dofactory.com/Patterns/PatternFactory.aspx

21 Example Pizza pizza = new ChicagoStyle(); pizza = new HawaiianStyle(); pizza = new NewYorkStyle();

22 Example Pizza store Pizza order Pizza(){ Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

23 orderPizza Method “Fix” Pizza orderPizza(String type){ Pizza pizza = new Pizza(); if(type.equals(“cheese”)){ pizza = new CheesePizza();{ else if(type.equals.(“pepperoni”){ pizza = new PepperoniPizza(); else… rest of the menu…. pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

24 Factory public class SimplePizzaFactory{ public Pizza createPizza(String type){ Pizza pizza = null; if(type.equals(“cheese”)){ pizza = new CheesePizza();{ else if(type.equals.(“pepperoni”){ pizza = new PepperoniPizza(); else… rest of the menu…. return pizza; }

25 orderPizza Method Revisited public class PizzaStore{ SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory){ this.factory = factory; } public Pizza orderPizza(String type){ Pizza pizza; pizza = factory.createPizza(type); … }

26 Factory Advantages Decoupling ◦The factory decouples the calling class from the target class. Encapsulation ◦Factory methods encapsulate the creation of objects.

27 Factory Disadvantages Usually the constructor is often made private to force clients to use the factory methods, as a result since the pattern relies on using a private constructor, the class cannot be extended. We have tight coupling between Factory class and products.

28 Factory Disadvantages [Cont..] If we add any new concrete product we need a new case statement in the method of Factory class. This violates open/closed design principle. In object-oriented programming, the open/closed principle statesobject-oriented programming "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification";


Download ppt "Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)"

Similar presentations


Ads by Google