Behavioral Patterns
1. Strategy
flowchart LR
Context --> Strategy
subgraph Dynamic Choose
Strategy --> Strategy1 & Strategy2 & ...
end
1.1. Case 1: TripMode
flowchart BT
ByBus & ByTrain & ByPlane --> TripMode
flowchart LR
p[enum TYPE]
ch1[BY_BUS]
ch2[BY_TRAIN]
ch3[BY_PLANE]
p --> ch1 & ch2 & ch3
classDiagram
TripMode <|-- ByBus
TripMode <|-- ByTrain
TripMode <|-- ByPlane
class TripMode {
+ virtual TYPE getType();
+ virtual void travel();
}
class ByBus {
+ virtual TYPE getType();
+ virtual void travel();
}
class ByTrain {
+ virtual TYPE getType();
+ virtual void travel();
}
class ByPlane {
+ virtual TYPE getType();
+ virtual void travel();
}
flowchart LR
p[static unordered_map s_trip_modes_]
ch1[BY_BUS, ByBus*]
ch2[BY_TRAIN, ByTrain*]
ch3[BY_PLANE, ByPlane*]
p --> ch1 & ch2 & ch3
1.2. Case 2: MathOperation
flowchart BT
Add & Subtract & Multiply & Devide --> MathOperation
1.3. Case 3: ParseFile
flowchart BT
ParseDxf & ParseJson & ParseStep --> ParseFile
2. Chain of Responsibility
flowchart LR
Root[A Request]
h1[Handle1]
h2[Handle2]
h3[...]
h4[Handle4]
Root --> h1 --> h2 --> h3 --> h4
flowchart BT
h1[Handle1::deal]
h2[Handle2::deal]
h3[...::deal]
h4[Handle4::deal]
h[Handle::deal]
h1 & h2 & h3 & h4 --> h
classDiagram
class Handle {
-Handle* next_handle_;
+void filter(const Request&, Response&);
+void setNextHandle(Handle*);
+Handle* getNextHandle();
+virtual deal(const Request&, Response&);
}
void filter(const Request& req, Response& res) {
deal(req, res);
#ifdef Stop passing down
if(res == FINISHED)
return;
#endif
auto next_handle = getNextHandle();
if(next_handle)
next_handle->filter(req, res);
#endif
}
flowchart
subgraph HandleChain
Handle1 --setNextHandle--> Handle2 --setNextHandle--> ... --setNextHandle--> Handle4
end
Req[A Request] --> HandleChain
HandleChain .-> Res[Response]
2.1. Case 1: Logger
flowchart
subgraph LoggerChain
ErrorLogger --gotoNext-->
WarnLogger --gotoNext-->
InfoLogger
ErrorLogger --Log ?--> ErrorLogger
WarnLogger --Log ?--> WarnLogger
InfoLogger --Log ?--> InfoLogger
end
Req[A Log] --> LoggerChain
LoggerChain .-> Res[Response]
2.2. Case 2: PassNotes
flowchart
subgraph PassNotesChain
Girl1 --passToTheNext--> Girl2 --passToTheNext--> Girl3
Girl1 --Be my girlfriend ?--> Girl1
Girl2 --Be my girlfriend ?--> Girl2
Girl3 --Be my girlfriend ?--> Girl3
end
Req[A Note] --> PassingNotesChain
PassingNotesChain .-> Res[Response]
3. Template
classDiagram
Template <|-- A
Template <|-- B
Template <|-- C
class Template {
+void commonMethod();
+virtual void uniqueMethod();
}
class A {
+virtual void uniqueMethod();
}
class B {
+virtual void uniqueMethod();
}
class C {
+virtual void uniqueMethod();
}
3.1. Case 1: Charge Device
flowchart LR
P[class ChargeDevice]
M1[-void powerOn]
M2[-void powerOff]
M3[-virtual void charge]
M4[+run]
M4_Ex(powerOn, charge, powerOff)
M1 & M2 & M3 --- P --- M4 --- M4_Ex
flowchart LR
ChargeDevice --- ChargePhone & ChargeComputer & ChargeEV --- charge
charge ---> chargePhone & chargeComputer & chargeEV
3.2. Case 2: Play Game
classDiagram
PlayGame <|-- PlayGameA
PlayGame <|-- PlayGameB
class PlayGame {
+void play();
-virtual void initGame();
-virtual void startGame();
-virtual void endGame();
}
class PlayGameA{
-void initGame();
-void startGame();
-void endGame();
}
class PlayGameB{
-void initGame();
-void startGame();
-void endGame();
}
4. State
flowchart BT
2[External Factors] --> Context
Context --reply--> 1[Inner State Changed]
State1 --> 3[Inner State]
State2 --> 3[Inner State]
State3 --> 3[Inner State]
4.1. LightSwitch
flowchart LR
Light --switch--> LightState
LightState --- LightOn & LightOff