1 / 28

Design Patterns

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?.

kiora
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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?

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Façade Patterns: Example From Wikipedia

  10. "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

  11. Singleton Creational Design Pattern David Archer Design Pattern: Singleton

  12. Design Pattern: Singleton

  13. Design Pattern: Singleton

  14. Singleton =? Global Variable Design Pattern: Singleton

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

  16. 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

  17. 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

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

  19. Pizza pizza = new ChicagoStyle(); pizza = new HawaiianStyle(); pizza = new NewYorkStyle(); Example

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

  21. 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”

  22. 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

  23. 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

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

  25. 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

  26. 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..]

More Related