280 likes | 396 Views
Design Patterns. Façade, Singleton, and Factory Methods Team Good Vibrations (1). Used to speed up development process. No “reinvention of the wheel”. Improve code readability for coders and maintainers. Creates a starting point for design. Why Use Design Patterns?.
E N D
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
Used to speed up development process. • No “reinvention of the wheel”. • Improve code readability for coders and maintainers. • Creates a starting point for design. Why Use Design Patterns?
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 Façade Pattern: Introduction
The community need an easy way to use a complex system. • Create a simplified interface to interact with the system. Façade Pattern: Main Idea
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. Façade Pattern: Motivation
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). Façade Pattern: 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. 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). Façade Patterns: Limitations
Façade Patterns: Example From Wikipedia
"Design Patterns - the Facade and Adapter Pattern." Scribd. Web. 24 Jan. 2011. <http://www.scribd.com/doc/2283673/Design- patterns-the-facade-and-adapter-pattern>. • "Facade Design Pattern." Design Patterns and Refactoring. Web. 24 Jan. 2011. <http://sourcemaking.com/design_patterns/facade>. • "Facade Pattern." Wikipedia, the Free Encyclopedia. Web. 24 Jan. 2011. <http://en.wikipedia.org/wiki/Facade_pattern>. Façade Pattern: Works Cited
Singleton Creational Design Pattern David Archer Design Pattern: Singleton
Singleton =? Global Variable Design Pattern: Singleton
Is the singleton design pattern a good idea? 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: Design Pattern: Singleton
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. Definition
Diagram Image Credit: http://www.dofactory.com/Patterns/PatternFactory.aspx
Pizza pizza = new ChicagoStyle(); pizza = new HawaiianStyle(); pizza = new NewYorkStyle(); Example
Pizza store Pizza order Pizza(){ Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } Example
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; } orderPizza Method “Fix”
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; } } Factory
public class PizzaStore{ SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory){ this.factory = factory; } public Pizza orderPizza(String type){ Pizza pizza; pizza = factory.createPizza(type); … } } orderPizza Method Revisited
Decoupling • The factory decouples the calling class from the target class. • Encapsulation • Factory methods encapsulate the creation of objects. Factory Advantages
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. Factory Disadvantages
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 states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; Factory Disadvantages [Cont..]