Skip to content

Commit

Permalink
Decorator Design Pattern
Browse files Browse the repository at this point in the history
Use Coffee Shop to illustrate the decorator design pattern
  • Loading branch information
hustwenchao committed Mar 28, 2016
1 parent d4d725e commit bab6383
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoffeeShop", "CoffeeShop\CoffeeShop.csproj", "{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/App.config
Original file line number Diff line number Diff line change
@@ -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>
20 changes: 20 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Beverage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoffeeShop
{
public abstract class Beverage
{
protected string description = "Unknown Beverage";

public string getDescription()
{
return description;
}

public abstract double cost();
}
}
17 changes: 17 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Beverages/DarkRoast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace CoffeeShop.Beverages
{
public class DarkRoast : Beverage
{
public DarkRoast()
{
description = "Dark Roast";
}

public override double cost()
{
return 0.89f;
}
}
}
22 changes: 22 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Beverages/Espresso.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoffeeShop.Beverages
{
public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}

public override double cost()
{
return 1.99f;
}

}
}
21 changes: 21 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Beverages/HouseBlend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoffeeShop.Beverages
{
public class HouseBlend : Beverage
{
public HouseBlend()
{
description = "HouseBlend";
}

public override double cost()
{
return 0.89f;
}
}
}
67 changes: 67 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/CoffeeShop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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>{34D4C17D-6FCD-4B84-AA6D-C81E4CA2B591}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CoffeeShop</RootNamespace>
<AssemblyName>CoffeeShop</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="Beverage.cs" />
<Compile Include="Beverages\DarkRoast.cs" />
<Compile Include="Beverages\HouseBlend.cs" />
<Compile Include="Condiments\Mocha.cs" />
<Compile Include="CondimentDecorator.cs" />
<Compile Include="Beverages\Espresso.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>
16 changes: 16 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/CondimentDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoffeeShop
{
public class CondimentDecorator : Beverage
{
public override double cost()
{
return 0.00f;
}
}
}
24 changes: 24 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Condiments/Mocha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoffeeShop.Condiments
{
public class Mocha : Beverage
{
Beverage beverage;

public Mocha(Beverage beverage)
{
this.beverage = beverage;
description = "Mocha";
}

public override double cost()
{
return 0.20f + beverage.cost();
}
}
}
24 changes: 24 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CoffeeShop.Beverages;
using CoffeeShop.Condiments;
using System;

namespace CoffeeShop
{
class Program
{
static void Main(string[] args)
{
Beverage beverage = new Espresso();
Console.WriteLine(beverage.getDescription()
+ " $" + beverage.cost());

Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
Console.WriteLine(beverage2.getDescription()
+ " $" + beverage2.cost());

Console.ReadLine();
}
}
}
36 changes: 36 additions & 0 deletions Decorator/CoffeeShop/CoffeeShop/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("CoffeeShop")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CoffeeShop")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

//将 ComVisible 设置为 false 将使此程序集中的类型
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("34d4c17d-6fcd-4b84-aa6d-c81e4ca2b591")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit bab6383

Please sign in to comment.