This article address: http://www.cnblogs.com/archimedes/p/java-abstract-factory-pattern.html, reprint please indicate source address.
Abstract Factory mode (alias: Package)
Provides an interface to create a series of (interdependent) objects without specifying their specific classes.
Overview
Abstract Factory method patterns can be used to design a system when the system is ready to provide a series of related objects to the user and does not want to create a coupling between user code and the class that creates the objects. The key to an abstract factory pattern is to define several abstract methods in an abstract class or interface that return an instance of a class, which allows its subclasses or classes that implement the interface to override these abstract methods to provide a series of related objects to the user.
Applicability
1. A system is independent of the creation, composition, and presentation of its products.
2. When a system is to be configured by one of multiple product families.
3. When you want to emphasize the design of a series of related product objects for joint use.
4. When you provide a Product class library and just want to display their interfaces instead of implementing them.
Participants
1.AbstractFactory declares an operation interface that creates an abstract product object.
2.ConcreteFactory implements the operation of creating a specific product object.
3.AbstractProduct declares an interface for a class of product objects.
4.ConcreteProduct defines a product object that will be created by the corresponding specific factory. Implements the Abstractproduct interface.
5.Client use only interfaces declared by the Abstractfactory and Abstractproduct classes
Structure and use of abstract factory pattern
The structure of the pattern consists of four roles:
• Abstract products (prodcut)
• Specific Products (concreteproduct)
• Abstract Factory (abstractfactory)
• Specific Factory (concretefactory)
UML Class diagrams for schemas
Actual combat part
Example 1: Establish a system that provides users with a suit (top + trousers) and a denim set (top + trousers).
Description and use of the structure of the pattern
1. Abstract (product):
Upperclothes.java
Public Abstract class upperclothes{ publicabstractint getchestsize (); Public Abstract int getheight (); Public Abstract String getName ();}
Trousers.java
Public Abstract class trousers{ publicabstractint getwaistsize (); Public Abstract int getheight (); Public Abstract String getName ();}
2. Specific products (concreteproduct) _1: Westernupperclothes.java
Public classWesternupperclothesextendsupperclothes{Private intchestsize; Private intheight; PrivateString name; Westernupperclothes (String name,intChestsize,intheight) { This. name=name; This. chestsize=chestsize; This. height=height; } Public intgetchestsize () {returnchestsize; } Public intgetheight () {returnheight; } PublicString GetName () {returnname; } }
2. Specific products (concreteproduct) _2: Cowboyupperclothes.java
Public classCowboyupperclothesextendsupperclothes{Private intchestsize; Private intheight; PrivateString name; Cowboyupperclothes (String name,intChestsize,intheight) { This. name=name; This. chestsize=chestsize; This. height=height; } Public intgetchestsize () {returnchestsize; } Public intgetheight () {returnheight; } PublicString GetName () {returnname; } }
2. Specific products (concreteproduct) _3: Westerntrousers.java
Public classWesterntrousersextendstrousers{Private intwaistsize; Private intheight; PrivateString name; Westerntrousers (String name,intWaistsize,intheight) { This. name=name; This. waistsize=waistsize; This. height=height; } Public intgetwaistsize () {returnwaistsize; } Public intgetheight () {returnheight; } PublicString GetName () {returnname; } }
2. Specific products (concreteproduct) _4: Cowboytrousers.java
Public classCowboytrousersextendstrousers{Private intwaistsize; Private intheight; PrivateString name; Cowboytrousers (String name,intWaistsize,intheight) { This. name=name; This. waistsize=waistsize; This. height=height; } Public intgetwaistsize () {returnwaistsize; } Public intgetheight () {returnheight; } PublicString GetName () {returnname; } }
3. Abstract Factory (abstractfactory): Clothesfactory.java
Public Abstract class clothesfactory{ publicabstract upperclothes createupperclothes (int chestsize,int height); Public Abstract Trousers createtrousers (int waistsize,int height);}
4. Specific Factory (concretefactory): Beijingclothesfactory.java
Public classBeijingclothesfactoryextendsClothesfactory { PublicUpperclothes Createupperclothes (intChestsize,intheight) { return NewWesternupperclothes ("Beijing brand suit Top", Chestsize,height); } PublicTrousers Createtrousers (intWaistsize,intheight) { return NewWesterntrousers ("Beijing Brand Suit Pants", Waistsize,height); }}
Shanghaiclothesfactory.java
Public classShanghaiclothesfactoryextendsClothesfactory { PublicUpperclothes Createupperclothes (intChestsize,intheight) { return NewWesternupperclothes ("Shanghai brand Denim Shirt", Chestsize,height); } PublicTrousers Createtrousers (intWaistsize,intheight) { return NewWesterntrousers ("Shanghai Brand Jeans", Waistsize,height); }}
5. Application _1: Shop.java
Public classshop{upperclothes cloth; Trousers Trouser; Public voidGivesuit (Clothesfactory Factory,intChestsize,intWaistsize,intheight) {Cloth=factory.createupperclothes (chestsize,height); Trouser=factory.createtrousers (waistsize,height); Showmess (); } Private voidshowmess () {System.out.println ("< set info >"); System.out.println (Cloth.getname ()+":"); System.out.print ("Bust:" +cloth.getchestsize ()); System.out.println ("Height:" +cloth.getheight ()); System.out.println (Trouser.getname ()+":"); System.out.print ("Waist:" +trouser.getwaistsize ()); System.out.println ("Height:" +trouser.getheight ()); }}
5. Application _2: Application.java
Public class application{ publicstaticvoid main (String args[]) { shop Shop =New shop (); Clothesfactory Factory=new beijingclothesfactory (); Shop.givesuit (Factory,110,82,170); Factory=new shanghaiclothesfactory (); Shop.givesuit (Factory,120,88,180);} }
Advantages of the abstract factory model
• Abstract Factory mode creates a series of related objects for the user to decouple the user from the class that created the objects.
• Use abstract Factory mode to easily configure a range of objects for your users. Users can get a set of related objects using different factories, and also avoid mixing objects in different series.
• In abstract Factory mode, a "specific factory" can be added at any time to provide users with a set of related objects.
Java design Pattern 2--abstract Factory mode (Factory)