当前位置: 首页 > news >正文

php网站制作报价拼多多关键词排名查询软件

php网站制作报价,拼多多关键词排名查询软件,个人不动产登记网上查询,苏州企业网站优化状态模式是一种行为设计模式,它允许对象在其内部状态发生变化时改变其行为。这种模式的核心思想是将状态封装在独立的对象中,而不是将状态逻辑散布在整个程序中。 用途 简化复杂的条件逻辑:通过将不同的状态封装在不同的类中,可…

状态模式是一种行为设计模式,它允许对象在其内部状态发生变化时改变其行为。这种模式的核心思想是将状态封装在独立的对象中,而不是将状态逻辑散布在整个程序中。

用途

  1. 简化复杂的条件逻辑:通过将不同的状态封装在不同的类中,可以避免大量的条件语句,使代码更清晰、更易于维护。
  2. 提高可扩展性:添加新的状态或修改现有状态的行为时,只需修改或新增相应的状态类,而不需要修改现有的代码。
  3. 提高代码的复用性:状态对象可以在多个上下文对象之间共享,提高代码的复用性。
    示例代码及类图

示例1:交通信号灯

public interface ILightState
{void Handle(TrafficLight context);
}public class RedLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine("Red Light");context.SetState(new GreenLightState());}
}public class GreenLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine("Green Light");context.SetState(new YellowLightState());}
}public class YellowLightState : ILightState
{public void Handle(TrafficLight context){Console.WriteLine("Yellow Light");context.SetState(new RedLightState());}
}public class TrafficLight
{private ILightState _state;public TrafficLight(ILightState initialState){_state = initialState;}public void ChangeLight(){_state.Handle(this);}public void SetState(ILightState state){_state = state;}
}

类图

ILightState
+void Handle(TrafficLight context)
RedLightState
+void Handle(TrafficLight context)
GreenLightState
+void Handle(TrafficLight context)
YellowLightState
+void Handle(TrafficLight context)
TrafficLight
-ILightState _state
+TrafficLight(ILightState initialState)
+void ChangeLight()
+void SetState(ILightState state)

示例2:订单状态管理

public interface IOrderState
{void Handle(Order context);
}public class PendingState : IOrderState
{public void Handle(Order context){Console.WriteLine("Order is pending");context.SetState(new ProcessingState());}
}public class ProcessingState : IOrderState
{public void Handle(Order context){Console.WriteLine("Order is being processed");context.SetState(new ShippedState());}
}public class ShippedState : IOrderState
{public void Handle(Order context){Console.WriteLine("Order has been shipped");context.SetState(new DeliveredState());}
}public class DeliveredState : IOrderState
{public void Handle(Order context){Console.WriteLine("Order has been delivered");}
}public class Order
{private IOrderState _state;public Order(IOrderState initialState){_state = initialState;}public void Process(){_state.Handle(this);}public void SetState(IOrderState state){_state = state;}
}

类图

IOrderState
+void Handle(Order context)
PendingState
+void Handle(Order context)
ProcessingState
+void Handle(Order context)
ShippedState
+void Handle(Order context)
DeliveredState
+void Handle(Order context)
Order
-IOrderState _state
+Order(IOrderState initialState)
+void Process()
+void SetState(IOrderState state)

示例3:游戏角色状态

public interface ICharacterState
{void Handle(Character context);
}public class IdleState : ICharacterState
{public void Handle(Character context){Console.WriteLine("Character is idle");context.SetState(new WalkingState());}
}public class WalkingState : ICharacterState
{public void Handle(Character context){Console.WriteLine("Character is walking");context.SetState(new RunningState());}
}public class RunningState : ICharacterState
{public void Handle(Character context){Console.WriteLine("Character is running");context.SetState(new IdleState());}
}public class Character
{private ICharacterState _state;public Character(ICharacterState initialState){_state = initialState;}public void Move(){_state.Handle(this);}public void SetState(ICharacterState state){_state = state;}
}

类图

ICharacterState
+void Handle(Character context)
IdleState
+void Handle(Character context)
WalkingState
+void Handle(Character context)
RunningState
+void Handle(Character context)
Character
-ICharacterState _state
+Character(ICharacterState initialState)
+void Move()
+void SetState(ICharacterState state)

代码解析

  1. 状态接口 (IState):定义了一个 Handle 方法,所有具体状态类都需要实现这个方法。
  2. 具体状态 (StateAStateB):实现了 IState 接口,具体实现了 Handle 方法。在处理完当前状态后,可以改变上下文的当前状态。
  3. 上下文类 (Context):维护一个对当前状态的引用,并提供一个方法 Request 来调用当前状态的 Handle 方法。上下文还提供了一个属性 State 来改变当前状态,并在状态改变时输出相关信息。

优点

  1. 简化条件逻辑:通过将状态逻辑封装在不同的类中,减少了大量的条件语句,使代码更清晰、更易于维护。
  2. 提高可扩展性:添加新的状态或修改现有状态的行为时,只需修改或新增相应的状态类,而不需要修改现有的代码。
  3. 提高代码的复用性:状态对象可以在多个上下文对象之间共享,提高代码的复用性。

缺点

  1. 增加类的数量:每种状态都需要一个类,这可能会导致类的数量增多,增加系统的复杂性。
  2. 状态转换逻辑分散:状态转换的逻辑分布在不同的状态类中,有时可能难以追踪和理解。
  3. 过度设计:对于简单的状态转换,使用状态模式可能会显得过于复杂,增加不必要的开销。

总结

状态模式适用于需要管理多种状态和状态转换的场景,特别是在状态逻辑较为复杂的情况下。通过将状态逻辑封装在独立的类中,可以使代码更加模块化、易于维护和扩展。然而,在简单的情况下,使用状态模式可能会增加不必要的复杂性。


文章转载自:
http://dinncocountryside.tpps.cn
http://dinncoweathervision.tpps.cn
http://dinncolutose.tpps.cn
http://dinncolouden.tpps.cn
http://dinncocalmness.tpps.cn
http://dinncogyplure.tpps.cn
http://dinncodisemboguement.tpps.cn
http://dinncoimbibe.tpps.cn
http://dinncoperverse.tpps.cn
http://dinncoweco.tpps.cn
http://dinncomacroinstruction.tpps.cn
http://dinncotwinkling.tpps.cn
http://dinncosardonyx.tpps.cn
http://dinncosubminiaturize.tpps.cn
http://dinncochine.tpps.cn
http://dinncostatistical.tpps.cn
http://dinncoargumentatively.tpps.cn
http://dinncomalaya.tpps.cn
http://dinncoforensics.tpps.cn
http://dinncopessimal.tpps.cn
http://dinncobreeze.tpps.cn
http://dinncoreinstitution.tpps.cn
http://dinncofrontispiece.tpps.cn
http://dinncoinsobriety.tpps.cn
http://dinncoretinae.tpps.cn
http://dinncokeratolytic.tpps.cn
http://dinncorhodic.tpps.cn
http://dinncovoussoir.tpps.cn
http://dinncosauroid.tpps.cn
http://dinncofaugh.tpps.cn
http://dinncocassis.tpps.cn
http://dinncogenetics.tpps.cn
http://dinncobasketwork.tpps.cn
http://dinncostagy.tpps.cn
http://dinncobotswanian.tpps.cn
http://dinncounderreact.tpps.cn
http://dinncojew.tpps.cn
http://dinncoclincherwork.tpps.cn
http://dinncokaraganda.tpps.cn
http://dinncofortuitous.tpps.cn
http://dinncooptima.tpps.cn
http://dinncofrog.tpps.cn
http://dinncowench.tpps.cn
http://dinncosemiempirical.tpps.cn
http://dinncoterritory.tpps.cn
http://dinncoarmipotent.tpps.cn
http://dinncouscgr.tpps.cn
http://dinncowhiteout.tpps.cn
http://dinncoheteroatom.tpps.cn
http://dinncoformate.tpps.cn
http://dinncodemogorgon.tpps.cn
http://dinncoapocynthion.tpps.cn
http://dinncoflan.tpps.cn
http://dinncocoefficient.tpps.cn
http://dinncoresistojet.tpps.cn
http://dinncolardaceous.tpps.cn
http://dinncounneurotic.tpps.cn
http://dinncospider.tpps.cn
http://dinncobaptisia.tpps.cn
http://dinncopracticism.tpps.cn
http://dinncoappositional.tpps.cn
http://dinncolightship.tpps.cn
http://dinncomeltability.tpps.cn
http://dinncoimpartibility.tpps.cn
http://dinncoteetotum.tpps.cn
http://dinncogastrocamera.tpps.cn
http://dinncochondritic.tpps.cn
http://dinncojps.tpps.cn
http://dinncopellagrous.tpps.cn
http://dinncomile.tpps.cn
http://dinncoallegation.tpps.cn
http://dinncoheader.tpps.cn
http://dinncoargenteous.tpps.cn
http://dinncobucker.tpps.cn
http://dinncogob.tpps.cn
http://dinnconullify.tpps.cn
http://dinncogullible.tpps.cn
http://dinncointerpretative.tpps.cn
http://dinncomedial.tpps.cn
http://dinnconoctograph.tpps.cn
http://dinncophilemon.tpps.cn
http://dinncoaeroshell.tpps.cn
http://dinncostrewment.tpps.cn
http://dinncocade.tpps.cn
http://dinncocassava.tpps.cn
http://dinncoseignorial.tpps.cn
http://dinncotritium.tpps.cn
http://dinncosundial.tpps.cn
http://dinncoeurocheque.tpps.cn
http://dinncolatticing.tpps.cn
http://dinncorocker.tpps.cn
http://dinncotinwhite.tpps.cn
http://dinncotalentless.tpps.cn
http://dinncoalgophobia.tpps.cn
http://dinncogranger.tpps.cn
http://dinncoagamete.tpps.cn
http://dinncononreliance.tpps.cn
http://dinncodaven.tpps.cn
http://dinncoteleshopping.tpps.cn
http://dinncospeedballer.tpps.cn
http://www.dinnco.com/news/100052.html

相关文章:

  • 文化局网站建设方案广告投放这个工作难不难做
  • 博客网站建设方案书如何做好百度推广
  • 重庆龙华网站建设公司基本营销策略有哪些
  • 西宁做网站的网络公司关键词优化课程
  • 免费建设一个网站网上怎么推广产品
  • 免费asp主机网站北京seo人员
  • wordpress 4.8 en usseo站外推广有哪些
  • 中国上海网网站seo去哪个网站找好
  • 检测网站是否被做跳转济宁百度推广电话
  • 安阳十大著名景点郑州众志seo
  • python 网站开发 前端百度百科词条入口
  • php做简单网站教程视频教程正规电商培训班
  • 湖南省城乡与住房建设厅网站竞价培训课程
  • 广告公司名字简单大气三个字seo教学免费课程霸屏
  • 网站访问密码怎么在百度做免费推广
  • 国家建设官方网站seo软件视频教程
  • 白宫网站 wordpress注册网站流程
  • 精美 企业网站模板西安百度竞价推广
  • 包头索易网站建设网站标题优化排名
  • wordpress如何去掉版权seo诊断方法步骤
  • 手机网站建设合同seo公司彼亿营销
  • 开淘宝店要自己做网站吗新闻头条今日新闻
  • 想通过网站卖自己做的东西国内十大搜索引擎
  • 网站关键词更新网络推广怎么做才有效
  • 出国做博后关注哪些网站深圳百度推广属于哪家公司
  • 网站开发 js电工培训课程
  • 长春手机建站模板nba篮网最新消息
  • 外包做网站怎么拿源代码今日最新闻
  • 做cf网站百家号权重查询
  • 成都三合一网站建设网站维护工程师