Java design Mode Gof Factory mode

Source: Internet
Author: User

First, the factory model (Factory)

1. The separation of creators and callers is realized.

2. Application Scenario
①jdk in the Calendar of getinstance ();
Acquisition of the Connection object of ②JDBC;
③hibernate sessionfactory to create a Session object;
④spring the IOC container to create the management Bean object;
⑤xml Parse Documentbuilderfactory create parser object;
⑥ the newinstance () of the Class object in the reflection.

3. Classification:
① Simple Factory mode (also known as static Factory mode, often used)
1, used to produce the same hierarchical structure of any product;
2, for the new product, you need to modify the existing code.
3, do not meet the opening and closing principle (open to expansion, the modification is closed)

② Factory Method mode
1, used for the production of fixed products in the same grade structure;
2, support to increase any product.

③ Abstract Factory mode
1, for the production of different product families of all products;
2, the addition of new products (one), powerless;
3, support to increase the product

Second, the factory model code implementation

1. Simple Factory mode

1.1. Interface of simple Factory mode product: Car.java

Package Cn.com.zfc.gof01.factory.simplefactoy;

/**
*
* @title Car
* @describe Simple Factory mode product interface Car
* @author Zhang Fuchang
* @date April 4, 2017 8:44:42
*/
Public interface Car {
public abstract void run ();

}

1.2, simple Factory mode product interface Car implementation class: Audi.java

Package Cn.com.zfc.gof01.factory.simplefactoy;

/**
*
* @title Audi
* @describe Simple Factory mode product interface Car implementation class: Audi.java
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class Audi implements Car {

@Override
public void Run () {
System.out.println ("Audi is driving");
}

}

1.3, simple Factory mode product interface Car implementation class: Benz.java

Package Cn.com.zfc.gof01.factory.simplefactoy;

/**
*
* @title Benz
* @describe Simple Factory mode product interface Car implementation class: Benz.java
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class Benz implements Car {

@Override
public void Run () {
System.out.println ("Mercedes-Benz in the drive");
}

}

1.4, Simple Factory mode product interface Car implementation class: Bmw.java

Package Cn.com.zfc.gof01.factory.simplefactoy;

/**
*
* @title BMW
* @describe Simple Factory mode product interface Car implementation class BMW
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class BMW implements Car {

@Override
public void Run () {
System.out.println ("BMW is driving");
}

}

1.5. Factory type of simple Factory mode: Carfactory.java

Package Cn.com.zfc.gof01.factory.simplefactoy;

/**
*
* @title Carfactory
* @describe Simple Factory, general use of static method
* @author Zhang Fuchang
* @date April 4, 2017 8:50:08
*/
public class Carfactory {

Get examples of BMW
public static Car GETBMW () {
return new BMW ();
}

Get an instance of Benz
public static Car Getbenz () {
return new Benz ();
}

Get an example of Audi
public static Car Getaudi () {
return new Audi ();
}

}

1.6, testing without factory class premise, the creation of Audi, Benz, Bmw:TestNoFactory.java

Package cn.com.zfc.gof01.factory.simplefactoy.test;

Import Cn.com.zfc.gof01.factory.simplefactoy.Audi;
Import Cn.com.zfc.gof01.factory.simplefactoy.Benz;
Import CN.COM.ZFC.GOF01.FACTORY.SIMPLEFACTOY.BMW;
Import Cn.com.zfc.gof01.factory.simplefactoy.Car;

/**
*
* @title Testnofactory
* @describe no factory class, create Audi, Benz, BMW
* @author Zhang Fuchang
* @date April 4, 2017 8:52:37
*/
public class Testnofactory {
public static void Main (string[] args) {
Need to know the interface and its implementation class
Car C1 = new Audi ();
Car C2 = new Benz ();
Car C3 = New BMW ();
C1.run ();
C2.run ();
C3.run ();
}

}

1.7, the test in the factory class premise, the creation of Audi, Benz, Bmw:TestSimpleFactory.java

Package cn.com.zfc.gof01.factory.simplefactoy.test;

Import Cn.com.zfc.gof01.factory.simplefactoy.Car;
Import Cn.com.zfc.gof01.factory.simplefactoy.CarFactory;

/**
*
* @title Testsimplefactory
* @describe Test Simple Factory mode with factory class to create Audi, Benz, BMW
* @author Zhang Fuchang
* @date April 4, 2017 8:56:39
*/
public class Testsimplefactory {
public static void Main (string[] args) {
Need to know the interfaces and factory classes
Car C1 = Carfactory.getaudi ();
Car C2 = Carfactory.getbenz ();
Car C3 = CARFACTORY.GETBMW ();
C1.run ();
C2.run ();
C3.run ();
}
}

2, factory method mode

2.1. Interface of simple Factory mode product: Car.java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Car
* @describe Simple Factory mode product interface Car
* @author Zhang Fuchang
* @date April 4, 2017 8:44:42
*/
Public interface Car {
public abstract void run ();
}

2.2, factory method mode product Interface Car implementation class: Audi.java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Audi
* @describe Factory Method Mode Product Interface Car Realization Class Audi
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class Audi implements Car {

@Override
public void Run () {
System.out.println ("Audi is driving");
}

}

2.3, the factory method mode product Interface Car implementation class Benz

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Benz
* @describe Factory Method Mode product Interface Car implementation class: Benz.java
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class Benz implements Car {

@Override
public void Run () {
System.out.println ("Mercedes-Benz in the drive");
}

}

2.4, factory method mode product Interface Car implementation class: Bmw.java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title BMW
* @describe Factory Method Mode product Interface Car implementation class BMW
* @author Zhang Fuchang
* @date April 4, 2017 8:45:48
*/
public class BMW implements Car {

@Override
public void Run () {
System.out.println ("BMW is driving");
}

}

2.5 Factory interface of factory method mode: Carfactory.java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Carfactory
* Factory Interface for @describe factory method mode Carfactory
* @author Zhang Fuchang
* @date April 4, 2017 9:05:18
*/
Public interface Carfactory {
Car Getcar ();
}

2.6, factory method mode factory class, need to implement the factory interface: Audifactory. java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Audifactory
* Factory class @describe factory method mode, need to implement factory interface
* @author Zhang Fuchang
* @date April 4, 2017 9:07:08
*/
public class Audifactory implements Carfactory {

@Override
Public Car Getcar () {
return new Audi ();
}

}

2.7, factory method mode factory class, need to implement the factory interface: Benzfactory. java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Benzfactory
* Factory class @describe factory method mode, need to implement factory interface
* @author Zhang Fuchang
* @date April 4, 2017 9:07:08
*/
public class Benzfactory implements Carfactory {

@Override
Public Car Getcar () {
return new Benz ();
}

}

2.8, factory method mode factory class, need to implement the factory interface: Carfactory. java

Package Cn.com.zfc.gof01.factory.factorymethod;

/**
*
* @title Bmwfactory
* Factory class @describe factory method mode, need to implement factory interface
* @author Zhang Fuchang
* @date April 4, 2017 9:07:08
*/
public class Bmwfactory implements Carfactory {

@Override
Public Car Getcar () {
return new BMW ();
}

}

2.9. Test Factory method Mode: Testfactorymethod.java

Package cn.com.zfc.gof01.factory.factorymethod.test;

Import Cn.com.zfc.gof01.factory.factorymethod.AudiFactory;
Import Cn.com.zfc.gof01.factory.factorymethod.BenzFactory;
Import Cn.com.zfc.gof01.factory.factorymethod.BmwFactory;
Import Cn.com.zfc.gof01.factory.factorymethod.Car;

/**
*
* @title Testfactorymethod
* @describe Test Factory method mode
* @author Zhang Fuchang
* @date April 4, 2017 9:09:34
*/
public class Testfactorymethod {
public static void Main (string[] args) {
Need to know the interfaces and their respective factory classes
Car C1 = new Audifactory (). Getcar ();
Car C2 = new Benzfactory (). Getcar ();
Car C3 = New Bmwfactory (). Getcar ();
C1.run ();
C2.run ();
C3.run ();
}
}

3. Abstract Factory mode

3.1, the product interface of the abstract factory, Engine: Engine.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Engine
* @describe the product interface of the abstract factory, engine
* @author Zhang Fuchang
* @date April 4, 2017 9:27:21
*/
Public interface Engine {
Engine efficiency
void efficiency ();
}

3.2, abstract interface of the product interface, SEAT: Seat.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Seat
* @describe Abstract interface of the product interface, seat
* @author Zhang Fuchang
* @date April 4, 2017 9:38:21
*/
Public interface Seat {
Comfortable seating
void Comfort ();

}

3.3, the product interface of the abstract factory, tyre: Tyre.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Tyre
* @describe the product interface of the abstract factory, tyre
* @author Zhang Fuchang
* @date April 4, 2017 9:39:06
*/
Public interface Tyre {
Tire Wear
void Wear ();
}

3.4, abstract factory model of product realization class, Low-end engine: Lowengine.java

Package cn.com.zfc.gof01.factory.abstractfactory;
/**
*
* @title Lowengine
* @describe Abstract Factory mode Product Realization class, low-end engine
* @author Zhang Fuchang
* @date April 4, 2017 9:55:02
*/
public class Lowengine implements Engine {

@Override
public void efficiency () {
SYSTEM.OUT.PRINTLN ("Low-end engine efficiency");
}

}

3.5, abstract factory model of product realization class, High-end engine: Luxuryengine.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Luxuryengine
* @describe Abstract Factory mode product Realization class, high-end engine
* @author Zhang Fuchang
* @date April 4, 2017 9:48:05
*/
public class Luxuryengine implements Engine {

@Override
public void efficiency () {
System.out.println ("High-end engine efficiency");
}

}

3.6, abstract factory model of product realization class, Low-end seat: Lowseat.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Lowseat
* @describe Abstract Factory mode Product Realization class, low-end seat
* @author Zhang Fuchang
* @date April 4, 2017 9:55:32
*/
public class Lowseat implements Seat {

@Override
public void Comfort () {
SYSTEM.OUT.PRINTLN ("Low-end seat comfort");
}

}

3.7, abstract factory model of product realization class, High-end seat: Luxuryseat.java

Package cn.com.zfc.gof01.factory.abstractfactory;
/**
*
* @title Luxuryseat
* @describe Abstract Factory Model product realization class, high-end seating
* @author Zhang Fuchang
* @date April 4, 2017 9:55:49
*/
public class Luxuryseat implements Seat {

@Override
public void Comfort () {
System.out.println ("High-end seat comfort");
}

}

3.8, abstract factory model of product realization class, Low-end tires: Lowtyre.java

Package cn.com.zfc.gof01.factory.abstractfactory;
/**
*
* @title Lowtyre
* @describe Abstract Factory mode Product Realization class, low-end tires
* @author Zhang Fuchang
* @date April 4, 2017 9:55:40
*/
public class Lowtyre implements Tyre {

@Override
public void Wear () {
SYSTEM.OUT.PRINTLN ("Low-end tires are easy to wear");
}

}

3.9, abstract factory model of product realization class, High-end tires: Luxurytyre.java

Package cn.com.zfc.gof01.factory.abstractfactory;
/**
*
* @title Luxurytyre
* @describe Abstract Factory model of product realization class, high-end tires
* @author Zhang Fuchang
* @date April 4, 2017 9:55:55
*/
public class Luxurytyre implements Tyre {

@Override
public void Wear () {
System.out.println ("High-end tires are not easy to wear");
}

}

3.10, abstract Factory product Family Factory interface: Carfactory.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Carfactory
* Product Family Factory Interface @describe Abstract Factory
* @author Zhang Fuchang
* @date April 4, 2017 9:54:28
*/
Public interface Carfactory {
Engine Getengine ();

Seat getseat ();

Tyre Gettyre ();
}

3.11, abstract factory product Family Factory Interface Realization class, Low-end factory: Lowcarfactory.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Lowcarfactory
* @describe Abstract Factory Product Family factory interface implementation class, low-end factory
* @author Zhang Fuchang
* @date April 4, 2017 10:07:40
*/
public class Lowcarfactory implements Carfactory {

@Override
Public Engine Getengine () {
return new Lowengine ();
}

@Override
Public Seat getseat () {
return new Lowseat ();
}

@Override
Public Tyre Gettyre () {
return new Lowtyre ();
}

}

3.12, abstract Factory product Family factory interface implementation class, High-end factory: Luxurycarfactory.java

Package cn.com.zfc.gof01.factory.abstractfactory;

/**
*
* @title Luxurycarfactory
* @describe the product family of the abstract Factory interface implementation class, high-end factory
* @author Zhang Fuchang
* @date April 4, 2017 9:59:38
*/
public class Luxurycarfactory implements Carfactory {

@Override
Public Engine Getengine () {
return new Luxuryengine ();
}

@Override
Public Seat getseat () {
return new Luxuryseat ();
}

@Override
Public Tyre Gettyre () {
return new Luxurytyre ();
}

}

3.13. Test the Abstract Factory mode: Testabstractfactory.java

Package cn.com.zfc.gof01.factory.abstractfactory.test;

Import Cn.com.zfc.gof01.factory.abstractfactory.CarFactory;
Import Cn.com.zfc.gof01.factory.abstractfactory.Engine;
Import Cn.com.zfc.gof01.factory.abstractfactory.LuxuryCarFactory;

/**
*
* @title Testabstractfactory
* @describe Test Abstract Factory mode
* @author Zhang Fuchang
* @date April 4, 2017 10:18:39
*/
public class Testabstractfactory {
public static void Main (string[] args) {
Carfactory carfactory = new Luxurycarfactory ();
Engine engine = Carfactory.getengine ();
Engine.efficiency ();
}
}

Java design Mode Gof Factory mode

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.