瀏覽代碼

建造者模式

jeffrey 9 年之前
父節點
當前提交
6939a6ffb9

+ 6 - 0
BuilderPattern/App.config

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

+ 59 - 0
BuilderPattern/BuilderPattern.csproj

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.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>{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>BuilderPattern</RootNamespace>
+    <AssemblyName>BuilderPattern</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </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.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Director.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>

+ 141 - 0
BuilderPattern/Director.cs

@@ -0,0 +1,141 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BuilderPattern
+{
+    /// <summary>
+    /// 指挥者(采购经理)
+    /// </summary>
+    public class Director
+    {
+        public void Construct(Builder builder)
+        {
+            builder.BuildMainFramePart();
+            builder.BuildScreenPart();
+            builder.BuildInputPart();
+        }
+    }
+
+    /// <summary>
+    /// 建造者(模拟装机过程)
+    /// 也可通过接口实现
+    /// </summary>
+    public abstract class Builder
+    {
+        /// <summary>
+        /// 组装主机
+        /// </summary>
+        public abstract void BuildMainFramePart();
+
+        /// <summary>
+        /// 组装显示器
+        /// </summary>
+        public abstract void BuildScreenPart();
+
+        /// <summary>
+        /// 组装输入设备(键鼠)
+        /// </summary>
+        public abstract void BuildInputPart();
+
+        /// <summary>
+        /// 获取组装电脑
+        /// </summary>
+        /// <returns></returns>
+        public abstract Computer GetComputer();
+    }
+
+    /// <summary>
+    /// 惠普电脑组装商
+    /// </summary>
+    public class HpBulider : Builder
+    {
+        Computer hp = new Computer() { Band = "惠普" };
+
+        public override void BuildMainFramePart()
+        {
+            hp.AssemblePart("主机");
+        }
+
+        public override void BuildScreenPart()
+        {
+            hp.AssemblePart("显示器");
+        }
+
+        public override void BuildInputPart()
+        {
+            hp.AssemblePart("键鼠");
+        }
+
+        public override Computer GetComputer()
+        {
+            return hp;
+        }
+    }
+
+    /// <summary>
+    /// 戴尔电脑组装商
+    /// </summary>
+    public class DellBulider : Builder
+    {
+        Computer dell = new Computer() { Band = "戴尔" };
+
+        public override void BuildMainFramePart()
+        {
+            dell.AssemblePart("主机");
+        }
+
+        public override void BuildScreenPart()
+        {
+            dell.AssemblePart("显示器");
+        }
+
+        public override void BuildInputPart()
+        {
+            dell.AssemblePart("键鼠");
+        }
+
+        public override Computer GetComputer()
+        {
+            return dell;
+        }
+    }
+
+    /// <summary>
+    /// 产品类
+    /// </summary>
+    public class Computer
+    {
+        /// <summary>
+        /// 品牌
+        /// </summary>
+        public string Band { get; set; }
+
+        /// <summary>
+        /// 电脑组件列表
+        /// </summary>
+        private List<string> assemblyParts = new List<string>();
+
+        /// <summary>
+        /// 组装部件
+        /// </summary>
+        /// <param name="partName">部件名称</param>
+        public void AssemblePart(string partName)
+        {
+            this.assemblyParts.Add(partName);
+        }
+
+        public void ShowProcess()
+        {
+            Console.WriteLine("开始组装『{0}』电脑:", Band);
+            foreach (var part in assemblyParts)
+            {
+                Console.WriteLine(string.Format("组装『{0}』;", part));
+            }
+
+            Console.WriteLine("组装『{0}』电脑完毕!", Band);
+        }
+    }
+}

+ 32 - 0
BuilderPattern/Program.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BuilderPattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            Director director = new Director();
+            HpBulider hpBuilder = new HpBulider();
+            DellBulider dellBuilder =new DellBulider();
+
+            //组装一批惠普电脑
+            director.Construct(hpBuilder);
+            Computer hp =  hpBuilder.GetComputer();
+            hp.ShowProcess();
+
+            Console.ReadLine();
+
+            //组装一批戴尔电脑
+            director.Construct(dellBuilder);
+            Computer dell = dellBuilder.GetComputer();
+            dell.ShowProcess();
+
+            Console.ReadLine();
+        }
+    }
+}

+ 36 - 0
BuilderPattern/Properties/AssemblyInfo.cs

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

+ 7 - 3
DesignPattern.sln

@@ -1,12 +1,12 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.25420.1
-MinimumVisualStudioVersion = 10.0.40219.1
+# Visual Studio 2012
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryPattern", "FactoryPattern\FactoryPattern.csproj", "{B6F92CD5-5347-4F36-96CB-2ED8B9F66C25}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingletonPattern", "SingletonPattern\SingletonPattern.csproj", "{1DC67A01-1D89-4D4B-A7E2-400B191FFB9D}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -21,6 +21,10 @@ Global
 		{1DC67A01-1D89-4D4B-A7E2-400B191FFB9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{1DC67A01-1D89-4D4B-A7E2-400B191FFB9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{1DC67A01-1D89-4D4B-A7E2-400B191FFB9D}.Release|Any CPU.Build.0 = Release|Any CPU
+		{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{F24EFFF3-8FE3-49C6-90E8-252AAB8A3332}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 4 - 0
SingletonPattern/GenericSingleton.cs

@@ -6,6 +6,10 @@ using System.Threading.Tasks;
 
 namespace SingletonPattern
 {
+    /// <summary>
+    /// 泛型单例模式的实现
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
     public class GenericSingleton<T> where T : class//,new ()
     {
         private static T instance;