-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactoryPattern.cs
More file actions
102 lines (91 loc) · 2.4 KB
/
Copy pathAbstractFactoryPattern.cs
File metadata and controls
102 lines (91 loc) · 2.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DesignMode
{
//file类族
public interface IFile { void Call(); }
public class Win_File : IFile
{
public void Call()
{
Console.WriteLine("this is win file");
}
}
public class IOS_File : IFile
{
public void Call()
{
Console.WriteLine("this is ios file");
}
}
public class Android_File : IFile
{
public void Call()
{
Console.WriteLine("this is android file");
}
}
// web类族
public interface IWeb { void Call(); }
public class Win_Web : IWeb
{
public void Call()
{
Console.WriteLine("this is win web");
}
}
public class IOS_Web : IWeb
{
public void Call()
{
Console.WriteLine("this is ios web");
}
}
public class Android_Web : IWeb
{
public void Call()
{
Console.WriteLine("this is android web");
}
}
//iweb,ifile类族没有直接联系,但有相同约束条件--平台,相同平台的对象的用一个工厂类创建
public interface AbstractFactory
{
IWeb CreateWeb();
IFile CreateFile();
}
public class AndroidFactory : AbstractFactory
{
public IFile CreateFile() { return new Android_File(); }
public IWeb CreateWeb() { return new Android_Web(); }
}
public class WinFactory : AbstractFactory
{
public IFile CreateFile() { return new Win_File(); }
public IWeb CreateWeb() { return new Win_Web(); }
}
public class IOSFactory : AbstractFactory
{
public IFile CreateFile() { return new IOS_File(); }
public IWeb CreateWeb() { return new IOS_Web(); }
}
//抽象工厂模式:提供创建相互关联的对象的统一接口
public class AbstractFactoryPattern
{
/*
static void Main(string[] args)
{
//上层不知道依赖具体类(android_file/android_web),只依赖接口ifile/iweb
AndroidFactory android = new AndroidFactory();
IFile file = android.CreateFile();
file.Call();
IWeb web = android.CreateWeb();
web.Call();
Console.ReadLine();
}
*/
}
}