设计模式-策略模式
现实生活中,要解决一件事的时候,可能会有很多种解决办法,编程的时候亦是如此。例如,在排序列表的时候,可以使用快速排序、冒泡排序等算法,甚至相同的算法有不同的实现方式。要在程序里为不同规模的列表切换不同的算法或实现,可以采用策略模式。
@startuml
!theme plain
abstract class Strategy {
+ strategy()
}
class StrategyOne {
+ strategy()
}
class StrategyTwo {
+ strategy()
}
class StrategyThree {
+ strategy()
}
class Context {
- Strategy strategy
+ execute()
}
Strategy <|-- StrategyOne
Strategy <|-- StrategyTwo
Strategy <|-- StrategyThree
Context o--> Strategy
@enduml