Title: Design Patterns
1Design Patterns ??????? ????????? 20040074 ???
2(No Transcript)
3Contents.
- Think about
- Strategy Pattern
- Observer Pattern
- Decorator Pattern
4Think about
5?? ?? ?????
- ????, ????, ?? ?? ?
- ?? ???? ? ????,
- ???, ?? ???,
- ? ?? ???, ? ?? ???,
- ?? ??? ?? ??
- ?? ?? ????? ??
6Simple 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 )? ?????,
- ? ???? ?? ?????
- ??? ? ??? ??.
12Diagram
Duck
Swim() Display() //?? ???????
Flyable
Fly()
Quackable
Quack()
MallardDuck
Display() Fly() Quack()
RedheadDuck
Display() Fly() Quack()
RubberDuck
Display() Quack()
DecoyDuck
Display()
13Strategy 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???????? ?????
?? ?? ??
19Duck ?? ????
- Public class Duck
- QuackBehavior quackBehavior// ????public
void performQuack() quackBehavior.quack() //
?? ??? ??? ?? ???? ?? quackBehavior? ???? ???
? ??? ??
Duck
FlyBehavior flyBehavior QuackBehavior quackBehavior
performQuack() //quack()?? Swim() Display() performFly() // fly() ?? // ?? ???? ???
20Duck ?? ????
- 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()
-
?! ?? ???!! ?? ? ??? ?????? ?????
25Diagram
26Diagram
27Strategy Pattern
- ?????? ???? ??? ?????
- ???? ??? ? ??? ???.
- ?????? ????
- ????? ???? ???????
- ????? ????? ??? ? ??.
28Observer Pattern
29Observer Pattern
- ? ??? ??? ???? ??? ?????? ????? ??? ?????? ???
???? ???????(one-to-many)???? ??
30??? ??
- ?? ?????? ?? ?????
- ???? ???? ???? ??? ??
31Observer Pattern
- ?) ?? ???? ? ???? ????,
- ??? ???? ????? ??
32Observer Pattern
33Decorator Pattern
34Decorator Pattern
- ??? ???? ??? ???? ??
- ??????
- ?????? ??? ?? ???
- ??? ???? ??? ? ?? ?? ??
35??? ??
- OCP( Open-Closed Principle )
- ???? ??? ????
- ?? ??? ???,
- ?? ??? ????
- ?? ??? ??.
36Decorator Pattern
- ?) ????? ?? ?? House Blend,
- Dark Roast, Decaf, Espresso ?? ?? ??
37Decorator Pattern
- ?? ???? ?????,
- ???? ??? ???? ??,
- ?? ?? ???? ?? ????
- ?? ??? ??? ??? ??.
- ?? ?? ????? ????,
- ???? ? ??? ??? ???.
38Decorator Pattern
- ?? ??,
- ?? ??? ???? ?? ???
- ??? ???????? ????
- ??? ?? ??? ??.
- DarkRoast ??? ????
- Mocha ??? ????.
- Whip ??? ????.
- cost() ???? ????.
39Decorator Pattern
- ?? ???? ??? ???? ??
- ?? ????? ????.
- ??? ??, ?? ???? cost?
- ????. ??? ???? ??
- ??? cost? ?? ???? ???
- ???? ??. ? ?? ????
- ??? ???, ????? ???
- ???? ?? ????? ???.
40Decorator Pattern
41Diagram
42Thank you