创建型设计模式 培训(英文)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Simple Pattern
public class PizzaStore { Pizza orderPizza(String type) { Pizza pizza = null; if(type.equals("chees")) { pizza = new CheesePizza(); } else if (type.equals("greek")) { pizza = new GreekPizza(); } else if (type.equals("pepperoni")) { pizza = new PepperoniPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
Simple Pattern

Mending design of pizza story
Simple Pattern

public class SimplePizzaFactory { public static Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("chees")) { pizza = new CheesePizza(); } else if (type.equals("greek")) { pizza = new GreekPizza(); } else if (type.equals("pepperoni")) { pizza = new PepperoniPizza(); } return pizza; } }
Merge Creator into ConcreteProduct
Simple Pattern

Advantage



Disadvantage
Reuse the code that create object
If product has any change, Creator class should be impacted. OCP: if add new type of product, in the view of client and product, it conform to OCP, but in the view of Factory Creator, it does not.
Factory Pattern

Simple Factory

Simply moved the code that is used to create object to a new class Creator.
Fra Baidu bibliotek
Factory Pattern

Pizza Story

You want to open a pizza store, Assume that you organize like this:
Add SimplePizzaFactory Class
public class PizzaStore { SimplePizzaFactory factory; public Pizza orderPizza(String type) { Pizza pizza = SimplePizzaFactory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }

Common & Difference of Design Pattern
Creational Pattern and Refactor
Bryan Li 6/18/2007
Agenda

OO Design Principle



Design Pattern

OCP(Open-Closed Principle) LSP(Liskov Substitution Principle) SRP(single responsibility Principle) DIP(Dependence Inversion Principle) ISP(Interface Segregation Principle) CARP(Composite/Aggregate Reuse Principle) LoD(Las od Demeter—Least Knowledge PrincipleLKP)

OO Design Principle

Factory Method

Define an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Creational Pattern Structural Pattern Behavioral Pattern

Agenda

Creational Pattern

Factory Pattern

Simple Factory Factory Method Abstract Factory
Factory Method

Pizza Story


Because your pizza store has a good business, many people want to open your sub store in New York and Chicago. So you will the localize favor problem. Based on simple factory, design like the following:
Simple Pattern

Feature




Fade Format

you don't need to instantiate an object to make use of the create method you can't create sublcass and change the behavior of the create method Not typical design pattern-- more of a programming idiom
相关文档
最新文档