名称 | Factory Method |
结构 | |
意图 | 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。 |
适用性 |
|
example
1// Factory Method
2
3// Intent: "Define an interface for creating an object, but let subclasses
4// decide which class to instantiate. Factory Method lets a class defer
5// instantiation to subclasses".
6
7// For further information, read "Design Patterns", p107, Gamma et al.,
8// Addison-Wesley, ISBN:0-201-63361-2
9
10/* Notes:
11* This design pattern is often used in a framework architecture, which
12* contains a number of related classes inside the framework, and which
13* expects application developers to derive custom classes from them.
14* If the instantiation of some custom application classes needs to be
15* carried out inside framework code, then the Factory Method design
16* pattern is an ideal solution.
17*
18* The sample below is based on a document view architecture.
19* It shows the framework providing classes DPApplication and DPDocument
20* and the application providing MyApplication and MyDocument, derived
21* from the corresponding framework classes. Note how the construction
22* of MyDocument is initiated from inside the framework's DPApplication,
23* yet the framework was not written specifically for it.
24*
25* It could be extended for views and workspaces along the same lines.
26*/
27
28namespace FactoryMethod_DesignPattern
29{
30 using System;
31
32 // These two classes could be part of a framework,
33 // which we will call DP
34 // ===============================================
35
36 class DPDocument
37 {
38
39
40 }
41
42 abstract class DPApplication
43 {
44 protected DPDocument doc;
45
46 abstract public void CreateDocument();
47
48 public void ConstructObjects()
49 {
50
51 // Create objects as needed
52 // . . .
53
54 // including document
55 CreateDocument();
56 }
57 abstract public void Dump();
58 }
59
60 // These two classes could be part of an application
61 // =================================================
62
63 class MyApplication : DPApplication
64 {
65 override public void CreateDocument()
66 {
67 doc = new MyDocument();
68 }
69
70 override public void Dump()
71 {
72 Console.WriteLine("MyApplication exists");
73 }
74 }
75
76 class MyDocument : DPDocument
77 {
78
79 }
80
81 /// <summary>
82 /// Summary description for Client.
83 /// </summary>
84 public class Client
85 {
86 public static int Main(string[] args)
87 {
88 MyApplication myApplication = new MyApplication();
89
90 myApplication.ConstructObjects();
91
92 myApplication.Dump();
93
94 return 0;
95 }
96 }
97
2
3// Intent: "Define an interface for creating an object, but let subclasses
4// decide which class to instantiate. Factory Method lets a class defer
5// instantiation to subclasses".
6
7// For further information, read "Design Patterns", p107, Gamma et al.,
8// Addison-Wesley, ISBN:0-201-63361-2
9
10/* Notes:
11* This design pattern is often used in a framework architecture, which
12* contains a number of related classes inside the framework, and which
13* expects application developers to derive custom classes from them.
14* If the instantiation of some custom application classes needs to be
15* carried out inside framework code, then the Factory Method design
16* pattern is an ideal solution.
17*
18* The sample below is based on a document view architecture.
19* It shows the framework providing classes DPApplication and DPDocument
20* and the application providing MyApplication and MyDocument, derived
21* from the corresponding framework classes. Note how the construction
22* of MyDocument is initiated from inside the framework's DPApplication,
23* yet the framework was not written specifically for it.
24*
25* It could be extended for views and workspaces along the same lines.
26*/
27
28namespace FactoryMethod_DesignPattern
29{
30 using System;
31
32 // These two classes could be part of a framework,
33 // which we will call DP
34 // ===============================================
35
36 class DPDocument
37 {
38
39
40 }
41
42 abstract class DPApplication
43 {
44 protected DPDocument doc;
45
46 abstract public void CreateDocument();
47
48 public void ConstructObjects()
49 {
50
51 // Create objects as needed
52 // . . .
53
54 // including document
55 CreateDocument();
56 }
57 abstract public void Dump();
58 }
59
60 // These two classes could be part of an application
61 // =================================================
62
63 class MyApplication : DPApplication
64 {
65 override public void CreateDocument()
66 {
67 doc = new MyDocument();
68 }
69
70 override public void Dump()
71 {
72 Console.WriteLine("MyApplication exists");
73 }
74 }
75
76 class MyDocument : DPDocument
77 {
78
79 }
80
81 /// <summary>
82 /// Summary description for Client.
83 /// </summary>
84 public class Client
85 {
86 public static int Main(string[] args)
87 {
88 MyApplication myApplication = new MyApplication();
89
90 myApplication.ConstructObjects();
91
92 myApplication.Dump();
93
94 return 0;
95 }
96 }
97