jeffrey 9 лет назад
Родитель
Сommit
1b62bb85c9

+ 48 - 0
DecoratorPattern/AbstractHouse.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DecoratorPattern
+{
+    /// <summary>
+    /// 抽象房类
+    /// </summary>
+    public abstract class AbstractHouse
+    {
+        /// <summary>
+        /// 面积
+        /// </summary>
+        public double Area { get; set; }
+
+        /// <summary>
+        /// 规格
+        /// </summary>
+        public string Specification { get; set; }
+
+        /// <summary>
+        /// 价格
+        /// </summary>
+        public decimal Price { get; set; }
+
+        /// <summary>
+        /// 定义抽象方法--展示
+        /// </summary>
+        public abstract void Show();
+    }
+
+    /// <summary>
+    /// 未装修房 -- 毛坯房
+    /// </summary>
+    public class WithoutDecoratorHouse : AbstractHouse
+    {
+        /// <summary>
+        /// 毛坯房就做简要展示
+        /// </summary>
+        public override void Show()
+        {
+            Console.WriteLine(string.Format("该户型为{0}㎡,户型设计为{1},目前均价为{2}元/㎡。", this.Area, this.Specification, this.Price));
+        }
+    }
+}

+ 6 - 0
DecoratorPattern/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>

+ 57 - 0
DecoratorPattern/DecoratorHouse.cs

@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DecoratorPattern
+{
+    /// <summary>
+    /// 装修房
+    /// </summary>
+    public abstract class DecoratorHouse : AbstractHouse
+    {
+        private readonly AbstractHouse house;
+
+        public DecoratorHouse(AbstractHouse house)
+        {
+            this.house = house;
+        }
+        public override void Show()
+        {
+            this.house.Show();
+        }
+    }
+
+    /// <summary>
+    /// 装修房--样板房
+    /// </summary>
+    public class ModelHouse : DecoratorHouse
+    {
+        public ModelHouse(AbstractHouse house) : base(house)
+        {
+        }
+
+        /// <summary>
+        /// 展示样板房细节
+        /// </summary>
+        private void ShowDetail()
+        {
+            Console.WriteLine(@"
+* 首先,您看到的是我们大概5平方的简单实用的入户花园。
+* 样板间的整体按欧式风格装修,精致温馨。
+* 进门右看是我们的餐厨一体化设计,客厅与餐厅动线相连,扩大了整个的空间视野。
+* 与客厅无缝连接的是超大的观景阳台,东南朝向,阳光充沛。
+* 动静分离的设计,将客厅与卧室进行有效的分离,保证了私密性及舒适度。
+* 主卧的落地窗设计,提供了足够的室内的采光度。
+* 主卧旁边的是干湿分离的卫生间。
+* 再旁边就是两个紧挨的房间,可按居家情况设计为儿童房、老人房或书房。");
+        }
+
+        public override void Show()
+        {
+            base.Show();
+            ShowDetail();
+        }
+    }
+}

+ 62 - 0
DecoratorPattern/DecoratorPattern.csproj

@@ -0,0 +1,62 @@
+<?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>{AD530192-DE5C-469F-B2C0-9314CA5CA686}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>DecoratorPattern</RootNamespace>
+    <AssemblyName>DecoratorPattern</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="AbstractHouse.cs" />
+    <Compile Include="DecoratorHouse.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>

+ 42 - 0
DecoratorPattern/Program.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DecoratorPattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            Console.WriteLine("装饰模式:");
+            Console.WriteLine("-------------------------------------------------");
+
+            Console.WriteLine("先看毛坯房:");
+            //未经装修的毛坯房
+            var withoutDecoratorHouse = new WithoutDecoratorHouse()
+            {
+                Area = 80.0,
+                Specification="三室一厅一卫",
+                Price = 8000
+            };
+
+            withoutDecoratorHouse.Show();
+
+            Console.WriteLine("-------------------------------------------------");
+
+            Console.WriteLine("再看样板房:");
+
+            //对毛坯房进行装修
+            var decoratorHouse = new ModelHouse(withoutDecoratorHouse);
+
+            decoratorHouse.Show();
+            Console.WriteLine("-------------------------------------------------");
+
+
+            Console.ReadLine();
+
+        }
+    }
+}

+ 36 - 0
DecoratorPattern/Properties/AssemblyInfo.cs

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

+ 6 - 0
DesignPattern.sln

@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandPattern", "CommandPa
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "ChainofResponsibility\ChainOfResponsibility.csproj", "{E4271E2E-3478-432D-87A4-6542FD005DAD}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{AD530192-DE5C-469F-B2C0-9314CA5CA686}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -63,6 +65,10 @@ Global
 		{E4271E2E-3478-432D-87A4-6542FD005DAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{E4271E2E-3478-432D-87A4-6542FD005DAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{E4271E2E-3478-432D-87A4-6542FD005DAD}.Release|Any CPU.Build.0 = Release|Any CPU
+		{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{AD530192-DE5C-469F-B2C0-9314CA5CA686}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE