forked from sheng-jie/Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
80 lines (68 loc) · 2.2 KB
/
Copy pathProgram.cs
File metadata and controls
80 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace FactoryPattern
{
class Program
{
//简单工厂:简单实用,但违反开放封闭;
//工厂方法:开放封闭,单一产品;
//抽象工厂:开放封闭,多个产品;
//反射工厂:可以最大限度的解耦。
static void Main(string[] args)
{
TestSimpleFactory();
TestFactoryMethod();
TestReflectFactory();
TestAbstractFactory();
}
/// <summary>
/// 测试简单工厂模式
/// </summary>
private static void TestSimpleFactory()
{
Console.WriteLine("简单工厂模式:");
var productA = SimpleFactory.Create(ProductEnum.ConcreateProductA);
productA.GetInfo();
Console.ReadLine();
}
/// <summary>
/// 测试工厂方法模式
/// </summary>
private static void TestFactoryMethod()
{
Console.WriteLine("工厂方法模式:");
IFactoryMethod factoryB =new ConcreateFactoryB();
var productB = factoryB.Create();
productB.GetInfo();
Console.ReadLine();
}
/// <summary>
/// 测试反射工厂模式
/// </summary>
private static void TestReflectFactory()
{
Console.WriteLine("反射工厂模式:");
var productB = ReflectFactory.Create("FactoryPattern.ConcreateCarB");
productB.GetInfo();
Console.ReadLine();
}
/// <summary>
/// 测试抽象工厂模式
/// </summary>
private static void TestAbstractFactory()
{
Console.WriteLine("抽象工厂模式:");
var bmwFactory = new BMWFactory();
bmwFactory.CreateCar().GetInfo();
bmwFactory.CreateBus().GetInfo();
var bydFactory = new BYDFactory();
bydFactory.CreateCar().GetInfo();
bydFactory.CreateBus().GetInfo();
Console.ReadLine();
}
}
}