策略者模式:根据不同的输入得到不同种类的输出结果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AbsBase
{
public float salary;
public virtual void CaculateTax()
{
}
}
public class AbsPerson : AbsBase
{
public override void CaculateTax()
{
salary *= 0.08f;
}
}
public class AbsCompany : AbsBase
{
public override void CaculateTax()
{
salary *= 0.12f;
}
}
public class TestAbstrator : MonoBehaviour
{
public void CaculateTax(AbsBase tmpBase)
{
tmpBase.CaculateTax();
}
// Start is called before the first frame update
void Start()
{
AbsPerson tmpPerson = new AbsPerson();
CaculateTax(tmpPerson);//多态
AbsCompany tmpCompany = new AbsCompany();
CaculateTax(tmpCompany);
}
// Update is called once per frame
void Update()
{
}
}
A——>C——>D
B——>C——>E
建造者模式:
使不同功能进行模块化,每个模块实现不同功能,有特定接口;
public class A
{
public void A1()
{
//.....
}
public void A2()
{
//....
}
}
public class B
{
A a;
public void B1()
{
a.A1();
}
public void B2()
{
B1();
}
}
public class C
{
public static C instance
public static C Instance
{
get
{
if(instance== null)
{
instance=new C();
}
return instance;
}
}
B b;
public void C1()
{
b.B2();
}
public void C2()
{
}
}
public class TestBuider : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//实现通过接口访问
C.Instance.C1();
}
// Update is called once per frame
void Update()
{
}
}
|