forked from litiansum/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
litian
committed
Oct 21, 2017
1 parent
f9bf530
commit bae8072
Showing
5 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters