The factory method pattern implementation of Java design mode is abstract factory?

Source: Internet
Author: User

The factory method pattern for Java Design patterns Editor:Shanli Java Research Organization2009-02-25 Source: IT168 website text tag: design mode Java

  "IT168 technical article"

First, the factory method (Factory) mode

The meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.

  II. model role and structure of factory methods

Abstract Factory (Creator) Role: Is the core of the factory method pattern, regardless of the application. Any factory class of objects created in the schema must implement this interface.

Specific factory (concrete Creator) Role: This is the specific factory class that implements the abstract factory interface, contains the logic that is closely related to the application, and is called by the application to create the Product object. There are two such roles in: Bulbcreator and Tubecreator.

Abstract product role: The super-type of the object created by the factory method pattern, which is the common parent or co-owned interface of the product object. In, this role is light.

Specific products (concrete product) Role: This role implements the interface defined by the abstract product role. A specific product has a specific factory-created, often one by one correspondence between them.

  

 Simple example of a city

1//Product Plant interface
2
3 public interface Plant {}
4
5//Specific product PLANTA,PLANTB
6
7 public class PlantA implements Plant {
8
9 public PlantA () {
10
System.out.println ("Create PlantA!");
12
13}
14
public void dosomething () {
16
System.out.println ("PlantA do something ...");
18
19}
20
21}
22
All public class Plantb implements Plant {
24
Public Plantb () {
26
System.out.println ("Create PLANTB!");
28
29}
30
to public void dosomething () {
32
System.out.println ("Plantb do something ...");
34
35}
36
37}
38
39//Product Fruit Interface
40
Interface public Fruit {}
42
43//Specific product FRUITA,FRUITB
44
public class Fruita implements Fruit {
46
Fruita public () {
48
System.out.println ("Create Fruita!");
50
51}
52
dosomething public void () {
54
System.out.println ("Fruita do something ...");
56
57}
58
59}
60
public class FRUITB implements Fruit {
62
FRUITB public () {
64
System.out.println ("Create FRUITB!");
66
67}
68
dosomething public void () {
70
System.out.println ("FRUITB do something ...");
72
73}
74
75}
76
77//Abstract Factory method
78
Interface Abstractfactory {
80
Bayi public Plant createplant ();
82
Fruit Createfruit ();
84
85}
86
87//Specific factory method
88
public class Factorya implements Abstractfactory {
90
Createplant public Plant () {
92
PlantA return new ();
94
95}
96
Fruit public Createfruit () {
98
Return to new Fruita ();
100
101}
102
103}
104
Factoryb public class implements Abstractfactory {
106
107 public Plant Createplant () {
108
109 return new PLANTB ();
110
111}
112
113 Public Fruit Createfruit () {
114
FRUITB return new ();
116
117}
118
119}
120
121

  Iv. Factory method model and simple factory model

The difference between the factory method pattern and the simple factory model is not very obvious. The core of the Factory method class is an abstract factory class, while the simple factory model places the core on a specific class.

The factory method pattern has an alias called the Polymorphism factory pattern because the specific factory class has a common interface, or a common abstract parent class.

When the system extension needs to add a new product object, just need to add a specific object and a specific factory object, the original factory objects do not need to make any changes, and do not need to modify the client, well in line with the "open-closed" principle. The simple factory model has to modify the factory method after adding the new Product object, the extensibility is not good.

The factory method model can evolve into a simple Factory mode after degradation.

The factory method mode (RPM) Implementation of Java design mode is abstract factory?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.