提交 c09df35f 编写于 作者: 少侠Smile丶's avatar 少侠Smile丶

add project && add dicorator and bridge

上级 d979a654
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
\ No newline at end of file
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();
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A0764AC7-5A51-4EC4-A3D8-EA452543D6D6}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Design_Pattern</RootNamespace>
<AssemblyName>Design Pattern</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="Structural\Bridge%28桥接模式%29\Bridge.cs" />
<Compile Include="Structural\Decorator%28装饰者%29\Decorator.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
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();
}
}
/* 定义实现者 */
/// <summary>
/// Implementor 会话接口
/// </summary>
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
//{
// //......
//}
}
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();
}
}
/// <summary>
/// 友情出演 人 类
/// </summary>
public class Person
{
IDecorator m_Weapon;
public void SetWeapon(IDecorator weapon)
{
m_Weapon = weapon;
}
public void UseWeapon()
{
m_Weapon.Attack();
}
}
/// <summary>
/// 武器接口
/// </summary>
public interface IDecorator
{
void Attack();
}
/// <summary>
/// 法杖类(被装饰者)
/// </summary>
public class Staff : IDecorator
{
public void Attack()
{
Console.WriteLine("法杖进行攻击,普通伤害");
}
}
/*-------------------- 以下是装饰类 --------------------*/
/// <summary>
/// 宝石装饰抽象类
/// </summary>
public abstract class GemStoneDecorator : IDecorator
{
IDecorator m_Weapon;
// 实现接口
public virtual void Attack()
{
m_Weapon.Attack();
}
public GemStoneDecorator(IDecorator weapon)
{
m_Weapon = weapon;
}
}
/// <summary>
/// 红宝石类(具体装饰类),增加攻击效果:增加眩晕效果
/// </summary>
public class RedGemStone : GemStoneDecorator
{
public override void Attack()
{
base.Attack();
Console.WriteLine("镶嵌了红宝石的武器进行攻击,造成眩晕效果");
}
public RedGemStone(IDecorator weapon) : base(weapon) { }
}
/// <summary>
/// 绿宝石类(具体装饰类),增加攻击效果:增加降速效果
/// </summary>
public class GreenGemStone : GemStoneDecorator
{
public override void Attack()
{
base.Attack();
Console.WriteLine("镶嵌了绿宝石的武器进行攻击,造成降速效果");
}
public GreenGemStone(IDecorator weapon) : base(weapon) { }
}
}
# sample-data-repo
关于游戏开发中的设计模式
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册