Skip to content

Commit d414e04

Browse files
committed
抽象工厂
1 parent 732ce31 commit d414e04

20 files changed

Lines changed: 314 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactory", "AbstractFactory\AbstractFactory.csproj", "{D599DAF7-507A-43EE-9DEE-860C228A07DD}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{D599DAF7-507A-43EE-9DEE-860C228A07DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{D599DAF7-507A-43EE-9DEE-860C228A07DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{D599DAF7-507A-43EE-9DEE-860C228A07DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{D599DAF7-507A-43EE-9DEE-860C228A07DD}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal
11.5 KB
Binary file not shown.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{D599DAF7-507A-43EE-9DEE-860C228A07DD}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AbstractFactory</RootNamespace>
11+
<AssemblyName>AbstractFactory</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="Program.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="App.config" />
49+
</ItemGroup>
50+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
51+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
52+
Other similar extension points exist, see Microsoft.Common.targets.
53+
<Target Name="BeforeBuild">
54+
</Target>
55+
<Target Name="AfterBuild">
56+
</Target>
57+
-->
58+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
8+
namespace AbstractFactory
9+
{
10+
/// <summary>
11+
/// 抽象工厂:提供一个创建一系列相关或者相互依赖对象的接口,而毋须指定他们的具体的类。
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
BreadMaker breadMaker = null;
18+
PizzaMaker pizzaMaker = null;
19+
IFactory factory = null;
20+
factory = new BlackBreadFactory();
21+
breadMaker = factory.CreateBread();
22+
breadMaker.GetBread();
23+
24+
factory=new HoneyBreadFactory();
25+
pizzaMaker = factory.CreatePizza();
26+
pizzaMaker.GetPizza();
27+
28+
}
29+
}
30+
31+
#region 工厂
32+
33+
/// <summary>
34+
/// 工厂接口定义
35+
/// </summary>
36+
public interface IFactory
37+
{
38+
BreadMaker CreateBread();
39+
PizzaMaker CreatePizza();
40+
}
41+
42+
/// <summary>
43+
/// 不同的面包建立一个具体的工厂方法来实现这个接口
44+
/// </summary>
45+
public class BlackBreadFactory : IFactory
46+
{
47+
public BreadMaker CreateBread()
48+
{
49+
return new BlackBread();
50+
}
51+
52+
public PizzaMaker CreatePizza()
53+
{
54+
return new BlackPizza();
55+
}
56+
}
57+
58+
/// <summary>
59+
/// 不同的面包建立一个具体的工厂方法来实现这个接口
60+
/// </summary>
61+
public class HoneyBreadFactory : IFactory
62+
{
63+
public BreadMaker CreateBread()
64+
{
65+
return new HoneyBread();
66+
}
67+
68+
public PizzaMaker CreatePizza()
69+
{
70+
return new HoneyPizza();
71+
}
72+
}
73+
74+
/// <summary>
75+
/// 不同的面包建立一个具体的工厂方法来实现这个接口
76+
/// </summary>
77+
public class WhiteBreadFactory : IFactory
78+
{
79+
public BreadMaker CreateBread()
80+
{
81+
return new WhiteBread();
82+
}
83+
84+
public PizzaMaker CreatePizza()
85+
{
86+
return new WhitePizza();
87+
}
88+
}
89+
#endregion
90+
91+
#region 面包类
92+
public abstract class BreadMaker
93+
{
94+
public abstract void GetBread();
95+
}
96+
97+
/// <summary>
98+
/// 黑面包
99+
/// </summary>
100+
public class BlackBread : BreadMaker
101+
{
102+
public override void GetBread()
103+
{
104+
Console.WriteLine("烤出了黑面包!");
105+
}
106+
}
107+
108+
/// <summary>
109+
/// 蜂蜜面包
110+
/// </summary>
111+
public class HoneyBread : BreadMaker
112+
{
113+
114+
public override void GetBread()
115+
{
116+
Console.WriteLine("烤出了蜂蜜面包!");
117+
}
118+
}
119+
120+
/// <summary>
121+
/// 白面包
122+
/// </summary>
123+
public class WhiteBread : BreadMaker
124+
{
125+
public override void GetBread()
126+
{
127+
Console.WriteLine("烤出了白面包!");
128+
}
129+
}
130+
131+
#endregion
132+
133+
#region 披萨类
134+
135+
public abstract class PizzaMaker
136+
{
137+
public abstract void GetPizza();
138+
}
139+
140+
public class BlackPizza : PizzaMaker
141+
{
142+
public override void GetPizza()
143+
{
144+
Console.WriteLine("生产出了黑披萨");
145+
}
146+
}
147+
148+
public class HoneyPizza : PizzaMaker
149+
{
150+
public override void GetPizza()
151+
{
152+
Console.WriteLine("生产出了蜂蜜披萨");
153+
}
154+
}
155+
156+
public class WhitePizza : PizzaMaker
157+
{
158+
public override void GetPizza()
159+
{
160+
Console.WriteLine("生产出了白披萨");
161+
}
162+
}
163+
164+
#endregion
165+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的常规信息通过以下
6+
// 特性集控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("AbstractFactory")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("AbstractFactory")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 使此程序集中的类型
18+
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
19+
// 则将该类型上的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("5008c64c-4c21-40d1-a655-6f2a6655ad7f")]
24+
25+
// 程序集的版本信息由下面四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)