From c09df35f13b1ef506520bea0f996920b36cf1626 Mon Sep 17 00:00:00 2001 From: acaccav147 <1090259660@qq.com> Date: Sun, 4 Jul 2021 15:59:44 +0800 Subject: [PATCH] add project && add dicorator and bridge --- Design Pattern/App.config | 6 + Design Pattern/Client.cs | 29 +++++ Design Pattern/Design Patterns.csproj | 44 +++++++ .../Bridge.cs" | 99 ++++++++++++++++ .../Decorator.cs" | 112 ++++++++++++++++++ README.md | 2 +- 6 files changed, 291 insertions(+), 1 deletion(-) create mode 100644 Design Pattern/App.config create mode 100644 Design Pattern/Client.cs create mode 100644 Design Pattern/Design Patterns.csproj create mode 100644 "Design Pattern/Structural/Bridge(\346\241\245\346\216\245\346\250\241\345\274\217)/Bridge.cs" create mode 100644 "Design Pattern/Structural/Decorator(\350\243\205\351\245\260\350\200\205)/Decorator.cs" diff --git a/Design Pattern/App.config b/Design Pattern/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/Design Pattern/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Design Pattern/Client.cs b/Design Pattern/Client.cs new file mode 100644 index 0000000..d14e8ca --- /dev/null +++ b/Design Pattern/Client.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Design_Pattern +{ + class Client + { + public static void Main() + { + // 装饰者 + IExecute exe = new Decorator(); + exe.Execute(); + + // 桥模式 + exe = new Bridge(); + exe.Execute(); + + Console.ReadKey(); + } + } + + public interface IExecute + { + void Execute(); + } +} diff --git a/Design Pattern/Design Patterns.csproj b/Design Pattern/Design Patterns.csproj new file mode 100644 index 0000000..2790176 --- /dev/null +++ b/Design Pattern/Design Patterns.csproj @@ -0,0 +1,44 @@ + + + + + Debug + AnyCPU + {A0764AC7-5A51-4EC4-A3D8-EA452543D6D6} + Exe + Design_Pattern + Design Pattern + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + \ No newline at end of file diff --git "a/Design Pattern/Structural/Bridge(\346\241\245\346\216\245\346\250\241\345\274\217)/Bridge.cs" "b/Design Pattern/Structural/Bridge(\346\241\245\346\216\245\346\250\241\345\274\217)/Bridge.cs" new file mode 100644 index 0000000..433e25c --- /dev/null +++ "b/Design Pattern/Structural/Bridge(\346\241\245\346\216\245\346\250\241\345\274\217)/Bridge.cs" @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Design_Pattern +{ + public class Bridge : IExecute + { + void IExecute.Execute() + { + Console.WriteLine("\n桥接模式执行......"); + // 声明一个客户端的战斗会话类,会话链接类型为TCP + var battle = new ClientBattleSession(new TCPSession()); + battle.OnConnect(); + battle.OnSend(); + battle.OnReceive(); + battle.OnClose(); + } + } + + /* 定义实现者 */ + + /// + /// Implementor 会话接口 + /// + public interface SessionImp + { + void Send(); + void Receive(); + } + + public class UDPSession : SessionImp + { + public void Send() { Console.WriteLine("UDP Send"); } + public void Receive() { Console.WriteLine("UDP Receive"); } + } + + public class TCPSession : SessionImp + { + public void Send() { Console.WriteLine("TCP Send"); } + public void Receive() { Console.WriteLine("TCP Receive"); } + } + + /* 定义抽象类 */ + + public abstract class BattleSession + { + protected SessionImp m_Imp; + + public virtual void OnConnect() { } + public virtual void OnClose() { } + public virtual void OnSend() { } + public virtual void OnReceive() { } + + public BattleSession(SessionImp imp) + { + m_Imp = imp; + } + } + + public class ClientBattleSession : BattleSession + { + public override void OnConnect() + { + base.OnConnect(); + Console.WriteLine("Client onConnect"); + } + + public override void OnClose() + { + base.OnClose(); + Console.WriteLine("Client onClose"); + } + + public override void OnSend() + { + base.OnSend(); + Console.WriteLine("Client onSend"); + m_Imp.Send(); + } + + public override void OnReceive() + { + base.OnReceive(); + Console.WriteLine("Client onReceive"); + m_Imp.Receive(); + } + + public ClientBattleSession(SessionImp imp) : base(imp) { } + } + + //public class ServerBattleSession : BattleSession + //{ + // //...... + //} + +} diff --git "a/Design Pattern/Structural/Decorator(\350\243\205\351\245\260\350\200\205)/Decorator.cs" "b/Design Pattern/Structural/Decorator(\350\243\205\351\245\260\350\200\205)/Decorator.cs" new file mode 100644 index 0000000..3e25fa6 --- /dev/null +++ "b/Design Pattern/Structural/Decorator(\350\243\205\351\245\260\350\200\205)/Decorator.cs" @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Design_Pattern +{ + public class Decorator : IExecute + { + void IExecute.Execute() + { + Console.WriteLine("装饰者模式执行......"); + Person person = new Person(); + // 定义一个镶嵌了绿宝石的法杖 + IDecorator GreenStaff = new GreenGemStone(new Staff()); + person.SetWeapon(GreenStaff); + person.UseWeapon(); + + // 定义一个镶嵌了红宝石的法杖 + IDecorator RedStaff = new RedGemStone(new Staff()); + person.SetWeapon(RedStaff); + person.UseWeapon(); + } + } + + /// + /// 友情出演 人 类 + /// + public class Person + { + IDecorator m_Weapon; + + public void SetWeapon(IDecorator weapon) + { + m_Weapon = weapon; + } + + public void UseWeapon() + { + m_Weapon.Attack(); + } + } + + /// + /// 武器接口 + /// + public interface IDecorator + { + void Attack(); + } + + /// + /// 法杖类(被装饰者) + /// + public class Staff : IDecorator + { + public void Attack() + { + Console.WriteLine("法杖进行攻击,普通伤害"); + } + } + + /*-------------------- 以下是装饰类 --------------------*/ + + /// + /// 宝石装饰抽象类 + /// + public abstract class GemStoneDecorator : IDecorator + { + IDecorator m_Weapon; + + // 实现接口 + public virtual void Attack() + { + m_Weapon.Attack(); + } + + public GemStoneDecorator(IDecorator weapon) + { + m_Weapon = weapon; + } + } + + /// + /// 红宝石类(具体装饰类),增加攻击效果:增加眩晕效果 + /// + public class RedGemStone : GemStoneDecorator + { + public override void Attack() + { + base.Attack(); + Console.WriteLine("镶嵌了红宝石的武器进行攻击,造成眩晕效果"); + } + + public RedGemStone(IDecorator weapon) : base(weapon) { } + } + + /// + /// 绿宝石类(具体装饰类),增加攻击效果:增加降速效果 + /// + public class GreenGemStone : GemStoneDecorator + { + public override void Attack() + { + base.Attack(); + Console.WriteLine("镶嵌了绿宝石的武器进行攻击,造成降速效果"); + } + + public GreenGemStone(IDecorator weapon) : base(weapon) { } + } +} diff --git a/README.md b/README.md index 45cc389..5d25b04 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# sample-data-repo +关于游戏开发中的设计模式 -- GitLab