`

State与Strategy

阅读更多

 

State模式定义:允许对象在内部状态改变时改变自己的行为,就像改变了对象的类。

Strategy模式定义:定义了算法簇,分别封装起来,让他们之间可以互相替换,此模式让算法的的变化独立于使用算法的客户。

 

这2个模式可以说从命名上就能看出其专注的问题点!

 

状态模式专注于对象的状态,对状态进行了行为上的封装,因此,不同的状态,所执行的操作时完全不同的,当有新的状态出现时,我们不得不增加类来满足业务的需要,而状态的改变对操作者是透明的,操作者不需要关注当前状态会执行什么,因为状态告诉操作者:我是足够聪明的,相信我!

 

策略模式不会去关注对象的状态,而主要考虑我需要什么样的操作,这些操作有哪些的共通的,那么我就把这些共通的行为进行对象化,产生行为类,自己去管理这些行为类,需要做什么样的事情,就持有该行为类,执行该类的共通方法。

 

状态模式由状态类管理状态的改变,而策略模式由用户来决定使用什么策略!

 

个人浅显的观点,不足的地方,请大家多指教,一起讨论

分享到:
评论

相关推荐

    A Pseudospectral Strategy for LCM of Automated Vehicles on Highway

    This paper presents a pseudospectral strategy for lane change maneuvers of automated vehicles on the highway. This strategy can optimize the longitudinal and lateral control simultaneously to account ...

    IT strategy

    HIT strategy About North Caroline State in US

    Near-Optimal Resilient Control Strategy Design for State-Saturated Networked Systems Under Stochastic Communication Protocol

    Near-Optimal Resilient Control Strategy Design for State-Saturated Networked Systems Under Stochastic Communication Protocol

    mule基本节点解释

    mule基本节点解释汇总 .... ... <default-service-exception-strategy> ... </default-service-exception-... <service name="GreeterUMO2" initialState="stopped"> ... </mule>

    math modelling

    In the absence of a study, the station believes the new advertising strategy has a 65% chance of success and a 35% chance of failure at the state level. If successful, the new advertising strategy ...

    Programming.in.the.Large.with.Design.Patterns

    Singleton, Iterator, Adapter, Decorator, State, Strategy, Factory Method, Observer, Facade and Template Method. Each pattern is introduced with a non-technical example or story that illustrates the ...

    设计模式C++代码示例-含VC工程

    Strategy Encapsulates an algorithm inside a class Template Method Defer the exact steps of an algorithm to a subclass Visitor Defines a new operation to a class without change

    Head First Of Design高清中文版 1/3

    如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、Interpreter、Memento、Chain of Responsibility。 结构型模式可以让你把类或对象组合到更大的结构中。如:Decorator、...

    Head First Of Design高清中文版 2/3

    如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、Interpreter、Memento、Chain of Responsibility。 结构型模式可以让你把类或对象组合到更大的结构中。如:Decorator、...

    Head First Of Design中文高清版 3/3

    如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、Interpreter、Memento、Chain of Responsibility。 结构型模式可以让你把类或对象组合到更大的结构中。如:Decorator、...

    36种最新设计模式整理

    Strategy 模式 Template Method 模式 Visitor 模式 Guarded Suspension 模式 Producer Consumer 模式 Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase ...

    [开发平台] 平台生态系统 (英文版)

    You will also learn to apply state-of-the-art research in software engineering, strategy, and evolutionary biology to leverage ecosystem dynamics unique to platforms. ☆ 出版信息:☆ [作者信息] ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    ■Chapter 9: Behavioral Patterns: State, Strategy, Template Method, and Visitor ■Chapter 10: Patterns of Enterprise Application Architecture: Repository, Unit of Work, Lazy Load, and Service Layer ...

    simple-immuter:Simple Immuter库的开发

    :rocket: 上 创建人:Immer ... 这意味着您可以通过修改数据来与数据进行交互,从而使数据的所有优点保持不变,即nextState是currentState的深层且不变的副本。 使用例 结果 BaseState不会更改,

    Design Patterns Elements of Reusable Object-Oriented Software

    • Strategy • Template Method • Visitor • Discussion of Behavioral Patterns Conclusion • What to Expect from Design Patterns • A Brief History • The Pattern Community • An Invitation ...

    A strategy for the assessment of bilingual handicapped children

    State University of New York at Albany Because of current federal legislation, educators must assess children with possible handicapping conditions and plan an individual education program for each...

    strategy-game-htn

    def build_town ( state ): print state return [( 'build_barracks_task' ,),( 'build_watchtower_task' ,)] 领域 有两种类型的资源: 金子 木头 有3种类型的建筑: 军营 岗楼 屋 有工人(苦工),他们需要...

    软件设计模式—行为型模式

    软件设计模式,行为型模式:ChainOf Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template method Visitor

    Sampled-data vehicular platoon control with communication delay

    communication strategy, considering communication delay, a platoon control law is proposed based on sampled state information, which allows the weights of state errors to vary along the platoon. ...

    设计模式源代码

    AbstractFactory Adapter Brige Builder ChainOfResponsibility Command Composite Decorator Facade Factory FlyWeight Interpreter Iterator Mediator ...State Strategy TemplateMethod Visitor

Global site tag (gtag.js) - Google Analytics