Factory mode: I personally think it is mainly used to create n object instances in the same series. (Note: "the same series" indicates that all objects inherit from an abstract class or all implement an interface)
Example: (the instance is still from the webcast of instructor Li Jianzhong, but simplified)
A car manufacturer needs to test different models of cars produced by "a certain brand", so he writes a set of tests.ProgramWe will use Audi (Audi) as the brand here. For example, if A5 is tested, A6 may be deprecated after a while, And A7, a8... they are also under study...
How can we make this set of testing programs respond to changes in various models, instead of releasing a new version every time a new model of Audi comes out?
Looking for a change: only the model is changed, but the testing method of the same brand remains unchanged (for example, commercial vehicles usually focus on testing business practicality, and sports cars usually focus on testing engine performance, concept Cars mainly test energy consumption. Luxury cars focus on interior decoration ...), therefore, you only need to propose the same type of test method.
1 Using System;
2
3 Namespace Factorymethod
4 {
5 Class Program
6 {
7 Static Void Main ( String [] ARGs)
8 {
9 Audifactory F = New A5factory (); // Reflection can completely remove the dependency between the client program and the specific type, which is skipped here...
10 Audi = F. createaudi ();
11 A. Test ();
12 Console. readkey ();
13 }
14 }
15
16 # Region Interface/abstract
17 Public Abstract Class Audi
18 {
19 Public Abstract Void Test ();
20 }
21
22
23 Public Abstract Class Audifactory
24 {
25 Public Abstract Audi createaudi ();
26 }
27 # Endregion
28
29
30 # Region Implementation
31 Public Class A5factory: audifactory
32 {
33 Public Override Audi createaudi ()
34 {
35 Return New Audia5 ();
36 }
37 }
38
39 Public Class A6factory: audifactory
40 {
41 Public Override Audi createaudi ()
42 {
43 Return New Audia6 ();
44 }
45 }
46
47 Public Class Audia5: AUDI
48 {
49 Public Override Void Test ()
50 {
51 Console. writeline ( " Audi A5 test completed! " );
52 }
53 }
54
55 Public Class Audia6: AUDI
56 {
57 Public Override Void Test ()
58 {
59 Console. writeline ( " Audi A6 test complete! " );
60 }
61 }
62 # Endregion
63 }
64
Here is another example of the factory mode for a calculator (from the example in the big talk mode) http://files.cnblogs.com/yjmyzz/Calculator_FactoryMethod.rar