Skip to content

Commit

Permalink
添加代理模式示例代码和说明
Browse files Browse the repository at this point in the history
  • Loading branch information
孤鸿 夜 committed Oct 22, 2017
1 parent bae8072 commit 5b00892
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 5 deletions.
Binary file added Decorator/装饰模式结构图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions 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.2002
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "简单工厂模式", "简单工厂模式", "{B9A0E9C2-B585-44FB-A44A-1839C944992E}"
EndProject
Expand All @@ -24,11 +24,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "单一职责原则", "单
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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{B0519B67-B1D7-42F9-ABA8-D8CA6494C0EA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "代理模式", "代理模式", "{E2999A1E-83CB-4A53-AAAC-E72A2605F013}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxies", "Proxies\Proxies.csproj", "{F0186C45-D0D4-4E7C-BDED-1BB9723538E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -48,6 +49,10 @@ Global
{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
{F0186C45-D0D4-4E7C-BDED-1BB9723538E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0186C45-D0D4-4E7C-BDED-1BB9723538E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0186C45-D0D4-4E7C-BDED-1BB9723538E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0186C45-D0D4-4E7C-BDED-1BB9723538E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -56,6 +61,7 @@ Global
{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}
{F0186C45-D0D4-4E7C-BDED-1BB9723538E2} = {E2999A1E-83CB-4A53-AAAC-E72A2605F013}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E6743BD3-7935-40EF-BE42-93886BAC3C40}
Expand Down
21 changes: 21 additions & 0 deletions Proxies/GiveGift/Girl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Proxies.GiveGift
{
/// <summary>
/// 女孩类
/// </summary>
class Girl
{
public Girl(string name)
{
_name = name;
}

private string _name;

public string Name { get => _name; set => _name = value; }
}
}
16 changes: 16 additions & 0 deletions Proxies/GiveGift/IGiveGift.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Proxies.GiveGift
{
/// <summary>
/// 送礼接口
/// </summary>
interface IGiveGift
{
void GiveDolls();
void GiveFlowers();
void GiveChocolate();
}
}
34 changes: 34 additions & 0 deletions Proxies/GiveGift/Proxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Proxies.GiveGift
{
/// <summary>
/// 代理类
/// </summary>
class Proxy : IGiveGift
{
Pursuit gg;

public Proxy(Girl mm)
{
gg = new Pursuit(mm);//通过代理,实例化追求者,Girl并不知道
}

public void GiveChocolate()
{
gg.GiveChocolate();
}

public void GiveDolls()
{
gg.GiveDolls();
}

public void GiveFlowers()
{
gg.GiveFlowers();
}
}
}
35 changes: 35 additions & 0 deletions Proxies/GiveGift/Pursuit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Proxies.GiveGift
{
/// <summary>
/// 追求者类
/// </summary>
class Pursuit : IGiveGift
{
Girl girl;

public Pursuit(Girl girl)
{
this.girl = girl;
}

public void GiveChocolate()
{
Console.WriteLine(girl.Name + "送你巧克力");
}

public void GiveDolls()
{
Console.WriteLine(girl.Name + "送你洋娃娃");

}

public void GiveFlowers()
{
Console.WriteLine(girl.Name + "送你鲜花");
}
}
}
35 changes: 35 additions & 0 deletions Proxies/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/****
* 代理模式:为其他对象提供一种代理以控制对这个对象的访问。
* 应用:
* 1. 远程代理,也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在与不同的地址空间的事实;
* 2. 虚拟代理,是根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真是对象;
* 3. 安全代理,用来控制真实对象访问时的权限。
* 4. 智能引用,是指当调用真实对象时,代理处理另外的一些事。
*
*
*/


using Proxies.GiveGift;
using System;

/// <summary>
/// 代理模式实例,追求者通过代理给女孩送礼物
/// </summary>
namespace Proxies
{
class Program
{
static void Main(string[] args)
{

Proxy proxy = new Proxy(new Girl("小红"));

proxy.GiveChocolate();
proxy.GiveDolls();
proxy.GiveFlowers();

Console.ReadKey();
}
}
}
8 changes: 8 additions & 0 deletions Proxies/Proxies.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>
Binary file added Proxies/代理模式结构.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5b00892

Please sign in to comment.