设计模式-策略模式

现实生活中,要解决一件事的时候,可能会有很多种解决办法,编程的时候亦是如此。例如,在排序列表的时候,可以使用快速排序、冒泡排序等算法,甚至相同的算法有不同的实现方式。要在程序里为不同规模的列表切换不同的算法或实现,可以采用策略模式。

@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

Date: 2024-03-11 Mon 19:39

Author: xueshumeng

Email: xue.shumeng@yahoo.com

Created: 2025-07-05 Sat 15:09