Design Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Design Patterns

Description:

Title: PowerPoint Author: apple Created Date: 12/11/2000 5:05:23 AM Document presentation format: (4:3) Company – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 43
Provided by: appl141
Category:

less

Transcript and Presenter's Notes

Title: Design Patterns


1
Design Patterns ??????? ????????? 20040074 ???
2
(No Transcript)
3
Contents.
  1. Think about
  2. Strategy Pattern
  3. Observer Pattern
  4. Decorator Pattern

4
Think about
5
?? ?? ?????
  • ????, ????, ?? ?? ?
  • ?? ???? ? ????,
  • ???, ?? ???,
  • ? ?? ???, ? ?? ???,
  • ?? ??? ?? ??
  • ?? ?? ????? ??

6
Simple Duck
Duck
Quack() Swim() Display() //?? ???????
MallardDuck
Display() // ??? ??? ??
RedheadDuck
Display() // ??? ??? ??
7
????? ??
Duck
Quack() Swim() Display() Fly() //?? ???????
MallardDuck
Display() // ??? ??? ??
RedheadDuck
Display() // ??? ??? ??
8
?? ?? ??
Duck
Quack() Swim() Display() Fly() //?? ???????
RubberDuck
quack() // ?? ????? ????? Display() // ??? ??? ??
MallardDuck
Display() // ??? ??? ??
RedheadDuck
Display() // ??? ??? ??
9
?? ?? ?? ??
Duck
Quack() Swim() Display() Fly() //?? ???????
RubberDuck
quack() // ?? ????? ????? Fly() // ???? ?? Display() // ??? ??? ??
MallardDuck
Display() // ??? ??? ??
RedheadDuck
Display() // ??? ??? ??
10
??? ? ????
DecoyDuck
quack() // ?????? Fly() // ???? ?? Display() // ???? ??
Duck
Quack() Swim() Display() Fly() //?? ???????
RubberDuck
quack() // ?? ????? ????? Fly() // ???? ?? Display() // ??? ??? ??
MallardDuck
Display() // ??? ??? ??
RedheadDuck
Display() // ??? ??? ??
11
??? ??
  • ????????
  • ???? ??? ????,
  • ???? ?? ?????? ?????.
  • ?, ??? ?? ??( fly ) ?
  • ???? ??( quack )? ?????,
  • ? ???? ?? ?????
  • ??? ? ??? ??.

12
Diagram
Duck
Swim() Display() //?? ???????
Flyable
Fly()
Quackable
Quack()
MallardDuck
Display() Fly() Quack()
RedheadDuck
Display() Fly() Quack()
RubberDuck
Display() Quack()
DecoyDuck
Display()
13
Strategy Pattern
14
??? ??
  • 1. ??? ??, ?????? ??
  • ????? ??.?????? ?? ????? ?? ?? ??? ?? ?????
    ??
  • 2. ????? ??? ????.

15
?????
Dog d new Dog() d.bark() Animal animal new
Dog() Animal.makeSound() A
getAnimal) a.makeSound()
Animal
MakeSound()
Dog
MakeSound() bark() Bark() // ??
Cat
MakeSound() meow() meow() // ?? ???
16
?? ?????
ltlt?????gtgt FlyBehavior
Fly()
FlyWithWings
fly() // ?? ??? ??
FlyNoWay
fly() //???? ?? ?? // ?????!
17
?? ??
ltlt ????? gtgt QuackBehavior
Quack()
Quack
Quack() // ?? ??? ?
Squeak
Quack() // ???? ?? // ??? ?
MuteQuack
Quack() // ???? ?? ?? // ?? ??? ??
18
??
  • ?? ??? ??????? ??? ????? ?? ??? ??
  • ??? ?? ???? ????? ??? ???? Duck???????? ?????
    ?? ?? ??

19
Duck ?? ????
  • Public class Duck
  • QuackBehavior quackBehavior// ????public
    void performQuack() quackBehavior.quack() //
    ?? ??? ??? ?? ???? ?? quackBehavior? ???? ???
    ? ??? ??

Duck
FlyBehavior flyBehavior QuackBehavior quackBehavior
performQuack() //quack()?? Swim() Display() performFly() // fly() ?? // ?? ???? ???
20
Duck ?? ????
  • Public class MallardDuck extends Duck
  • public MallardDuck()
  • quackBehavior new Quack()
  • flyBehavior new FlyWithWings()
  • public void display()
  • system.out.println(?? ??????.)
  • // ??? ??? ???? ?? ? ??.

21
?? ?? ?? ?? 1
Duck
FlyBehavior flyBehavior QuackBehavior quackBehavior
Swim() Display() performQuack() //quack()?? performFly() // fly() ?? // ?? ???? ???
Duck
FlyBehavior flyBehavior QuackBehavior quackBehavior
Swim() Display() performQuack() performFly() setFlyBehavior() setQuackBehavior() // ?? ???? ???
  • Public void setFlyBehavior(FlyBehavior fb)
  • flyBehavior fb
  • Public void setQuackBehavior(QuackBehavior qb)
  • quackBehavior qb

22
?? ?? ?? ?? 2
  • Public class ModelDuck extends Duck
  • public ModelDuck()
  • flyBehavior new FlyNoWay()
  • quackBehavior new Quack()
  • public void display()
  • system.out.println(?? ?? ?????)

23
?? ?? ?? ?? 3
  • Public class FlyRocketPowered implements
    FlyBehavior
  • public void fly()
  • system.out.println(?????? ?????)

24
?? ?? ?? ?? 4
  • Public class MinicuckSimulator
  • public static void main(String args)
  • Duck mallard new MallardDuck()
  • mallard.performQuack()
  • mallard.performFly()
  • Duck model new ModelDuck()
  • model.performFly()
  • model.setFlyBehavior(new FlyRocketPowered())
  • model.performFly()

?! ?? ???!! ?? ? ??? ?????? ?????
25
Diagram
26
Diagram
27
Strategy Pattern
  • ?????? ???? ??? ?????
  • ???? ??? ? ??? ???.
  • ?????? ????
  • ????? ???? ???????
  • ????? ????? ??? ? ??.

28
Observer Pattern
29
Observer Pattern
  • ? ??? ??? ???? ??? ?????? ????? ??? ?????? ???
    ???? ???????(one-to-many)???? ??

30
??? ??
  • ?? ?????? ?? ?????
  • ???? ???? ???? ??? ??

31
Observer Pattern
  • ?) ?? ???? ? ???? ????,
  • ??? ???? ????? ??

32
Observer Pattern
33
Decorator Pattern
34
Decorator Pattern
  • ??? ???? ??? ???? ??
  • ??????
  • ?????? ??? ?? ???
  • ??? ???? ??? ? ?? ?? ??

35
??? ??
  • OCP( Open-Closed Principle )
  • ???? ??? ????
  • ?? ??? ???,
  • ?? ??? ????
  • ?? ??? ??.

36
Decorator Pattern
  • ?) ????? ?? ?? House Blend,
  • Dark Roast, Decaf, Espresso ?? ?? ?? 

37
Decorator Pattern
  • ?? ???? ?????,
  • ???? ??? ???? ??,
  • ?? ?? ???? ?? ????
  • ?? ??? ??? ??? ??.
  • ?? ?? ????? ????,
  • ???? ? ??? ??? ???.

38
Decorator Pattern
  • ?? ??,
  • ?? ??? ???? ?? ???
  • ??? ???????? ????
  • ??? ?? ??? ??.
  • DarkRoast ??? ????
  • Mocha ??? ????.
  • Whip ??? ????.
  • cost() ???? ????.

39
Decorator Pattern
  • ?? ???? ??? ???? ??
  • ?? ????? ????.
  • ??? ??, ?? ???? cost?
  • ????. ??? ???? ??
  • ??? cost? ?? ???? ???
  • ???? ??. ? ?? ????
  • ??? ???, ????? ???
  • ???? ?? ????? ???.

40
Decorator Pattern
  • ????? ??? ????? 

41
Diagram
42
Thank you
Write a Comment
User Comments (0)
About PowerShow.com