责任链模式
经理和高管可以响应采购请求或将其移交给上级。
每个位置都可以有自己的一套规则,他们可以批准这些订单。
- 首先有一个领导类专门负责处理这些请求
- 之后根据权限分别对子类进行处理
- 在调用的时候会根据处理类的权限进行处理,如果权限不足就传递给下一级别
public class ChainOfResponsibility:MonoBehaviour{
void Start{
Approver llw = new Director();
Approver al = new ViceScident();
Approver mcl = new Prescident();
llw.SetSuccessor(al);
al.SetSuccessor(mcl);
Purchase purchase = new Purchase(900.00,90,"Assets");
llw.ProcessRequest(purchase);
purchase = new Purchase(4001.21,29,"");
llw.ProcessRequest(purchase);
purchase = new Purchase(100021,890,"");
}
/// 负责处理请求的基类(领导)
public class Approver{
protected Approver successor; // 上级
public void SetSuccessor(Approver successor){
this.successor = successor;
}
public abstract void ProcessRequest(Purchase purchase);
}
public class Purchase{
private float m_amount; //金额
private int m_number; //采购数量
private string m_purpose; // 目的
// 赋值
public Purchase(float m_amount,int m_number,string m_purpose){
this.m_purpose = m_purpose;
this.m_amount = m_amount;
this.m_number = m_number;
}
public float Amount
{
get{return m_amount;}
set{m_amount = value;}
}
public int Number
{
get{return m_number;}
set{m_number = value;}
}
public string Purpose
{
get{return m_purpose;}
set{m_purpose = value;}
}
}
public class Director:Approver{
if (purchase.Amount < 10000.0)
{
Debug.Log(this.GetType().Name+" approved request# "+purchase.Number);
}
else if (successor != null)
{
successor.ProcessRequest(purchase);
}
}
public calss Prescident:Approver{
if (purchase.Amount < 100000.0)
{
Debug.Log(this.GetType().Name+" approved request# "+purchase.Number);
}
else
{
Debug.Log("Request# "+purchase.Number+ "requires an executive meeting!");
}
}
public class VivePrescident:Approver{
public override void ProcessRequest(Purchase purchase){
if (purchase.Amount < 50000.0)
{
Debug.Log(this.GetType().Name+" approved request# "+purchase.Number);
}
else if (successor != null)
{
successor.ProcessRequest(purchase);
}
}
}
}
根据提供的计算类型进行计算
public class ChainOfResponsibility2 : MonoBehaviour{
void Start(){
IChainOfCalc calc1 = new AddNumbers();
IChainOfCalc calc2 = new SubstractNumbers();
IChainOfCalc calc3 = new DivideNumbers();
IChainOfCalc calc4 = new MultiplyNumbers();
calc1.SetNextChain(calc2);
calc2.SetNextChain(calc3);
calc3.SetNextChain(calc4);
Number myNumbers = new Number(3, 5, CalculationType.Add);
calc1.Calculate(myNumbers);
// another example:
Number myOtherNumbers = new Number(6, 2, CalculationType.Multiply);
calc1.Calculate(myOtherNumbers);
// or pass it to some chain object inbetween which will not work in this case:
Number myLastNumbers = new Number(12, 3, CalculationType.Subtract);
calc3.Calculate(myLastNumbers);
}
// 计算的类型
public enum CalculationType
{
Add,
Subtract,
Divide,
Multiply
};
public interface IChainOfCalc{
void SetNextChain(IChainOfCalc nextChain);
void Calculate(Number number);
}
public class Number{
public int Num1{get; protected set;}
public int Num2{get; protected set;}
public CalculationType CalcType{get; protected set;}
public Number(int num1, int num2 , CalculationType calc){
this.Num1 = num1;
this.Num2 = num2;
this.CalcType = calc;
}
}
public class AddNumber : IChainOfCalc{
protected IChainOfCalc nextChain;
public void SetNextChain(IChainOfCalc nextChain){
this.nextChain = nextChain;
}
public void Calculate(Number request){
if(request.CalcType == CalculationType.Add)
{
Debug.Log("Adding: " + request.number1 + " + " + request.number2 + " = " + (request.number1 + request.number2).ToString());
}
else if(nextInChain != null)
nextInChain.Calculate(request);
else
Debug.Log ("Handling of request failed: " + request.calculationWanted);
}
}
public class SubtractNumber : IChainOfCalc
{
protected IChainOfCalc nextInChain;
public void SetNextChain(IChainOfCalc nextChain)
{
this.nextInChain = nextChain;
}
public void Calculate(Number request)
{
if(request.CalcType == CalculationType.Subtract)
{
Debug.Log("Substracting: " + request.number1 + " - " + request.number2 + " = " + (request.number1 - request.number2).ToString());
}
else if(nextInChain != null)
nextInChain.Calculate(request);
else
Debug.Log ("Handling of request failed: " + request.calculationWanted);
}
}
public class DivideNumber : IChainOfCalc
{
protected IChainOfCalc nextInChain;
public void SetNextChain(IChainOfCalc nextChain)
{
this.nextInChain = nextChain;
}
public void Calculate(Number request)
{
if(request.CalcType == CalculationType.Divide)
{
Debug.Log("Dividing: " + request.number1 + " / " + request.number2 + " = " + (request.number1 / request.number2).ToString());
}
else if(nextInChain != null)
nextInChain.Calculate(request);
else
Debug.Log ("Handling of request failed: " + request.calculationWanted);
}
}
public class MultiplyNumber : IChainOfCalc
{
protected IChainOfCalc nextInChain;
public void SetNextChain(IChainOfCalc nextChain)
{
this.nextInChain = nextChain;
}
public void Calculate(Number request)
{
if(request.CalcType == CalculationType.Multiply)
{
Debug.Log("Multiplying: " + request.number1 + " * " + request.number2 + " = " + (request.number1 * request.number2).ToString());
}
else if(nextInChain != null)
nextInChain.Calculate(request);
else
Debug.Log ("Handling of request failed: " + request.calculationWanted);
}
}
}
最后根据上述思路可以在Unity写出一个关卡选择器等等