推荐阅读:

前言

       在开发游戏过程中,当玩家合成一种道具的时候,对于不痛的道具,需要的碎片个数,类型是不同的。用传统的写法,就是使用if...else...语句来判断。如果后面,策划修改了道具合成机制,我们就需要更改if结构判断了,这就违背了设计模式原则中的对扩展的开发,对修改的关闭,为此,我们引入责任链模式。

介绍

       责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

要素

       1.抽象处理者(Handler):定义出一个处理请求的接口。如果需要,接口可以定义 出一个方法以设定和返回对下家的引用。

       2.具体处理者(ConcreteHandler):具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。

       3.请求类(Request):处理者需要处理的请求信息;

实现

       这里我们还是用上面的例子,使用责任链模式来实现奖品的分发机制。

1.创建请求类

	//请求类,请求合成道具
public class SyntheticRequest
{
/// 当前拥有的碎片数量
public int DebrisNum{ get; set; } public SyntheticRequest(int num)
{
this.DebrisNum= num;
}
}

2.创建抽象角色类

	//抽象角色类,可以通过合成得到的道具
public abstract class Prop
{
//下一级道具,更低一级的道具
public Prop NextProp{ get; set; }
//当前道具类型
public string PropType{ get; set; }
//构造函数
public Prop(string type)
{ this.PropType= type; } /// 该角色的执行行为
public abstract void Behaviour(SyntheticRequest request);
}

3.创建具体角色类

    ///高级道具
public class Prop1:Prop
{
public Prop1(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 1000)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
}
///中级道具
public class Prop2:Prop
{
public Prop2(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 500)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
}
///低级道具
public class Prop3:Prop
{
public Prop3(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 10)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
}

4.使用责任链模式

	//使用责任链模式
class Program
{
static void Main(string[] args)
{
//申请合成道具
SyntheticRequest request= new SyntheticRequest(66); //对该活动的审批可能涉及的角色
Prop prop1= new Prop1("高级道具");
Prop prop2= new Prop2("中级道具");
Prop prop3= new Prop3("低级道具"); //设置责任链
prop1.NextProp = prop2;
prop2.NextProp = prop3; //合成处理
prop1.Behaviour(request);
}
}

整合代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 责任链模式
{
//请求类,请求合成道具
public class SyntheticRequest
{
/// 当前拥有的碎片数量
public int DebrisNum{ get; set; } public SyntheticRequest(int num)
{
this.DebrisNum= num;
}
} //抽象角色类,可以通过合成得到的道具
public abstract class Prop
{
//下一级道具,更低一级的道具
public Prop NextProp{ get; set; }
//当前道具类型
public string PropType{ get; set; }
//构造函数
public Prop(string type)
{ this.PropType= type; } /// 该角色的执行行为
public abstract void Behaviour(SyntheticRequest request);
} ///高级道具
public class Prop1:Prop
{
public Prop1(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 1000)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
}
///中级道具
public class Prop2:Prop
{
public Prop2(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 500)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
}
///低级道具
public class Prop3:Prop
{
public Prop3(string type) : base(type) { } public override void Behaviour(SyntheticRequest request)
{
if (request.DebrisNum>= 10)
{
Console.WriteLine("获得{0},消耗{1}碎片", this.PropType,request.DebrisNum);
}
else if (NextProp != null)
{
Console.WriteLine("{0}个碎片不够,只能合成更低一级的道具,即{1}", request.DebrisNum, NextProp.PropType);
NextProp.Behaviour(request);
}
}
} //使用责任链模式
class Program
{
static void Main(string[] args)
{
//申请合成道具
SyntheticRequest request= new SyntheticRequest(66); //对该活动的审批可能涉及的角色
Prop prop1= new Prop1("高级道具");
Prop prop2= new Prop2("中级道具");
Prop prop3= new Prop3("低级道具"); //设置责任链
prop1.NextProp = prop2;
prop2.NextProp = prop3; //合成处理
prop1.Behaviour(request);
}
}
}

优缺点

优点:

       降低了请求的发送者和接收者之间的耦合;把多个条件判定分散到各个处理类中,使得代码更加清晰,责任更加明确。

缺点:

       在找到正确的处理对象之前,所有的条件判定都要执行一遍,当责任链过长时,可能会引起性能的问题;可能导致某个请求不被处理。

总结

       代码中存在多个if-else语句的情况下,此时可以考虑使用责任链模式来对代码进行重构。

注意

       这里举的例子,在实际开发过程中可能不会用到设计模式,希望各位读者切勿固步自封。

设计模式(C#)——12责任链模式的更多相关文章

  1. Python使用设计模式中的责任链模式与迭代器模式的示例

    Python使用设计模式中的责任链模式与迭代器模式的示例 这篇文章主要介绍了Python使用设计模式中的责任链模式与迭代器模式的示例,责任链模式与迭代器模式都可以被看作为行为型的设计模式,需要的朋友可 ...

  2. Java设计模式学习记录-责任链模式

    前言 已经把五个创建型设计模式和七个结构型设计模式介绍完了,从这篇开始要介绍行为型设计模式了,第一个要介绍的行为型设计模式就是责任链模式(又称职责链模式). 责任链模式 概念介绍 责任链模式是为了避免 ...

  3. 《java设计模式》之责任链模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...

  4. 《JAVA设计模式》之责任链模式(Chain of Responsibility)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...

  5. 重学 Java 设计模式:实战责任链模式「模拟618电商大促期间,项目上线流程多级负责人审批场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 场地和场景的重要性 射击

  6. [设计模式] javascript 之 责任链模式

    责任链模式:定义 责任链接模式又称职责链模式,是一种对象的行为模式:它是一种链式结构,每个节点都有可能两种操作,要么处理该请求停止该请求操作,要么把请求转发到下一个节点,让下一个节点来处理请求:该模式 ...

  7. Java设计模式系列之责任链模式

    责任链模式 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道 ...

  8. 设计模式学习之责任链模式(Chain of Responsibility,行为型模式)(22)

    参考:http://www.cnblogs.com/zhili/p/ChainOfResponsibity.html 一.引言 在现实生活中,有很多请求并不是一个人说了就算的,例如面试时的工资,低于1 ...

  9. Java描述设计模式(15):责任链模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景描述 1.请假审批流程 公司常见的请假审批流程:请假天数 当 day<=3 天,项目经理审批 当 3<day<= ...

随机推荐

  1. Quartus ii调试技巧_01

    前几天李主任跟我分享了一些特别好用的调试技巧: 1)System Sources and Probes Editor---类似于人为设置触发条件,创建虚拟按键等功能,这段时间一直在做一个电机的驱动,板 ...

  2. Linux vim环境设置

    //vim /etc/vimrc(管理员权限) 1. 显示行号: set number 或者  set nu 不显示行号: set nonu 2.自动缩进: set autoindent 3.C语言自 ...

  3. [学习笔记] MySQL入门

    一.MySQL的安装与简单使用 ubuntu16.04下安装MySQL: sudo apt-get update sudo apt-get install mysql-server mysql-cli ...

  4. .NET为何物?

    .NET是 Microsoft XML Web services 平台.XML Web services 允许应用程序通过 Internet 进行通讯和共享数据,而不管所采用的是哪种操作系统.设备或编 ...

  5. 一文掌握 Spring Boot Profiles

    Spring Boot Profiles 简介 Profile 的概念其实很早在 Spring Framework 就有了,在 Spring Framework 3.1 版本引入了注解 @Profil ...

  6. Python3发送邮件功能

    Python3实现邮件发送功能 import smtplib from email.mime.text import MIMEText # 导入模块 class SendEmail: def send ...

  7. Linux内容点(部分)

    文件属性 -w      文件或目录,对目前(有效的)用户或组来说是可写的       -x       文件或目录,对目前(有效的)用户或组来说是可执行的       -o       文件或目录, ...

  8. Netty源码解析---服务端启动

    Netty源码解析---服务端启动 一个简单的服务端代码: public class SimpleServer { public static void main(String[] args) { N ...

  9. restapi(3)- MongoDBEngine : MongoDB Scala编程工具库

    最近刚好有同事在学习MongoDB,我们讨论过MongoDB应该置于服务器端然后通过web-service为客户端提供数据的上传下载服务.我们可以用上节讨论的respapi框架来实现针对MongoDB ...

  10. Unity经典游戏编程之:球球大作战

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...