设计模式24:State Pattern (状态模式)

状态模式(State Pattern)是一种行为设计模式,它让一个对象在其内部状态改变时改变它的行为。该模式通过将行为封装在独立的对象中来实现这一目的,这些对象被称为状态对象。本文通过代码实例介绍了如何在C#中使用状态模式,展示了如何让对象根据内部状态改变其行为。
摘要由CSDN通过智能技术生成

英文原文:http://www.dofactory.com/Patterns/PatternState.aspx

一、State Pattern (状态模式)

Define: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

定义:当一个对象的内部状态进行改变的时候允许对象改变其行为。这个对象将显然可以改变其类。

二、UML类图

state

  • Context  (Account)
    • defines the interface of interest to clients
    • maintains an instance of a ConcreteState subclass that defines the current state.
  • State  (State)
    • defines an interface for encapsulating the behavior associated with a particular state of the Context.
  • Concrete State  (RedState, SilverState, GoldState)
    • each subclass implements a behavior associated with a state of Context

    三、State Pattern (状态模式)实例性代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值