Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
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.
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
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.
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.
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).
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.
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: Example From Wikipedia
Façade Pattern: Works Cited "Design Patterns - the Facade and Adapter Pattern." Scribd. Web. 24 Jan "Facade Design Pattern." Design Patterns and Refactoring. Web. 24 Jan "Facade Pattern." Wikipedia, the Free Encyclopedia. Web. 24 Jan
Design Pattern: Singleton 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 References:
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.
Diagram Image Credit:
Example 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; }
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; }
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; }
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); … }
Factory Advantages Decoupling ◦The factory decouples the calling class from the target class. Encapsulation ◦Factory methods encapsulate the creation of objects.
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.
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";