设计模式
1.工厂模式:所有的对象的创造都在一个类(都由一个对象)里面创造
示例:
public class Fruit
{
public virtual void ShowMe()
{
UnityEngine.Debug.Log("I am Fruit");
}
}
public class Apple : Fruit
{
public override void ShowMe()
{
//base.ShowMe();
UnityEngine.Debug.Log("I am Apple");
}
}
public class Orange : Fruit
{
public override void ShowMe()
{
//base.ShowMe();
UnityEngine.Debug.Log("I am Orange");
}
}
//工厂模式:通过传入name,返回name对应的object
public class FactoryClassManager
{
public static Fruit CreatFactory(string name)
{
switch (name)
{
case "Apple":
return new Apple();
case "Orange":
return new Orange();
// ...
default:
return new Fruit();
}
}
}
使用:
void Start () {
Fruit f = FactoryClassManager.CreatFactory("Apple");
f.ShowMe();
}
2.策略模式
示例
public enum EmployeeType
{
NromalStaff,
Manager,
CTO,
MaxValue
}
public class Salary
{
public static int GetSalary(EmployeeType type)
{
switch (type)
{
case EmployeeType.NromalStaff:
return 5000;
case EmployeeType.Manager:
return 15000;
case EmployeeType.CTO:
return 25000;
case EmployeeType.MaxValue:
return 35000;
default:
return 0;
break;
}
}
}
使用:
void Start () {
Debug.Log(Salary.GetSalary(EmployeeType.Manager));
}
3.观察者模式
示例
public class Worker
{
public string name;
public bool isWorking;
public bool isFinished;
public Worker(string name)
{
this.name = name;
}
public void Work()
{
//Do Something
}
}
public class Manager : MonoBehaviour
{
public Worker w = null;
public Manager(Worker w)
{
this.w = w;
}
void Update()
{
if (w!= null)
{
if (w.isWorking)
{
Debug.Log(w.name + "正在工作");
}
if (w.isFinished)
{
Debug.Log(w.name + "完成工作");
}
}
}
}
使用:
void Start () {
Worker w1 = new Worker("Jam");
Manager m = new Manager(w1);
}
4.代理模式
示例
public class Worker1
{
public string name;
public bool isWorking;
public bool isFinished;
public Worker1(string name)
{
this.name = name;
}
public void Working()
{
Agency.isWorking.Invoke();
}
public void Finished()
{
Agency.isFinished.Invoke();
}
}
public delegate void IsFinish();
public delegate void IsWork();
public class Agency
{
public static IsWork isWorking;
public static IsFinish isFinished;
public bool isWorkerWorking;
public bool isWorkerFinished;
public Agency()
{
isWorking += WorkerWorking;
isFinished += WorkerFinished;
}
public void WorkerWorking()
{
isWorkerWorking = true;
}
public void WorkerFinished()
{
isWorkerFinished = true;
}
}
使用:
void Start () {
Agency agency = new Agency();
Debug.Log("工人正在工作" + agency.isWorkerWorking);
Debug.Log("工人完成了工作" + agency.isWorkerFinished);
}
5.单例模式
示例:
public class Single:MonoBehaviour
{
public static Single _instance;
public string part1 = "Part1";
void Awake()
{
_instance = this;
}
}
使用:
void Start () {
Debug.Log(Single._instance.part1);
}
6.门面模式
示例
public class Shoes
{
public string name;
public int price = 200;
public Shoes(string name)
{
this.name = name;
}
public void ShowMe()
{
Debug.Log("Shoes name is" + name);
Debug.Log("Shoes price is" + price);
}
}
public class Cloth
{
public string name;
public int price = 300;
public Cloth(string name)
{
this.name = name;
}
public void ShowMe()
{
Debug.Log("Cloth name is" + name);
Debug.Log("Cloth price is" + price);
}
}
public class Food
{
public string name;
public int price = 50;
public Food(string name)
{
this.name = name;
}
public void ShowMe()
{
Debug.Log("Food name is" + name);
Debug.Log("Food price is" + price);
}
}
public class Facade
{
public Food[] foods = new Food[] {
new Food("cookie"),new Food("bread")
};
public Cloth[] clothes = new Cloth[] {
new Cloth("Meb"),new Cloth("Tb")
};
public Shoes[] shoes = new Shoes[] {
new Shoes("Nike"),new Shoes("LiNing")
};
}
使用:
void Start () {
Facade facade = new Facade();
facade.clothes[0].ShowMe();
}
7.建造者模式
示例:
public class Part1
{
public string name;
public Part1(string name)
{
this.name = name;
}
}
public class Part2
{
public string name;
public Part2(string name)
{
this.name = name;
}
}
public class Part3
{
public string name;
public Part3(string name)
{
this.name = name;
}
}
public class Builder
{
public Part1 part1 = null;
public Part2 part2 = null;
public Part3 part3 = null;
public Builder()
{
}
public Builder(string name1, string name2,string name3)
{
part1 = new Part1(name1);
part2 = new Part2(name2);
part3 = new Part3(name3);
}
public static Builder Build()
{
return new Builder("11", "22", "33");
}
}
使用:
void Start () {
Builder bu = new Builder();
Debug.Log(bu.part1.name);
}
8.中介者模式
示例:
public class Tenant
{
public string name;
public string Tel;
public Tenant(string name, string tel)
{
this.name = name;
this.Tel = tel;
}
}
public class HouseHolder
{
public string name;
public string Tel;
public HouseHolder(string name, string tel)
{
this.name = name;
this.Tel = tel;
}
}
public class Intermediary
{
public Tenant ten = new Tenant("Jim","010-111222");
public HouseHolder house = new HouseHolder("Tim","010-11223344");
public void ShowMsg()
{
Debug.Log("租客的姓名" + ten.name);
Debug.Log("户主的姓名" + house.name);
}
}
使用:
void Start () {
Intermediary iter = new Intermediary(;
iter.ShowMsg();
}