Skip to content

Commit

Permalink
添加装饰模式
Browse files Browse the repository at this point in the history
  • Loading branch information
litian committed Oct 21, 2017
1 parent f9bf530 commit bae8072
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Decorator/Decorator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
51 changes: 51 additions & 0 deletions Decorator/Finerlies/Finerly.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
29 changes: 29 additions & 0 deletions Decorator/Finerlies/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Decorator.Finerlies
{
/// <summary>
/// 装扮人类
/// </summary>
class Person
{
/// <summary>
/// 姓名
/// </summary>
private string _name;

public Person() { }

public Person(string name)
{
_name = name;
}

public virtual void Show()
{
Console.WriteLine("装扮的{0}", _name);
}
}
}
33 changes: 33 additions & 0 deletions Decorator/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
14 changes: 13 additions & 1 deletion DesignPatterns.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -37,13 +44,18 @@ 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
EndGlobalSection
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}
Expand Down

0 comments on commit bae8072

Please sign in to comment.