8/9/2019 UML Chuong8 Mauthietke
1/38
Chn 8: Mu thit k attern
UML/NN 1
8/9/2019 UML Chuong8 Mauthietke
2/38
Phn mm tt?Theo kin trc
Comodity (Tin nghi) Firmness Tnh n nh Delight (Tnh m quan)
UML/NN 2
8/9/2019 UML Chuong8 Mauthietke
3/38
Kh khn ca thit kphn mm phc tp
nh bt qui tc Phn mm c ththa i v lun lun thai
UML/NN 3
8/9/2019 UML Chuong8 Mauthietke
4/38
Nguyn l thit k lp Nguyn l ng m: m vi pht trin thm
Nguyn l thay thLiskov: Cc lp c s c th
Nguyn l nghch
o ph
thu
c: Ph thuc
trc phn tng qu nhiu s bt li)
Principle): Nn tch giao din (vd c chc nngchung) dng chung thnh nhiu c th
UML/NN 4
8/9/2019 UML Chuong8 Mauthietke
5/38
Mu thit k(Design Pattern) Ngi thit ktt c kh nng nhn ra nhng mu
quyt vn , , ,
thuc tnh ca thit km c thiu chnh choh mu thit k ii u t cc vn
5 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
6/38
Pattern Tn mu (Pattern name) - describes the essence of the
Mc ch (Intent) - describes the pattern and what itdoes
Tn khc (Also-known-as) - lists any synonyms for the
pattern -
problem
Kh nn v n d n A licabilit - notes s ecificdesign situations in which the pattern is applicable
6 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
7/38
Pattern Cu trc (Structure) - describes the classes that are
re uired to im lement the attern
Lp tham gia (Participants) - describes theresponsibilities of the classes that are required to implement
Cng tc (Collaborations) - describes how the
participants collaborate to carry out their responsibilities H qu Consequences - escri es t e esign orces
that affect the pattern and the potential trade-offs that mustbe considered when the pattern is implemented (t cho
n c c a c n Mu lin quan (Related patterns) - cross-references
related desi n atterns
7 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
8/38
Cc mu thit k Creational Pattern (Mu kin to):
Abstractional Factory Method Pattern Builder pattern
Factory Method pattern
Prototype Pattern Singleton Pattern
Structure Pattern: c mu Com ositeAdaptor)
8 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
9/38
Mu thit kSingleton Cho php c mt thhin ca lp. Vd: Find
a og, n ey, m ng o n cc ugikt ni CSDL
UML/NN 9
8/9/2019 UML Chuong8 Mauthietke
10/38
Singleton (kin to)
Singleton
static Instance
singletonData
return uniqueinstance
SingletonOp()
GetSingletonData()
CS 406: DesignPatterns10
8/9/2019 UML Chuong8 Mauthietke
11/38
Singleton CodeUsing system;
{
Protect Singlestone() {}
public static SingleStone Instance(){
if (instance==null)
instance = new SingleStone ();return SingleStone;
}UML/NN 11
8/9/2019 UML Chuong8 Mauthietke
12/38
ADAPTER (structure) Ti sdng cc lp nhng giao din (interface)
ng ng c
C nhng vn khng tng thch tim tng
4/16/2010 F21ASMF12
8/9/2019 UML Chuong8 Mauthietke
13/38
Adapter
C 2 mu:
M u cu trc Class Mu Cu trc Object
4/16/2010 F21ASMF13
8/9/2019 UML Chuong8 Mauthietke
14/38
Adapter1
ClientTarget
Adaptee
reques spec c equest
Adapter
a tha k
4/16/2010 F21ASMF14
8/9/2019 UML Chuong8 Mauthietke
15/38
Adapter2
Client Target
reques
Adapter Adaptee
request() specificRequest()
4/16/2010 F21ASMF15
8/9/2019 UML Chuong8 Mauthietke
16/38
Adapter example
....//this method expects a WeeklyEmployee object
pu c s a c vo pr n ay ee y mp oyee e
{
System.out.println(e.getName()
+ is paid + e.getPay() ;
}
PayProcessor WeeklyEmployee
4/16/2010 F21ASMF16
printPay(WeeklyEmp
loyee e)
getName()getPay()
8/9/2019 UML Chuong8 Mauthietke
17/38
Example
PayProcessor
printPay(WeeklyEmpclient
ada tee loyee e)
?
getEmployeeName()getMonthlySalary()
getName()getPay()
target
4/16/2010 F21ASMF17
8/9/2019 UML Chuong8 Mauthietke
18/38
Biu lpClient - unaltered
Target
Pay
ProcessorprintPay
WeeklyEmployeegetPay()
-unaltered.
ee yEmployeee)
ge ame
MonthlyAdapter MonthlyEmployeeMonthlyEmployee : megetPay()getName()
getMonthlyPay()getEmployeeName()
Adapteeunaltered
Adaptor
4/16/2010 F21ASMF18
Provides own implementation of
targets methods which calladaptees methods
8/9/2019 UML Chuong8 Mauthietke
19/38
MonthlyAdapter
public class MonthlyAdapter
extends WeeklyEmployee {
private MonthlyEmployee me; //composed with adaptee
created with adaptee object
public MonthlyAdapter(MonthlyEmployee memp)
{ me = memp; }
//own implementation of target method,
//adapting to use adaptees instead
{ return me.getEmployeeName(); }
public int getPay()
4/16/2010 F21ASMF19
.
}
8/9/2019 UML Chuong8 Mauthietke
20/38
Test
class Test {
ublic static void main(Strin ar [])
{
WeeklyEmployee we
= new Weekl Em lo ee tim 150
PayProcessor.printPay(we);
=
new MonthlyEmployee(jane, 200);
MonthlyAdapter ma = new MonthlyAdapter (me);
.
}
}
4/16/2010 F21ASMF20
8/9/2019 UML Chuong8 Mauthietke
21/38
Mu thit kObserver (hnh vi)
Bi ton thc t: Mt i tng thi gian m g c n n u n c
nhng h s, ng h kim Ta c th D dng thm, bt nhiu i tng th hin
Khi tay i i tng thi gian th tt c cc thhi n c a th i gian u thay i theo
21 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
22/38
Observer
Intent: Ph thuc mt nhiu khi mt i tng bin
bin i tng
tng c lin quan
Khi c 2 hng mt ci ph thuc vo mt ci khc Khi m t bin i ti m t i t n i hi bin i nhn
i tng khc m khng bit bao nhiu i tng cnbin i
22 CNPM/NN
m ng p c n ng ng o
tng khc m khng cn bit r chng
8/9/2019 UML Chuong8 Mauthietke
23/38
M hnh Observer
A
Relative Percentages
A B C D
B
CY 10 40 30 20
Z 10 40 30 20A DCB
A=10%B=40%
C=30% Application data
CS 406: DesignPatterns23
D=20%Requests, modifications
8/9/2019 UML Chuong8 Mauthietke
24/38
Observer-- Structure
Subject Observer
*
Attach(Observer) Update()
e ac server
Notify()
ConcreteSubject ConcreteObserver
observerState
SetState()
GetState() Update()
24 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
25/38
Observer Sequence Diagram
aConcreteSubject:
ConcreteSubject
aConcreteObserver:
ConcreteObserver
anotherConcreteObserver:
ConcreteObserver
SetState()
Update()
GetState()
Update()
GetState()
25 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
26/38
Observer-- Example
Subject Observer
*
Attach(Observer) Update()
e ac server
Notify()
ClockTimerDigitalClock AnalogClock
Update()ClockTimer *sub;
Update()
ClockTimer *sub;
Draw() Draw()
26 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
27/38
Observer Example Sample Code
class Subject;
public:
virtual ~Observer();virtual void Update(Subject * theChngedSub) = 0;
pro ec e :Observer();
}
class Subject {public:
virtual ~Subject();virtual void Attach Observer *
virtual void Detach(Observer *);virtual void Notify();
protected:* *
27 CNPM/NN
_};
8/9/2019 UML Chuong8 Mauthietke
28/38
Observer Example
void Subject :: Attach(Observer* o) {--_
}
void Sub ect :: Detach Observer* o_observers-->Remove(o);
}
void Subject :: Notify() {ListIterator i (_observers);for (i.First(); !i.IsDone(); i.Next()) {
. urren em - p a e s ;}
}
28 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
29/38
Observer Example
class ClockTimer : public Subject {public:
virtual int GetHour();virtual int GetMinute();v r ua n e econ ;void Tick();
}
void ClockTimer :: Tick() {// update internal time-keeping state//Notify();
}
29 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
30/38
Observer Exampleclass DigitalClock : public Widget, public Observer {
public:*
virtual ~DigitalClock();
virtual void Update(Subject *);v r ua vo raw ;
private:ClockTimer* _subject;
}
DigitalClock :: DigitalClock(ClockTimer *s) {_subject = s;_sub ect->Attach this
}DigitalClock :: ~DigitalClock(ClockTimer *s) {_subject->Detach(this);
30 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
31/38
Observer Example
void DigitalClock :: Update(Subject *theChngedSub) {if (theChngedSub == _subject){
}}void DigitalClock :: Draw() {
ge e new va ues rom e su ecint hour = _subject->GetHour();int minute = _subject->GetMinute();
// //draw the digital clock
}
// main
ClockTimer *timer = new ClockTimer;AnalogClock * analogClock = new AnalogClock(timer);DigitalClock * digitalClock = new DigitalClcok(timer);
31 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
32/38
Observer C# (tham khao file project)
using System;
using System.Collections;
{
private ArrayList _observer;
public Subject()
_observer = new ArrayList();
}
public void Attach(Observer o)
{
_observer.Add(o);
}
public void Detach(Observer o)
{
_observer.Add(o);
public void Notify(){
for (int i = 0; i < _observer.Count; i++)
{
((Observer)_observer[i]).Update(this);
}
}
}
UML/NN 32
8/9/2019 UML Chuong8 Mauthietke
33/38
Mu thit kComposite (kin trc)
Bi ton thc t: Mt my c cc chi titm y, c c c m y c g m c c cmy n gin (chi tit my n) v c thcn ng c m y c m y p c m
gm c nhng chi tit my n v cc chim y p c c Gii quyt bi ton tnh gi tr ca cc chi tit:
g r m y ng g r c a c c c m y nv gi tr ca chi tit my phc (bng tt c gi
33 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
34/38
Composite
Intent (mc ch): Compose objects into tree structures tore resent art-whole hierarchies. Let clients treat individualobjects and compositions of objects uniformly
Motivation (sthc y): components can be grouped to form
aPicture
aPicture aLIne aRectangle
aText aLine aRectangle
34 CNPM/NN
8/9/2019 UML Chuong8 Mauthietke
35/38
V d 1Graphic *
Draw()
Add(Graphic)Remove(Graphic)
TextLine Rectangle
Picture
Draw()Draw() Draw()
Draw()
Add(Graphic)
Remove(Graphic)
35 CNPM/NN
GetChild(int)
8/9/2019 UML Chuong8 Mauthietke
36/38
V d 2Component *
Operation()
Add(Component)Remove(Component)
LeafComposite
Operation()
Operation()
Add(Component)Remove(Component)
36 CNPM/NN
GetChild(int)
8/9/2019 UML Chuong8 Mauthietke
37/38
8/9/2019 UML Chuong8 Mauthietke
38/38
V d khcEquipment *
FloppyDisk CompositeEquipmentCard
Chasis BusCabinet
38 CNPM/NN