The Factory mode of JAVA design mode

Source: Internet
Author: User


1. Why use Factory mode?

For example, if a drop driver is going to drive money, he will have to make a car, and then he can use the car to earn

Money; For the driver, the demand is very high, after all, the car is difficult to produce;

If the factory model is used, the driver needs only to know the car factory and go directly to the factory to pick up the car. thereby lowering

Lower the skill requirements of the driver.


2. What is the core nature of the factory model?

1. instantiate the object . Replace the new operation with a factory method

2, the choice implements the class, the creation object process carries on the unified management and the control; decouple the caller /client from the specific implementation


3, application scenario?

A, the GetInstance method of the calendar in the JDK

B. Getting the Connection object in JDBC

C, hibernate in Sessionfactory create session

D, spring in IOC container create management Bean Object


4, the classification of the factory model?

A, simple Factory mode

B, factory method mode

C, Abstract Factory mode


5. Simple Factory mode

5.1 Key points:

A, the simple Factory mode is also called the Static Factory mode, because the factory class usually uses the static method

Create different object instances by receiving different parameters

B, Extensibility: The Code of the factory class must be modified before it can be extended.


6 Factory Method mode

6.1 Key points:

A, each instance object, corresponds to a factory; Create an instance no longer centrally

B, Extensibility: can be extended by adding new factory classes without modifying the original code


7. Abstract Factory mode

7.1 Key points:

A. Combination of different instance objects (used to produce all products of different product families)

B, support to increase the product group, can not add new products;


7.2 Abstract Factory code example:

First, the definition of three product classes

Package Com.xingej.patterns.factory.abstractfactory;public interface Seat {void Say ();}    Class Lowseat implements Seat {@Override public void Say () {System.out.println ("----I am a low-level car seat OH-----");    }}class Highseat implements Seat {@Override public void Say () {SYSTEM.OUT.PRINTLN ("---I am an advanced car seat oh-----"); };}
package com.xingej.patterns.factory.abstractfactory;public  Interface wheel {    void showinfo ();} class lowwheel implements wheel {     @Override      Public void showinfo ()  {        system.out.println ("--- -I am a low-grade wheel OH----");    }}class highwheel implements wheel {      @Override     public void showinfo ()  {         SYSTEM.OUT.PRINTLN ("----I am a premium wheel oh-----");     }} 
Package Com.xingej.patterns.factory.abstractfactory;public interface Engine {void Show ();    Class Highengine implements engine {@Override public void Show () {System.out.println ("----I am an advanced engine oh----");    }}class Lowengine implements engine {@Override public void Show () {SYSTEM.OUT.PRINTLN ("---I am a low engine oh----"); }}


Second, define a batch of product group interfaces

Package com.xingej.patterns.factory.abstractfactory;/** * Defines a batch of product groups * * @author Erjun November 9, 2017 pm 1:49:33 */public Interf    Ace Carfactory {Engine buildengine ();    Seat buildseat (); Wheel Buildwheel ();}


Third, the realization of different product groups

package com.xingej.patterns.factory.abstractfactory;public  class highcarfactory implements carfactory {     @Override      public engine buildengine ()  {        return  new highengine ();    }     @Override      Public seat buildseat ()  {        return new  Highseat ();    }     @Override     public wheel  buildwheel ()  {        return new highwheel ();     }} 
Package Com.xingej.patterns.factory.abstractfactory;public class Lowcarfactory implements Carfactory {@Override pub    Lic Engine Buildengine () {return new lowengine ();    } @Override Public Seat buildseat () {return new lowseat ();    } @Override Public Wheel Buildwheel () {return new Lowwheel (); }}


Test:

package com.xingej.patterns.factory.abstractfactory;import  org.junit.test;public class abstractfactorytest {     @Test      public void testabstractfactory ()  {         Carfactory factory = new highcarfactory ();         Engine highengine = factory.buildengine ();         Highengine.show ();        seat highseat =  Factory.buildseat ();         highseat.say ();         wheel highwheel = factory.buildwheel ();         highwheel.showinfo ();     }} 

8. Summary:

1, simple factory, the creation of objects are implemented in a factory class, (generally by passing in different parameters, to return different objects)

2, factory method, to create a factory for each object, that is, the creation of objects are assigned to different factories to implement, not

In a factory in a centralized implementation;

3, abstract factory, in a factory, create a series of relationships between each other objects; For example, in an abstract notebook factory, you can

To create a motherboard, video card, processor, mouse, etc.


Simple factory, factory method mode

The code has been uploaded to Git.

Https://github.com/xej520/xingej-design-patterns



This article is from the "Xej Distributed Studio" blog, so be sure to keep this source http://xingej.blog.51cto.com/7912529/1981388

The Factory mode of JAVA design mode

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.