Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45440309
First, overview:
Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
second, why use
Factory mode is our most commonly used mode, the famous Jive Forum, on the use of a lot of Factory mode, Factory mode in Java program system can be said to be ubiquitous.
Why is Factory mode so often used? Because the factory pattern is equivalent to creating the instance object's new, we often have to build the instance object according to class classes, such as a a=new a () Factory mode is also used to create the instance object, so later new will be more than the mind, whether you can consider the utility Factory mode, although doing so, May do more work, but will give you more scalability and minimal modifications to your system
third, practicality
- A system is independent of the creation, composition, and presentation of its products.
- When a system is to be configured by one of multiple product families.
- When you want to emphasize the design of a series of related product objects for joint use.
- When you provide a Product class library and just want to display their interfaces instead of implementing them.
Iv. participants
- Abstractfactory declares an operation interface that creates an abstract product object.
- Concretefactory implements the operation of creating a specific product object.
- Abstractproduct declares an interface for a class of product objects.
- Concreteproduct defines a product object that will be created by the corresponding specific factory. Implements the Abstractproduct interface.
- Client only uses interfaces declared by the Abstractfactory and Abstractproduct classes
v. Class Diagram
Vi. Examples
- abstractfactory: defining abstract Engineering classes Ianimalfactory
Package com.lyz.design.abstractfactory;/** * This interface is identified in the class diagram * Abstractfactory abstract Factory * @author Liuyazhuang * */public INTERFAC E ianimalfactory { /** * Defines the method for creating an instance of the ICat interface * @return * /ICat createcat (); /** * Defines how to create an instance of an Idog interface * @return * /Idog Createdog ();}
- concretefactory: Create two implementation classes for the abstract factory class, Whiteanimalfactory and Blackanimalfactory
Package com.lyz.design.abstractfactory;/** * Ianimalfactory Abstract Factory Implementation class * @author Liuyazhuang * */public class Whiteanimalfactory implements Ianimalfactory {public ICat Createcat () { return new Whitecat (); Public Idog Createdog () { return new Whitedog ();} }
Package com.lyz.design.abstractfactory;/** * Ianimalfactory Abstract Factory Implementation class * @author Liuyazhuang */public class Blackanimalfactory implements Ianimalfactory {@Override public ICat Createcat () { return new Blackcat (); Public Idog Createdog () { return new Blackdog ();} }
- abstractproduct: define abstract product interfaces to be produced in an abstract factory icat and Idog
ABSTRACTPRODUCT * defined in the package com.lyz.design.abstractfactory;/** * Class Diagram * Products manufactured by specified factory * @author Liuyazhuang * */public interface ICat { /** * Definition Method * /void Eat ();}
ABSTRACTPRODUCT * defined in the package com.lyz.design.abstractfactory;/** * Class Diagram * Products manufactured by specified factory * @author Liuyazhuang * */public interface idog {/** * Definition Method * /void Eat ();}
- concreteproduct: Create a Product implementation class Blackcat, Blackdog, Whitecat, Whitedog
Package com.lyz.design.abstractfactory;/** * ICat Interface Implementation class * @author Liuyazhuang * */public class Blackcat implements ICat {@ Override public void Eat () { System.out.println ("The Black Cat is eating!"); }
Package com.lyz.design.abstractfactory;/** * Idog Implementation class * @author Liuyazhuang */public class Blackdog implements Idog {@Over Ride public void Eat () { System.out.println ("The Black Dog is eating"); }
Package com.lyz.design.abstractfactory;/** * ICAT Implementation class * @author Liuyazhuang * */public class Whitecat implements ICat {@Ov Erride public void Eat () { System.out.println ("The White Cat is eating!"); }
Package com.lyz.design.abstractfactory;/** * Idog Implementation class * @author Liuyazhuang * */public class Whitedog implements Idog {@Ov Erride public void Eat () { System.out.println ("The White Dog is eating!"); }
- Client: Defines a test class
Package com.lyz.design.abstractfactory;/** * Testing class * @author Liuyazhuang * */public class Test {public static void main (Stri Ng[] args) { ianimalfactory blackanimalfactory = new Blackanimalfactory (); ICat Blackcat = Blackanimalfactory.createcat (); Blackcat.eat (); Idog Blackdog = Blackanimalfactory.createdog (); Blackdog.eat (); Ianimalfactory whiteanimalfactory = new Whiteanimalfactory (); ICat Whitecat = Whiteanimalfactory.createcat (); Whitecat.eat (); Idog Whitedog = Whiteanimalfactory.createdog (); Whitedog.eat ();}}
The Black Cat is eating! The Black Dog is Eatingthe white cat is eating! The White Dog is eating!
Vii. SummaryThus, the factory method does provide a very flexible and powerful dynamic expansion mechanism for the system structure, so long as we change the specific factory method, the system can change the system function without a little change in other places .
On the Java design pattern--Abstract Factory mode (ABSTRACTFACTOTY)