1:个人学习记录。
2:大佬请绕道。。。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace DesignMode.wcc
{
/// <summary>
/// 设计模式
/// </summary>
public class Design
{
//当前类静态实列
static Design design;
#region 单列
/// <summary>
/// 获取Design类实列对象
/// </summary>
public static Design GetInitDesign
{
get
{
if (design == null)
{
Start();
}
return design;
}
}
static void Start()
{
if (design == null)
{
design = new Design();
}
}
public void InitMainStart()
{
//适配器
Adapter adapter = new Adapter();
adapter.ThreeFun();
//工厂
Automobile mobile = CarsSimpleFactory.GetCarInitData("宝马");
mobile.ProduceCars();
//观察者
MainStart mainStart = new MainStart();
mainStart.Start();
//策略
StragetyStart stragetyStart = new StragetyStart();
stragetyStart.Start();
//外观
FacadeMain facadeMain = new FacadeMain();
facadeMain.Start();
//组合
CompositeMain compositeMain = new CompositeMain();
compositeMain.Start();
}
#endregion
}
}
#region 创建型:主要用于创建对象
#region 工厂模式
//定义生产汽车的抽象类
abstract class Automobile
{
/// <summary>
/// 生产汽车
/// </summary>
public abstract void ProduceCars();
}
/// <summary>
/// 奔驰
/// </summary>
class Gallop : Automobile
{
public override void ProduceCars()
{
Debug.Log("生产奔驰一系列操作");
}
}
class Bmw : Automobile
{
public override void ProduceCars()
{
Debug.Log("生产宝马一系列操作");
}
}
/// <summary>
/// 工厂类 负责生产汽车
/// </summary>
class CarsSimpleFactory
{
public static Automobile GetCarInitData(string carName = "奔驰")
{
Automobile automobile = null;
switch (carName)
{
case "奔驰":
automobile = new Gallop();
break;
case "宝马":
automobile = new Bmw();
break;
}
return automobile;
}
}
#endregion
#endregion
#region 结构型模式:主要用于处理类或对象的组合
#region 适配器模式:
/// 这里以插座和插头的例子来诠释适配器模式
/// 现在我们买的电器插头是2个孔,但是我们买的插座只有3个孔的
/// 这是我们想把电器插在插座上的话就需要一个电适配器
///重写父类三个插头的接口 在重写接口内部调用两个空的方法
///
class Threewplug
{
public virtual void ThreeFun()
{
Debug.Log("Threewplug三个接口方法");
}
}
class Twowplug
{
public void TwoFun()
{
Debug.Log("我是两个插头");
}
}
/// <summary>
/// 适配器
/// </summary>
class Adapter : Threewplug
{
Twowplug twowplug = new Twowplug();
public override void ThreeFun()
{
twowplug.TwoFun();
}
}
#endregion
#region 组合模式(Composite):关键点:简单对象和复合对象必须实现相同的接口
//。这就是组合模式能够将组合对象和简单对象进行一致处理的原因。
class CompositeMain
{
public void Start()
{
ComplexGraphics graphics = new ComplexGraphics("一个复杂图形和两条线段组成的复杂图形");
graphics.Add(new Line("线段A"));
ComplexGraphics CompositeCG = new ComplexGraphics("一个圆和一条线组成的复杂图形");
CompositeCG.Add(new Circle("圆"));
CompositeCG.Add(new Circle("线段B"));
graphics.Add(CompositeCG);
Line linem = new Line("线段C");
graphics.Add(linem);
graphics.Draw();
}
}
//图形抽象类
public abstract class Graphics
{
public string Name { get; set; }
public Graphics(string name)
{
this.Name = name;
}
public abstract void Draw();
// 移除了Add和Remove方法
// 把管理子对象的方法放到了ComplexGraphics类中进行管理
// 因为这些方法只在复杂图形中才有意义
}
//简单图形类线
class Line : Graphics
{
public Line(string name) : base(name)
{
}
public override void Draw()
{
Debug.Log("画" + Name);
}
}
//简单图形类圆
class Circle : Graphics
{
public Circle(string name) : base(name)
{
}
public override void Draw()
{
Debug.Log("画" + Name);
}
}
/// 复杂图形
/// 由一些简单图形组成,这里假设该复杂图形由一个圆两条线组成的复杂图形
///
class ComplexGraphics : Graphics
{
List<Graphics> complexGraphicsList = new List<Graphics>();
public ComplexGraphics(string name) : base(name)
{
}
public override void Draw()
{
foreach (Graphics item in complexGraphicsList)
{
item.Draw();
}
}
public void Add(Graphics graphics)
{
complexGraphicsList.Add(graphics);
}
public void Remove(Graphics graphics)
{
complexGraphicsList.Remove(graphics);
}
}
#endregion
#region 外观模式(Facade) :外观模式提供了一个统一的接口,用来访问子系统中的一群接口。
//观定义了一个高层接口,让子系统更容易使用。
//使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直接通过外观类来调用内部子系统中方法,从而外观模式让客户和子系统之间避免了紧耦合。
class FacadeMain
{
/// 验证选课的人数是否已满
/// 通知用户课程选择成功与否
public void Start()
{
RegistrationFacade registrationFacade = new RegistrationFacade();
registrationFacade.RegisterCourse("设计模式", "荣稳稳");
}
}
/// <summary>
/// 外观类
/// </summary>
class RegistrationFacade
{
SystemA systemA;
SystemB systemB;
public RegistrationFacade()
{
systemA = new SystemA();
systemB = new SystemB();
}
public bool RegisterCourse(string courseName, string studentName)
{
//课程是否选择成功
if (!systemA.SystemAFun(courseName))
{
return false;
}
return systemB.SystemBFun(studentName);
}
}
//子系统A:处理选择的课程是否成功
class SystemA
{
public bool SystemAFun(string name)
{
Debug.Log(string.Format("正在验证课程 {0} 是否人数已满", name));
return true;
}
}
//子系统B:处理课程人数是否已满
class SystemB
{
public bool SystemBFun(string studentName)
{
Debug.Log(string.Format("正在向{0}发生通知", studentName));
return true;
}
}
#endregion
#endregion
#region 行为型模式:主要用于描述类或对象如何交互和怎样分配职责
#region 观察者模式:技能效果调用
//使用委托与事件来简化观察者模式的实现
//需求:腾讯订阅号和订阅者
/// <summary>
/// 订阅号父类
/// </summary>
public class TenXun
{
Action<object> action;
public string symName;
public string info;
//构造函数:初始化基础数据
public TenXun(string symName, string info)
{
this.symName = symName;
this.info = info;
}
public TenXun()
{
}
//public TenXun(string mmp)
//{
//}
//注册事件
public void AddAction(Action<object> actionEvent)
{
action += actionEvent;
}
//取消事件
public void RemoveAction(Action<object> actionEvent)
{
action -= actionEvent;
}
//触发一对多事件
public void UpDate()
{
if (action != null)
{
action(this);
}
}
}
//具体订阅号
class JTnXun : TenXun
{
public JTnXun(string symName, string info) : base(symName, info)
{
}
}
//具体订阅者
public class Subscriber
{
public string name;
public Subscriber(string name)
{
this.name = name;
}
//响应事件具体方法
public void JtEvent(object obj)
{
TenXun ten = obj as TenXun;
Debug.Log("当前订阅者名称" + name + "订阅号" + ten.symName + "//" + ten.info);
}
}
public class MainStart
{
public void Start()
{
//观察者模式测试
JTnXun jTnXunm = new JTnXun("王者荣耀", "新增互动玩法");
var mm = jTnXunm.symName;
Subscriber subscribe = new Subscriber("张三");
Subscriber subscribe_wangwu = new Subscriber("王五");
//注册事件
jTnXunm.AddAction(subscribe.JtEvent);
jTnXunm.AddAction(subscribe_wangwu.JtEvent);
//响应所有的订阅者
jTnXunm.UpDate();
//迭代器模式测试------------------------------------------------------------------------------
Iterator iterator;
IListCollectionTest listCollectionTest = new CollectionTest();
iterator = listCollectionTest.GetIterator();
while (iterator.MoveNext())
{
int m = (int)iterator.GetCurrent();
Debug.Log(m + "迭代器模式测试");
iterator.Next();
}
}
}
#endregion
#region 策略模式:技能效果类和方法的构建。
//将每个算法封装到不同的策略类中,使得它们可以互换”。
public class StragetyStart
{
public void Start()
{
//个人的
InterestOperation interestOperationPersonal = new InterestOperation(new PersonalTaxStrategy());
var resurlt = interestOperationPersonal.GetTax(10);
//企业的的
InterestOperation interestOperationEnterpriseTax = new InterestOperation(new EnterpriseTaxStrategy());
var resurlt2 = interestOperationEnterpriseTax.GetTax(10);
//国家的
InterestOperation interestOperation = new InterestOperation(new StateEnterpriseTaxStrategy());
var resurlt3 = interestOperation.GetTax(10);
Debug.Log("个人的:" + resurlt + "/" + "企业的" + resurlt2 + "/" + "国家的" + resurlt3);
}
}
/// <summary>
/// 定义计算的抽象接口
/// </summary>
public interface ITaxStragety
{
double CalculateTax(double income);
}
//统一管理暴露接口
public class InterestOperation
{
ITaxStragety taxStragety;
public InterestOperation(ITaxStragety taxStragety)
{
//传入具体实体
this.taxStragety = taxStragety;
}
//获取结果
public double GetTax(double income)
{
return taxStragety.CalculateTax(income);
}
}
//个人所得税
public class PersonalTaxStrategy : ITaxStragety
{
public double CalculateTax(double income)
{
return income + 50;
}
}
//企业所得税
public class EnterpriseTaxStrategy : ITaxStragety
{
public double CalculateTax(double income)
{
return income - 50;
}
}
//国家所得税
public class StateEnterpriseTaxStrategy : ITaxStragety
{
public double CalculateTax(double income)
{
return income + 100;
}
}
#endregion
#region 迭代器模式:就是用迭代器类来承担遍历集合元素的职责。
/// <summary>
///接口迭代器
/// </summary>
interface Iterator
{
bool MoveNext();//判断当前索引是否超过数组边界
System.Object GetCurrent();
void Next();
void Reset();
}
/// <summary>
///接口集合
/// </summary>
interface IListCollectionTest
{
Iterator GetIterator();
}
/// <summary>
///具体迭代器
/// </summary>
class TeratorTest : Iterator
{
// 迭代器要集合对象进行遍历操作,自然就需要引用集合对象
CollectionTest CollectionTest;
int _index;
public TeratorTest(CollectionTest m)
{
CollectionTest = m;
_index = 0;
}
object Iterator.GetCurrent()
{
return CollectionTest.GetElement(_index);
}
bool Iterator.MoveNext()
{
if (_index < CollectionTest.Length)
{
return true;
}
return false;
}
void Iterator.Next()
{
if (_index < CollectionTest.Length)
{
_index++;
}
}
void Iterator.Reset()
{
_index = 0;
}
}
/// <summary>
///具体集合
/// </summary>
class CollectionTest : IListCollectionTest
{
int[] collection;
public CollectionTest()
{
collection = new int[] { 2, 4, 6, 8 };
}
Iterator IListCollectionTest.GetIterator()
{
return new TeratorTest(this);
}
public int Length
{
get { return collection.Length; }
}
public int GetElement(int index)
{
return collection[index];
}
}
#endregion
#endregion