The factory is probably the simplest design pattern. It should be appropriate if you use it. Let's first look at its pattern diagram:
Simply put, the factory model separates "Products" from "product production" and abstracts "products. This forms a simple factory model. If you abstract the process of "product production", you will get the method factory model. If you have multiple products in the method factory model, an abstract factory is formed.
Let's take the above three examples:
1. Simple Factory
1 // Client
2 public class simplefactorytest {
3 Public static void main (string [] ARGs) throws nothisproduct {
4 product producta = simplefactory. createproduct (simplefactory. product_a );
5 product productb = simplefactory. createproduct (simplefactory. product_ B );
6 producta. Work ();
7 productb. Work ();
8}
9}
10 // define abstract Products
11 abstract class product {
12 public abstract void work ();
13}
14 // product
15 class producta extends product {
16 @ override
17 Public void work (){
18 system. Out. println ("producta is working! ");
19}
20}
21 // product B
22 class productb extends product {
23 @ override
24 public void work (){
25 system. Out. println ("productb is working! ");
26}
27}
28 // define an exception. This exception is thrown when this type of product cannot be found.
29 class nothisproduct extends exception {
30 public nothisproduct (string aname ){
31 super (aname );
32}
33}
34 // define the factory class
35 class simplefactory {
36 public static final int product_a = 0;
37 public static final int product_ B = 1;
38 public static product createproduct (INT type) throws nothisproduct {
39 if (product_a = type)
40 return New producta ();
41 else if (product_ B = type)
42 return New productb ();
43 else
44 throw new nothisproduct ("it has no this type:" + Type + "product ...");
45}
46}
2. Method Factory
1 /**
2 * in simple factory mode, all focus is placed on the factory class. Once a new product is added to the system, it must be modified.
3 * factory class, which does not meet the principle of opening/closing (for modification and opening to expansion); factory method mode provides an abstraction
4 * factory interface. Different product factory classes are implemented in different product structures to create products in this level.
5 * product, usually a specific factory creates a specific product. When adding a product, you only need to implement the new
6 * The factory should be able to meet the needs and comply with the principle of opening and closing
7 */
8 // Client
9 public class methodfactorytest {
10 public static void main (string [] ARGs ){
11 Product producta = new factorya (). createproduct ();
12 product productb = new factoryb (). createproduct ();
13 producta. Work ();
14 productb. Work ();
15}
16}
17 // define abstract Products
18 abstract class product {
19 public abstract void work ();
20}
21 // product
22 class producta extends product {
23 @ override
24 public void work (){
25 system. Out. println ("producta is working! ");
26}
27}
28 // product B
29 class productb extends product {
30 @ override
31 public void work (){
32 system. Out. println ("productb is working! ");
33}
34}
35
36 // define the factory interface to generate the product Interface
37 abstract class factory {
38 abstract product createproduct ();
39}
40 // factory of the active product
41 class factorya extends factory {
42 @ override
43 product createproduct (){
44 return New producta ();
45}
46}
47 // factory of the active product B
48 class factoryb extends factory {
49 @ override
50 product createproduct (){
51 return New productb ();
52}
53}
3. Abstract Factory
1 // Client
2 public class abstractfactorytest {
3
4 public static void main (string [] ARGs ){
5
6}
7}
8 // define abstract product
9 abstract class producta {
10 public abstract void work ();
11}
12 // specific product one
13 class productaone extends producta {
14 @ override
15 public void work (){
16 system. Out. println ("producta is working! ");
17}
18}
19 // specific product two
20 class productatwo extends producta {
21 @ override
22 public void work (){
23 system. Out. println ("productb is working! ");
24}
25}
26
27 // define abstract product B
28 abstract class productb {
29 public abstract void work ();
30}
31 // specific product one
32 class productbone extends productb {
33 @ override
34 public void work (){
35 system. Out. println ("producta is working! ");
36}
37}
38 // specific product two
39 class productbtwo extends productb {
40 @ override
41 public void work (){
42 system. Out. println ("productb is working! ");
43}
44}
45
46 // define the factory interface to generate the product Interface
47 abstract class factory {
48 abstract producta createproducta ();
49 abstract productb createproductb ();
50}
51 class factorya extends factory {
52 @ override
53 public producta createproducta (){
54 return New productaone ();
55}
56 @ override
57 public productb createproductb (){
58 return New productbone ();
59}
60}
61 class factoryb extends factory {
62 @ override
63 public producta createproducta (){
64 return New productatwo ();
65}
66 @ override
67 public productb createproductb (){
68 return New productbtwo ();
69}
70}