Factory Method - PowerPoint PPT Presentation

About This Presentation
Title:

Factory Method

Description:

Title: Factory Method Subject: Design Patterns Author: Ivaylo Hristov Keywords: design patterns, factory method Description: This presentation is part of the design ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 29
Provided by: Ivay8
Learn more at: https://www.devbg.org
Category:
Tags: factory | method | trott

less

Transcript and Presenter's Notes

Title: Factory Method


1
Factory Method
Design Patterns in C
www.devbg.org/patternscourse/
?????? ??????? www.ivaylo-hristov.net
2
??????????
  • ?????????
  • Simple Factory
  • Factory Method
  • ???????? ??????????
  • ???? ?? ?????????? Factory
  • ?? ? ?????? ???????
  • ????????? ?? ?????????

3
?????????
  • ???????????? ?????? ?? ?????
  • ???????
  • ??????
  • ??????????
  • ??????? ?? ????
  • ???????? ?? ????

?????? ?? ????????? ??????? ???????, ????? ??
????? ?? ??????????, ? ????? ????? ?? ?? ???????
? ??????????? ???????
4
Factory
  • ???????? ?? ???????? ?? ??????? ?????.
  • ?????????? ?????? ???? ???? ???????? ???????.
  • ???? ?? ????????? ?? ????????? ?? ?????? ??????
    ?? ???????????????? ? ????????? ? ????????? ??
    ??????????

5
Simple Factory
  • ???? ?? ? ??????!
  • ????? ?? ????? ? ??????? Factory
  • ???????? ?? ????? ?????
  • ???? ?? ? ?????? ????????? ?? ????????? ??????

6
Simple Factory
  • ?? ?? ???????? ????

7
Simple Factory
Pizza OrderPizza(PizzaType aType) Pizza pizza
null switch (aType) case
PizzaType.Cheese return new CheesePizza()
case PizzaType.Pepperoni return new
PepperoniPizza() case PizzaType.Hawai re
turn new HawaiPizza() default return
null (???????? ??????????)
8
Simple Factory
pizza.prepare() pizza.bake() pizza.cut()
pizza.box() return pizza
9
Simple Factory
public class SimplePizzaFactory public Pizza
CreatePizza(PizzaType aType) Pizza pizza
null switch (aType) case
PizzaType.Cheese return new CheesePizza()
case PizzaType.Pepperoni return new
PepperoniPizza() case PizzaType.Hawai r
eturn new HawaiPizza() default return
null return pizza
10
Simple Factory
  • ???????? ??????????? ?? ?????? ?? ???? ?????
  • ??? ?????? ??????? ????????? ???? ?? ???? ?????
  • ????? ?? ???????? ?????? ????????? ?? ?????
  • ?????????? ?????? ?? ??????????

11
??????? ?? ????
  • ?????? ?? ???????? ??????? ?? ????
  • ????? ???????? ?????? ????
  • ?????? ?? ????? ???????
  • ?????? ?? ???????? ? ?????????? ???????

12
???????? ??????????
public abstract class Pizza protected string
Name protected string Dough protected string
Souce protected ArrayList Toppings new
ArrayList() public void Prepare() Conso
le.WriteLine ("Preparing " Name) Console.Wr
iteLine ("Tossing dough") Console.WriteLine ("
Adding souce ... ") Console.WriteLine ("
Adding toppings ") for ( int i 0 i lt
Toppings.Count i ) Console.WriteLine ("
0", Toppingsi) (????????
??????????)
13
???????? ??????????

public void Bake()
Console.WriteLine("Bake for 25 minutes at 350")
public void Cut()
Console.WriteLine("Cut the pizza in diagonal
slices") public void Box()
Console.WriteLine ("Place the pizza in
official box")
14
???????? ??????????
public class NYStyleCheesePizza Pizza
public NYStyleCheesePizza() Name "NY
Style Sauce and Cheese Pizza" Dough "Thin
Crust Dough" Souce "Marinara
Souce" Toppings.Add("Grated Reggiano
Cheese")
15
???????? ??????????
public abstract class PizzaStore public Pizza
OrderPizza(PizzaType aType) Pizza
pizza pizza CreatePizza(aType) pizza.prepa
re() pizza.bake() pizza.cut() pizza.box()
return pizza protected abstract Pizza
CreatePizza(PizzaType aType)
16
???????? ??????????
public class NYPizzaStore PizzaStore
protected override Pizza CreatePizza(PizzaType
aType) switch (aType ) case
PizzaType.Cheese return new
NYStyleCheesePizza() case
PizzaType.Pepperoni return new
NYStylePepperoniPizza () case
PizzaType.Hawai return new
NYStyleHawaiPizza () default return
null
17
???????? ??????????
18
???????? ??????????
19
????????????

Factory Method
20
???? ?? ?????????? Factory
  • ?????? ?????? ???? ?? ????????? ?????, ?? ??
    ?????? ????? ?? ????? ??? ?????? ?? ???? ????
    ?????
  • ??????? ???? ?? ?????? ?? ?????? ????? ????? ??
    ???????
  • ?????? ?? ???????? ??????? ????????? (framework)

21
?????????? ?? Factory
  • ????? ?? ???????? ?????????? ? ???????
  • ??????????? ??????? ???? ??? ??? ?????
  • ???????????? ?? ???? ?????? ????? ?????????? ?
    ???????????? ??? ?? ? ?????????? ? ??????.

22
?? ? ?????? ???????
  • ??
  • ????? ????? ?? ?????? ???????
  • ????? ????? ?? ?????????????? ??????????
    ?????????????? (???????? ???????? ?? ??????)
  • ?????????? ?????? ?? ??????????
  • ??????
  • ???????? ?? ??????????
  • ?? ???? ?? ?? ????????? ?????????? ?????????
    ???????? (???????? ?? ????? ?? ??????????
    ???????? ? using)

23
Factory Method in the real life
  • ???????? ?????????? ?? ??????? ?? ?????????
    ?????? ? .NET Framework
  • System.Net.WebRequest
  • IEnumerable
  • ????????????? ?? ???????? ? Parse
  • ? ?? ????? ????? ????? ?????

24
????????? ?? ?????????
  • ?????????????? ? Factory ???????? ?? ?? ????????
    ???? ?? ???????? Create ? ????, ????? ??
    ???????.
  • CreateButton
  • CreatePizza
  • ?????????????? ? ?? ????????? ???????? ?????????
    Factory ?????? ???????????? ????, ????? ??
    ??????? ? Factory
  • ControlFactory
  • PizzaFactory

25
Factory Method
?
?
?
?
?
????????
?
?
?
26
?????????? ??????????
  • Discover the Design Patterns You're Already Using
    in the .NET Framework Head First
  • Best Practices Seeing Patterns The Factory
    Method
  • Exploring the Factory Design Pattern
  • www.dofactory.com - Factory Method
  • Design Guidelines Update Factories vs.
    Constructors
  • Non-Software Examples of Software Design Patterns
  • The Factory Method (Creational) Design Pattern

27
?????????? ??????????
  • C Design Patterns A Tutorial
  • By James W. Cooper
  • Design Patterns in C
  • By Steven John Metsker
  • Design Patterns Explained
  • By Alan Shalloway, James Trott

28
?????????? ??????????
  • Head First Design Patterns
  • By Elisabeth Freeman, Eric Freeman, Bert Bates,
    Kathy Sierra
  • Applied Java Patterns
  • By Stephen Stelting, Olaf Maassen
Write a Comment
User Comments (0)
About PowerShow.com