Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Design Patterns

Similar presentations


Presentation on theme: "Programming Design Patterns"— Presentation transcript:

1 Programming Design Patterns
Dependency Inversion Principle Design Principle Depend upon abstractions. Do not depend upon concrete classes. Stronger than “program to an interface, not an implementation”. High-level Components should NOT depend on low level components. Both should depend on abstractions. Last week: Factory method example Which one is the high level component Pizza Implementations (NYStyleCheezePizza) low level components What is the common abstraction? Homework Common abstraction is Pizza class Programming Design Patterns 11/13/2018 1 1

2 Programming Design Patterns
Object dependency diagram Page 138: chapter 4 Page 140: chapter 4 Inversion in “Dependency inversion principle” The top-to-bottom dependency diagram in page 138 has now been inverted Common abstraction is Pizza class Programming Design Patterns 11/13/2018 2 2

3 Programming Design Patterns
Guidelines for Dependency Inversion principle No variable should hold a reference to a concrete class Why? No class should derive from a concrete class No method should override an implementation of any of its base class Use factories instead of reference to a concrete class derive from abstract classes instead Do not override base class method, spoils the abstraction Programming Design Patterns 11/13/2018 3 3

4 Programming Design Patterns
Guidelines for Dependency Inversion principle (2) No variable should hold a reference to a concrete class Sure, it is impossible to program this way Use factories whenever possible Ok to instantiate classes that will never change Example: String No class should derive from a concrete class Reduce dependency on a concrete class Derive from an abstraction (abstract interface) Common abstraction is Pizza class Programming Design Patterns 11/13/2018 4 4

5 Programming Design Patterns
Guidelines for Dependency Inversion principle (3) No method should override an implementation of any of its base class methods If you need to override then the base class method wasn’t really an abstraction to start with. The idea of a base class method is to share the implementation with all the subclasses Common abstraction is Pizza class Programming Design Patterns 11/13/2018 5 5

6 Programming Design Patterns
Builder Pattern Encapsulate construction of a product Enforce a series of steps Suppose an OOCar company builds two models of cars: OOCamry and OOCorolla The car manufacturing process needs to follow steps in this order: buildEngine, buildBody, buildExterior, buildInterior Write object oriented code to build these cars The driver code should not change if the manufacturing steps change Programming Design Patterns 11/13/2018 6 6

7 Programming Design Patterns
OOCar Builder OOCar ooCamry = new OOCamry(v6engine, sedanBody, noLeatherInterior, blueColorExterior); // params are enums OOCar ooCorolla = new OOCorolla(...); // workshop constructs the car by putting // together the various parts in the right order OOWorkshopInterface shop = new OOWorkshop(); shop.construct(ooCamry); shop.construct(ooCorolla); Programming Design Patterns 11/13/2018 7 7

8 Programming Design Patterns
Car Workshop public class OOWorkShop implements WorkshopInterface { // force the order of building process // Obviously, this is a dummy workshop! public void construct(OOCar cb) { cb.buildEngine(); hb.buildBody(); cb.buildInterior(); cb.buildExterior(); } Programming Design Patterns 11/13/2018 8 8

9 Programming Design Patterns
Steps to build a car public abstract class OOCar { String carInfo; void setDescription(String carInfoIn); // the order of these methods is not important abstract public void buildInterior(); abstract public void buildExterior(); abstract public void buildEngine(); abstract public void buildBody(); } [Could use an interface instead of relying on inheritance] Programming Design Patterns 11/13/2018 9 9

10 Programming Design Patterns
Build OOCamry public class OOCamry extends OOCar { public OOCamry(String description) { setDescription(description); } public void buildInterior() { // Camry specific } public void buildExterior(){ // Camry specific } public void buildEngine(){ // Camry specific } public void buildBody(){ // Camry specific } Programming Design Patterns 11/13/2018 10 10

11 Programming Design Patterns
Build OOCorolla public class OOCorolla extends OOCar { public OOCamry(String description) { setDescription(description); } public void buildInterior() { // Corolla specific } public void buildExterior(){ // Corolla specific } public void buildEngine(){ // Corolla specific } public void buildBody(){ // Corolla specific } Programming Design Patterns 11/13/2018 11 11

12 Programming Design Patterns
OOCar Builder OOCar ooCamry = new OOCamry(v6engine, sedanBody, noLeatherInterior, blueColorExterior); // params are enums OOCar ooCorolla = new OOCorolla(...); // workshop constructs the car by putting // together the various parts in the right order OOWorkshopInterface shop = new OOWorkshop(); shop.construct(ooCamry); shop.construct(ooCorolla); Programming Design Patterns 11/13/2018 12 12

13 Programming Design Patterns
Builder Pattern Encapsulate the construction of a product and allow it to be constructed in steps Encapsulates the way a complex object is created Multi-step construction process Note that factory is a one step process Internal implementation hidden from client Client just sees the abstract interface, and so the underlying implementations can be changed Programming Design Patterns 11/13/2018 13 13

14 Programming Design Patterns
Back to making Pizzas with factories Use factories to allow specialized pizzas to have their own ingredient variations example: fresh tomatoes/canned- tomatoes Programming Design Patterns 11/13/2018 14 14

15 Programming Design Patterns
Original Pizza class public abstract class Pizza { String name, dough, sauce, cheese; void prepare() { } void bake() { Force specializations to prepare() using their own ingredients. How can this be done? Programming Design Patterns 11/13/2018 15 15

16 Programming Design Patterns
Pizza class: enforce use of specialized prepare() public abstract class Pizza { String name, dough, sauce; abstract void prepare(); void bake() { } Programming Design Patterns 11/13/2018 16 16

17 Programming Design Patterns
Pizza class: enforce use of factory to implement prepare() public abstract class Pizza { String name, dough, sauce; // Classwork: force use of special factories // to define the prepare method void prepare(); void bake() { } Programming Design Patterns 11/13/2018 17 17

18 Programming Design Patterns
Classwork: PizzaIngredientFactory Define an interface PizzaIngredientFactory Add a method to create each ingredient Idea is to have subclasses provide specialized implementations for ingredients Programming Design Patterns 11/13/2018 18 18

19 Programming Design Patterns
Interface for ingredients public interface PizzaIngredientFactory { public Dough createDough(); public Sauce createSauce(); public Cheese createCheese(); public Veggies[] createVeggies(); } Programming Design Patterns 11/13/2018 19 19

20 Programming Design Patterns
NYStylePizza’s ingredients public NYPizzaIngredientFactory implements PizzaIngredientFactory { public Sauce createSauce() { } public Veggies[] createVeggies() { Programming Design Patterns 11/13/2018 20 20

21 Programming Design Patterns
Cheese Pizza using factory (1) public class CheesePizza extends Pizza { // constructor should take an ingredient factory // the prepare method should create // specialized dough, sauce, cheese } Programming Design Patterns 11/13/2018 21 21

22 Programming Design Patterns
Cheese Pizza using factory (2) public class CheesePizza extends Pizza { private PizzaIngredientFactory ingredientFactory; Private Dough dough; // rest of the ingredients public CheesePizza(PizzaIngredientFactory ingFactory) { ingredientFactory = ingFactory; } public void prepare(// parameterized if needed) { dough = ingredientFactory.createDough(); sauce = ingredientFactory.createSauce(); cheese = ingredientFactory.createCheese(); Programming Design Patterns 11/13/2018 22 22

23 Programming Design Patterns
Abstract Factory Pattern Provides an interface for creating families of related or dependent objects without specifying their concrete classes Variety of factories can be used for different contexts Decouples driver code from the actual factory that creates products Revisit PizzaIngredientFactory: it calls createDough(), createSauce(), etc. This is the family of related objects Each of these create() methods could use a simple factory Not surprisingly the create() call looks like a factory call Programming Design Patterns 11/13/2018 23 23

24 Programming Design Patterns
Abstract Factory Pattern and Factory method patterns What are the similarities? What are the differences? Which one creates objects? Which one decouples client code from concrete objects? Which design principle is followed in them? Which is one is better for maintenance? Programming Design Patterns 11/13/2018 24 24

25 Programming Design Patterns
Abstract Factory Pattern and Factory method patterns (2) Both create objects Both decouple client code from concrete types Factory Method uses inheritance Abstract Factory uses composition Provides interface to create a family of products Subclasses (those implementing the interface) decide how concrete objects are created Instantiate a factory and pass it to code written against the factory interface Programming Design Patterns 11/13/2018 25 25

26 Programming Design Patterns
Abstract Factory Pattern and Factory method patterns (3) Abstract Factory: If you need to add new products Need to change the interface! Oh! Also, the interface is usually big (lots of methods) Factory Method Usually has just one method (orderPizza(…)) Implements code in the abstract creator that makes use of the concrete types the subclasses create Programming Design Patterns 11/13/2018 26 26


Download ppt "Programming Design Patterns"

Similar presentations


Ads by Google