فهرست منبع

Merge branch 'master' of https://github.com/yanshengjie/Design-Pattern.git

shengjie_yan 8 سال پیش
والد
کامیت
76c388fd5a

+ 6 - 0
CompositePattern/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+    </startup>
+</configuration>

+ 61 - 0
CompositePattern/CompositePattern.csproj

@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>CompositePattern</RootNamespace>
+    <AssemblyName>CompositePattern</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+  </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>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Organization.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 73 - 0
CompositePattern/Organization.cs

@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+
+namespace CompositePattern
+{
+    /// <summary>
+    ///     组织架构
+    /// </summary>
+    public abstract class Organization
+    {
+        /// <summary>
+        ///     成员姓名
+        /// </summary>
+        public string MemberName { get; set; }
+
+        /// <summary>
+        ///     成员职位
+        /// </summary>
+        public string MemberPosition { get; set; }
+
+        /// <summary>
+        ///     直接上级
+        /// </summary>
+        public Organization ParentNode { get; set; }
+
+        public void Display()
+        {
+            var basicInfo = string.Format("姓名:{0},职位:{1}", MemberName, MemberPosition);
+            var parentInfo = ParentNode == null
+                ? ""
+                : string.Format(",直接上级:『姓名:{0},职位:{1}』", ParentNode.MemberName, ParentNode.MemberPosition);
+            Console.WriteLine(basicInfo + parentInfo);
+        }
+    }
+
+    /// <summary>
+    ///     部门
+    /// </summary>
+    public class Department : Organization
+    {
+        private readonly List<Organization> _organizationInfo = new List<Organization>();
+
+        public Department(string departmentName, string charge)
+        {
+            MemberPosition = departmentName;
+            MemberName = charge;
+        }
+
+        public void Add(Organization org)
+        {
+            _organizationInfo.Add(org);
+            org.ParentNode = this;
+        }
+
+        public void Remove(Organization org)
+        {
+            _organizationInfo.Remove(org);
+        }
+
+        public List<Organization> GetDepartmentMembers()
+        {
+            return _organizationInfo;
+        }
+    }
+
+
+    /// <summary>
+    ///     员工
+    /// </summary>
+    public class Member : Organization
+    {
+    }
+}

+ 73 - 0
CompositePattern/Program.cs

@@ -0,0 +1,73 @@
+using System;
+
+namespace CompositePattern
+{
+    internal class Program
+    {
+        private static void Main(string[] args)
+        {
+            Console.WriteLine("组合模式:");
+            Console.WriteLine("-------------------");
+
+            var organzation = new Department("CEO", "老总");
+            var developDepart = new Department("研发部经理", "研哥");
+
+            organzation.Add(developDepart);
+
+            var projectA = new Department("Erp项目组长", "E哥");
+
+            developDepart.Add(projectA);
+
+            var memberX = new Member {MemberPosition = "开发工程师", MemberName = "开发X"};
+            var memberY = new Member {MemberPosition = "开发工程师", MemberName = "开发Y"};
+            var memberZ = new Member {MemberPosition = "测试工程师", MemberName = "测试Z"};
+
+            projectA.Add(memberX);
+            projectA.Add(memberY);
+            projectA.Add(memberZ);
+
+            Console.WriteLine("组合模式:从上往下遍历");
+            DisplayStructure(organzation);
+            Console.WriteLine("-------------------");
+            Console.WriteLine();
+
+            Console.WriteLine("组合模式:从下往上遍历");
+            FindParent(memberX);
+            Console.WriteLine("-------------------");
+            Console.ReadLine();
+        }
+
+        /// <summary>
+        ///     正序排序
+        /// </summary>
+        /// <param name="org"></param>
+        private static void DisplayStructure(Organization org)
+        {
+            if (org.ParentNode == null)
+                org.Display();
+
+            var departMent = (Department) org;
+
+            foreach (var depart in departMent.GetDepartmentMembers())
+            {
+                depart.Display();
+                if (!(depart is Member))
+                    DisplayStructure((Department) depart);
+            }
+        }
+
+        /// <summary>
+        ///     倒序排序
+        /// </summary>
+        /// <param name="member"></param>
+        private static void FindParent(Organization member)
+        {
+            member.Display();
+            while (member.ParentNode != null)
+            {
+                member.ParentNode.Display();
+                member = member.ParentNode;
+            }
+        }
+    }
+}

+ 36 - 0
CompositePattern/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("CompositePattern")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("CompositePattern")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+//将 ComVisible 设置为 false 将使此程序集中的类型
+//对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("edee3e18-5c3f-4b91-9edf-393dc42f5e3f")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 18 - 0
DesignPattern.sln

@@ -27,6 +27,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "Strategy
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverPattern", "ObserverPattern\ObserverPattern.csproj", "{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -81,6 +87,18 @@ Global
 		{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{A0D5F127-471C-47AC-A9E0-F2BB72D2EF8F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{EDEE3E18-5C3F-4B91-9EDF-393DC42F5E3F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}.Release|Any CPU.Build.0 = Release|Any CPU
+		{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 12 - 0
FacadePattern/ATM.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FacadePattern
+{
+    class ATM
+    {
+    }
+}

+ 23 - 0
FacadePattern/AccountVerificationCenter.cs

@@ -0,0 +1,23 @@
+using System.Collections.Generic;
+
+namespace FacadePattern
+{
+    /// <summary>
+    ///     银行账户验证中心
+    /// </summary>
+    public class AccountVerificationCenter
+    {
+        private readonly List<BankAccount> accounts = new List<BankAccount>
+        {
+            new BankAccount("123456789012345", "555555", "圣杰", "135****9309", 1000000),
+            new BankAccount("123456789012346", "222222", "Jeffrey", "135****9309", 2000000),
+            new BankAccount("123456789012347", "333333", "Shengjie", "135****9309", 3000000),
+            new BankAccount("123456789012348", "777777", "程序猿", "135****9309", 4000000),
+            new BankAccount("123456789012349", "888888", "设计狮", "135****9309", 5000000)
+        };
+
+        public void Verify(string bankNo, string password)
+        {
+        }
+    }
+}

+ 6 - 0
FacadePattern/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+    </startup>
+</configuration>

+ 32 - 0
FacadePattern/Bank.cs

@@ -0,0 +1,32 @@
+using System;
+
+namespace FacadePattern
+{
+    public class Bank : IBank
+    {
+        /// <summary>
+        ///     查询余额
+        /// </summary>
+        /// <param name="account">银行账户</param>
+        /// <returns></returns>
+        public int CheckBalance(BankAccount account)
+        {
+            throw new NotImplementedException();
+        }
+
+        public int WithdrewMoney(BankAccount account, int money)
+        {
+            throw new NotImplementedException();
+        }
+
+        public int DepositMoney(BankAccount account, int money)
+        {
+            throw new NotImplementedException();
+        }
+
+        public int TransferMoney(BankAccount account, BankAccount targetAccount, int money)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 39 - 0
FacadePattern/BankAccount.cs

@@ -0,0 +1,39 @@
+namespace FacadePattern
+{
+    public class BankAccount
+    {
+        public BankAccount(string bankNo, string password, string name, string phone, int totalMoney)
+        {
+            BankNo = bankNo;
+            Password = password;
+            Name = name;
+            Phone = phone;
+            TotalMoney = totalMoney;
+        }
+
+        /// <summary>
+        ///     银行卡号
+        /// </summary>
+        public string BankNo { get; set; }
+
+        /// <summary>
+        ///     取款密码
+        /// </summary>
+        public string Password { get; set; }
+
+        /// <summary>
+        ///     持卡人姓名
+        /// </summary>
+        public string Name { get; set; }
+
+        /// <summary>
+        ///     手机
+        /// </summary>
+        public string Phone { get; set; }
+
+        /// <summary>
+        ///     总金额
+        /// </summary>
+        public int TotalMoney { get; set; }
+    }
+}

+ 65 - 0
FacadePattern/FacadePattern.csproj

@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{CA00E48A-FCA2-44F1-A7C3-DF89FD3802BE}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>FacadePattern</RootNamespace>
+    <AssemblyName>FacadePattern</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+  </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>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="AccountVerificationCenter.cs" />
+    <Compile Include="ATM.cs" />
+    <Compile Include="Bank.cs" />
+    <Compile Include="BankAccount.cs" />
+    <Compile Include="IBank.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 37 - 0
FacadePattern/IBank.cs

@@ -0,0 +1,37 @@
+namespace FacadePattern
+{
+    public interface IBank
+    {
+        /// <summary>
+        ///     查询余额
+        /// </summary>
+        /// <param name="account">银行账户</param>
+        /// <returns></returns>
+        int CheckBalance(BankAccount account);
+
+        /// <summary>
+        ///     取款
+        /// </summary>
+        /// <param name="account">银行账户</param>
+        /// <param name="money">取多少钱</param>
+        /// <returns></returns>
+        int WithdrewMoney(BankAccount account, int money);
+
+        /// <summary>
+        ///     存款
+        /// </summary>
+        /// <param name="account">银行账户</param>
+        /// <param name="money">存多少钱</param>
+        /// <returns></returns>
+        int DepositMoney(BankAccount account, int money);
+
+        /// <summary>
+        ///     转账
+        /// </summary>
+        /// <param name="account">转出账户</param>
+        /// <param name="targetAccount">目标账户</param>
+        /// <param name="money">转多少钱</param>
+        /// <returns></returns>
+        int TransferMoney(BankAccount account, BankAccount targetAccount, int money);
+    }
+}

+ 15 - 0
FacadePattern/Program.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FacadePattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+        }
+    }
+}

+ 36 - 0
FacadePattern/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("FacadePattern")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("FacadePattern")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+//将 ComVisible 设置为 false 将使此程序集中的类型
+//对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("ca00e48a-fca2-44f1-a7c3-df89fd3802be")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 6 - 0
ObserverPattern/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+    </startup>
+</configuration>

+ 49 - 0
ObserverPattern/DelegateImplement/FishingRod.cs

@@ -0,0 +1,49 @@
+using System;
+
+namespace ObserverPattern.DelegateImplement
+{
+    /// <summary>
+    ///     鱼竿
+    /// </summary>
+    public class FishingRod
+    {
+        public delegate void FishingHandler(FishType type); //声明委托
+        public event FishingHandler FishingEvent; //声明事件
+
+        public void Fishing()
+        {
+            Console.WriteLine("开始下钩!");
+
+            //用随机数模拟鱼咬钩,若随机数为偶数,则为鱼咬钩
+            if (new Random().Next() % 2 == 0)
+            {
+                var a = new Random(10).Next();
+                var type = (FishType) new Random().Next(0, 5);
+                Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了");
+                if (FishingEvent != null)
+                    FishingEvent(type);
+            }
+        }
+    }
+
+
+    /// <summary>
+    ///     垂钓者(观察者)
+    /// </summary>
+    public class FishingMan
+    {
+        public FishingMan(string name)
+        {
+            Name = name;
+        }
+
+        public string Name { get; set; }
+        public int FishCount { get; set; }
+
+        public void Update(FishType type)
+        {
+            FishCount++;
+            Console.WriteLine("{0}:钓到一条[{2}],已经钓到{1}条鱼了!", Name, FishCount, type);
+        }
+    }
+}

+ 12 - 0
ObserverPattern/FishType.cs

@@ -0,0 +1,12 @@
+namespace ObserverPattern
+{
+    public enum FishType
+    {
+        鲫鱼,
+        鲤鱼,
+        黑鱼,
+        青鱼,
+        草鱼,
+        鲈鱼
+    }
+}

+ 64 - 0
ObserverPattern/ObserverPattern.csproj

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{4C79FD56-5B6A-44A6-A2F1-4F5DBCAEC907}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>ObserverPattern</RootNamespace>
+    <AssemblyName>ObserverPattern</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+  </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>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="DelegateImplement\FishingRod.cs" />
+    <Compile Include="FishType.cs" />
+    <Compile Include="SimpleImplement\FishingTool.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <ItemGroup />
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 70 - 0
ObserverPattern/Program.cs

@@ -0,0 +1,70 @@
+using System;
+using System.Threading;
+using ObserverPattern.SimpleImplement;
+
+namespace ObserverPattern
+{
+    internal class Program
+    {
+        private static void Main(string[] args)
+        {
+            SimpleObserverTest();
+            Console.ReadLine();
+            Console.WriteLine("=======================");
+            DelegateObserverTest();
+            Console.ReadLine();
+        }
+
+        /// <summary>
+        ///     测试简单实现的观察者模式
+        /// </summary>
+        private static void SimpleObserverTest()
+        {
+            Console.WriteLine("简单实现的观察者模式:");
+            Console.WriteLine("=======================");
+            //1、初始化鱼竿
+            var fishingRod = new FishingRod();
+
+            //2、声明垂钓者
+            var jeff = new FishingMan("圣杰");
+
+            //3、将垂钓者观察鱼竿
+            fishingRod.AddSubscriber(jeff);
+
+            //4、循环钓鱼
+            while (jeff.FishCount < 5)
+            {
+                fishingRod.Fishing();
+                Console.WriteLine("-------------------");
+                //睡眠5s
+                Thread.Sleep(5000);
+            }
+        }
+
+        /// <summary>
+        ///     测试委托实现的观察者模式
+        /// </summary>
+        private static void DelegateObserverTest()
+        {
+            Console.WriteLine("委托实现的观察者模式:");
+            Console.WriteLine("=======================");
+            //1、初始化鱼竿
+            var fishingRod = new DelegateImplement.FishingRod();
+
+            //2、声明垂钓者
+            var jeff = new DelegateImplement.FishingMan("圣杰");
+
+            //3、注册观察者
+            fishingRod.FishingEvent += jeff.Update;
+
+            //4、循环钓鱼
+            while (jeff.FishCount < 5)
+            {
+                fishingRod.Fishing();
+                Console.WriteLine("-------------------");
+                //睡眠5s
+                Thread.Sleep(5000);
+            }
+        }
+    }
+}

+ 36 - 0
ObserverPattern/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("ObserverPattern")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("ObserverPattern")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+//将 ComVisible 设置为 false 将使此程序集中的类型
+//对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("4c79fd56-5b6a-44a6-a2f1-4f5dbcaec907")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 85 - 0
ObserverPattern/SimpleImplement/FishingTool.cs

@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+
+namespace ObserverPattern.SimpleImplement
+{
+    /// <summary>
+    ///     钓鱼工具抽象类
+    ///     用来维护订阅者列表,并通知订阅者
+    /// </summary>
+    public abstract class FishingTool
+    {
+        private readonly List<ISubscriber> _subscribers;
+
+        protected FishingTool()
+        {
+            _subscribers = new List<ISubscriber>();
+        }
+
+        public void AddSubscriber(ISubscriber subscriber)
+        {
+            if (!_subscribers.Contains(subscriber))
+                _subscribers.Add(subscriber);
+        }
+
+        public void RemoveSubscriber(ISubscriber subscriber)
+        {
+            if (_subscribers.Contains(subscriber))
+                _subscribers.Remove(subscriber);
+        }
+
+        public void Notify(FishType type)
+        {
+            foreach (var subscriber in _subscribers)
+                subscriber.Update(type);
+        }
+    }
+
+    /// <summary>
+    ///     鱼竿
+    /// </summary>
+    public class FishingRod : FishingTool
+    {
+        public void Fishing()
+        {
+            Console.WriteLine("开始下钩!");
+
+            //用随机数模拟鱼咬钩,若随机数为偶数,则为鱼咬钩
+            if (new Random().Next() % 2 == 0)
+            {
+                var type = (FishType) new Random().Next(0, 5);
+                Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了");
+                Notify(type);
+            }
+        }
+    }
+
+    /// <summary>
+    ///     订阅者(观察者)接口
+    ///     由具体的订阅者实现Update()方法
+    /// </summary>
+    public interface ISubscriber
+    {
+        void Update(FishType type);
+    }
+
+    /// <summary>
+    ///     垂钓者实现观察者接口
+    /// </summary>
+    public class FishingMan : ISubscriber
+    {
+        public FishingMan(string name)
+        {
+            Name = name;
+        }
+
+        public string Name { get; set; }
+        public int FishCount { get; set; }
+
+        public void Update(FishType type)
+        {
+            FishCount++;
+            Console.WriteLine("{0}:钓到一条[{2}],已经钓到{1}条鱼了!", Name, FishCount, type);
+        }
+    }
+}

+ 14 - 2
README.md

@@ -19,11 +19,11 @@
 
 3. [我是独一无二的『单例模式』](http://www.jianshu.com/p/2054c44dcd5a)
 
-4. [创建相似对象,就交给『工厂模式』吧](http://www.jianshu.com/p/de190cd72fb6)
+4. [创建相似对象,就交给『工厂模式』吧](http://www.jianshu.com/p/1275b99ca973)
 
 5. [固定模板,不同算法,就用『模板方法模式』](http://www.jianshu.com/p/4c8d1a0a75e1)
 
-6. [关注产出,不关心细节,『建造者模式』](http://www.jianshu.com/p/f5a87d678b79)
+6. [关注产出,不关心细节,『建造者模式』](http://www.jianshu.com/p/c5811ca1d208)
 
 7. [重复构造,打出原形,『原型模式』](http://www.jianshu.com/p/ce7b981708b4)
 
@@ -36,3 +36,15 @@
 11. [流程业务,各司其职,『责任链模式』](http://www.jianshu.com/p/95908acb842a)
 
 12. [毛坯还是精装修,先看看样板房,『装饰模式』](http://www.jianshu.com/p/246041fc39a4)
+
+13. [算法独立于场景而灵活变化,『策略模式』](http://www.jianshu.com/p/390fd50d02b8)
+
+14. [新旧对接,『适配器模式』](http://www.jianshu.com/p/5e5f5024c62e)
+
+15. [玩转集合容器,『迭代器模式』](http://www.jianshu.com/p/ae229f72c522)
+
+16. [部分整体,树形结构,『组合模式』来帮忙](http://www.jianshu.com/p/9e49e5d702df)
+
+17. [『观察者模式』来钓鱼](http://www.jianshu.com/p/45675c73296d)
+
+![设计模式之禅](http://upload-images.jianshu.io/upload_images/2799767-4df489c0f630a241.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)