Java basics-factory model, java infrastructure Factory

Source: Internet
Author: User

Java basics-factory model, java infrastructure Factory

Through learning, we can summarize the characteristics of the Java factory model in one sentence-create an object by creating a factory, without worrying about the details and complex processes such as whether the constructed object instance can be instantiated.

What about the factory model? Just like our transformation from labor-intensive society to technology-intensive society. For example, I used to create a table. I needed to prepare a table, from picking wood, cutting wood, shipping wood, designing tables, and creating tables, due to the extremely high labor cost and the time required to learn the technology, a carpenter has certain limitations in innovation. Later, people entered the industrial age, and the development of the manufacturing industry grew by leaps and bounds. In addition, they advocated refined division of labor. For example, a computer manufacturer knows how to install a computer and its components, and the computer manufacturer does not have to know the production details of each component. However, it can also make computers with superior performance (which is optimal.

I. Factory Model

The factory mode mainly provides a transitional interface for creating objects, so that the specific process of creating objects is blocked and isolated to improve flexibility.

2. There are three types of factory models

1. Simple Factory)

2. Factory Method)

3. Abstract Factory)

These three models are gradually abstracted from top to bottom and more general.

Factory method mode:

An abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple specific factory classes. Each factory class can only create an instance of a specific product class.

Abstract Factory mode:

Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. One abstract factory class can be derived from multiple specific factory classes; you can create multiple product instances for each specific factory class.

3. Simple factory Model
Create a factory (a function or a class method) to create a new object.

Code example:

1 public class Dog {2 private String name; 3 private String address; 4 5 void shout () {6 System. out. println ("Wang ~~ "); 7} 8 9 public String getName () {10 return name; 11} 12 13 public void setName (String name) {14 this. name = name; 15} 16 17 public String getAddress () {18 return address; 19} 20 21 public void setAddress (String address) {22 this. address = address; 23} 24} 25 26 class DogFactory {27 static public Dog getDog () {28 // return new Dog (); 29 Dog = new dog (); 30 dog. setAddress ("Hangzhou, Zhejiang"); 31 dog. setName ("M. zhou "); 32 return dog; 33} 34} 35 36 class Test2 {37 public static void main (String [] args) {38 Dog = DogFactory. getDog (); 39 Dog dog2 = DogFactory. getDog (); 40 Dog dog3 = DogFactory. getDog (); 41 42 System. out. println (dog = dog2); // false43} 44}

Iv. Factory method mode

The factory method mode removes the static attribute of the factory method in the simple factory mode so that it can be inherited by the quilt class. In this way, the pressure on the factory method in the simple factory mode can be shared by different factory subclass in the factory method mode.

Factory method mode composition:

1. Abstract Factory role:

The core of the factory method model, which has nothing to do with applications. It is an interface that must be implemented by a specific factory role or a parent class that must be inherited. In java, it is implemented by abstract classes or interfaces.

2. Specific factory roles:

It contains Code related to the specific business logic. An application is called to create the objects of a specific product.

3. Abstract Product role:

It is the parent class or implemented interface inherited by a specific product. In java, abstract classes or interfaces are generally used for implementation.

4. Specific product roles:

The object created by the specific factory role is the instance of this role. It is implemented by specific classes in java.

The factory method mode uses multiple sub-classes inherited from the abstract factory role to replace the "God class" in the simple factory mode ". In this way, the pressure on objects is shared. In this way, the structure becomes flexible. We have learned three main features of object-oriented, namely inheritance, encapsulation, and polymorphism, the factory model is an application that combines these three features, and the structure of the factory role is also in line with the open and closed principles.

The following is an example:

/** @ Author M. zhou * @ date 2017-7-1 * version 1.0 ** // create a computer manufacturing factory public interface MyFactory {public MyBattery createBattery (); // define create battery public MyDisplay createDisplay (); // define create monitor public MyMotherBoard createMotherBorad (); // define create motherboard public MyShell createShell (); // define create shell}
// Create a computer raw material factory to provide battery, display screen, main board and housing public class ComFactory implements MyFactory {@ Override public <MyBattery> MyBattery createBattery () {// TODO Auto-generated method stub return new SuperBattery;} @ Override public MyDisplay createDisplay () {// TODO Auto-generated method stub return new SuperDisplay ;} @ Override public MyMotherBoard createMotherBorad () {// TODO Auto-generated method stub return new SuperMotherBoard;} @ Override public MyShell createShell () {// TODO Auto-generated method stub return new SuperShell ;}}
// The interface for creating a computer assembly plant is only responsible for obtaining materials from raw materials and configuring them as new computers. For others, public interface ComAssemblyPlant {public Computer Assembley ();}
// New computer assembly plant (which can replace, upgrade parts, and manufacture new computers according to market demands) public class NewComputerFactory implements ComAssemblyPlant {MyFactory materialFactory; // raw material factory @ Override public Computer Assembley () {Computer computer = new Computer (); computer. setBattery (materialFactory. createBattery (); computer. setDisplay (materialFactory. createDisplay (); computer. setMotherBoard (materialFactory. createMotherBorad (); computer. setShell (materialFactory. createShell (); return computer ;}}

Related Article

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.