diff --git a/Decorator/Decorator.csproj b/Decorator/Decorator.csproj new file mode 100644 index 0000000..ce1697a --- /dev/null +++ b/Decorator/Decorator.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.0 + + + diff --git a/Decorator/Finerlies/Finerly.cs b/Decorator/Finerlies/Finerly.cs new file mode 100644 index 0000000..cedfc9e --- /dev/null +++ b/Decorator/Finerlies/Finerly.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Decorator.Finerlies +{ + class Finerly : Person + { + protected Person _person; + + public virtual void Decoteter(Person person) + { + _person = person; + } + + public override void Show() + { + if (_person != null) + { + _person.Show(); + } + } + } + + class TShirts : Finerly + { + public override void Show() + { + Console.WriteLine("大T恤"); //扩展 + base.Show(); + } + } + + class BigTrouser : Finerly + { + public override void Show() + { + Console.WriteLine("垮裤"); //扩展 + base.Show(); + } + } + + class WearSneakers : Finerly + { + public override void Show() + { + Console.WriteLine("破球鞋"); //扩展 + base.Show(); + } + } +} diff --git a/Decorator/Finerlies/Person.cs b/Decorator/Finerlies/Person.cs new file mode 100644 index 0000000..6d88414 --- /dev/null +++ b/Decorator/Finerlies/Person.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Decorator.Finerlies +{ + /// + /// 装扮人类 + /// + class Person + { + /// + /// 姓名 + /// + private string _name; + + public Person() { } + + public Person(string name) + { + _name = name; + } + + public virtual void Show() + { + Console.WriteLine("装扮的{0}", _name); + } + } +} diff --git a/Decorator/Program.cs b/Decorator/Program.cs new file mode 100644 index 0000000..0092a5b --- /dev/null +++ b/Decorator/Program.cs @@ -0,0 +1,33 @@ +/**** + * 装饰模式:动态地给对象添加一些额外的职责,就增加功能而言,装饰模式比生产子类跟灵活。 + * 优点: + * 把类中的装饰功能从类中搬移出去,这样可以简化原有的类。 + * 缺点: + * 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂 + * + * + */ + +using Decorator.Finerlies; +using System; + +namespace Decorator +{ + class Program + { + static void Main(string[] args) + { + Person xc = new Person("小李"); + + TShirts ts = new TShirts(); + BigTrouser bt = new BigTrouser(); + WearSneakers ws = new WearSneakers(); + + ts.Decoteter(xc); + bt.Decoteter(ts); + ws.Decoteter(bt); + ws.Show(); + Console.ReadKey(); + } + } +} diff --git a/DesignPatterns.sln b/DesignPatterns.sln index 5d1a513..a3b3b34 100644 --- a/DesignPatterns.sln +++ b/DesignPatterns.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27004.2005 +VisualStudioVersion = 15.0.27004.2002 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "简单工厂模式", "简单工厂模式", "{B9A0E9C2-B585-44FB-A44A-1839C944992E}" EndProject @@ -23,6 +23,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "单一职责原则", "单 C:\Users\litia\Desktop\单一职责原则.docx = C:\Users\litia\Desktop\单一职责原则.docx EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "装饰模式", "装饰模式", "{FD5FC40F-E348-43FE-8AB5-A37A4FD70470}" + ProjectSection(SolutionItems) = preProject + C:\Users\IWIT\Desktop\装饰者模式结构图.png = C:\Users\IWIT\Desktop\装饰者模式结构图.png + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Decorator\Decorator.csproj", "{B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,6 +44,10 @@ Global {2C4C7090-141C-41BC-BA69-E112916E4BCC}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C4C7090-141C-41BC-BA69-E112916E4BCC}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C4C7090-141C-41BC-BA69-E112916E4BCC}.Release|Any CPU.Build.0 = Release|Any CPU + {B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -44,6 +55,7 @@ Global GlobalSection(NestedProjects) = preSolution {09810354-E3B4-47FE-A9DA-D0DE69411C30} = {B9A0E9C2-B585-44FB-A44A-1839C944992E} {2C4C7090-141C-41BC-BA69-E112916E4BCC} = {93EAC40F-A508-4F46-81FD-673789692318} + {B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA} = {FD5FC40F-E348-43FE-8AB5-A37A4FD70470} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E6743BD3-7935-40EF-BE42-93886BAC3C40}