Abstract Factory mode:
This pattern provides an interface for creating a family of related or dependent objects without specifying a specific implementation class.
What problems to solve:
Abstract Factory mode allows customers to use the abstract interface to create a set of related products, customer class and factory class separation, customers need any product, only to the factory request, customers can obtain new products without modification. Thus, the customer is decoupled from the specific product.
Uml:
Code structure:
Code:
Define Abstract Products:
Package com.demo.factory.model;/** * Abstract Product */public abstract class Abstractbasefood { protected String kind; protected int num; protected float price; public float Totalprice () { return this.num * this.price; }}
Product Interface:
Package com.demo.factory.model;/** * Abstract Product Interface */public interface Ifood { void Printmessage ();}
Abstract specific product-beverage class:
Package com.demo.factory.model;/** * Abstract specific products-beverage */public abstract class Beverage extends Abstractbasefood Implements ifood{ @Override public void Printmessage () { System.out.println ("--" + This.kind + "drink, \ t price:" + This.price + ",. \ t Number:" + this.num + ", \ t total:" + this.totalprice ());} }
Abstract specific product-chickenwings class:
Package com.demo.factory.model;/** * Abstract specific products-chickenwings */public abstract class Chickenwings extends Abstractbasefood Implements Ifood { @Override public void Printmessage () { System.out.println ("--" + This.kind + "flavor chicken wings, \ t Price: "+ This.price +",. \ t Number: "+ this.num +", \ t total: "+ this.totalprice ());} }
Abstract specific product-frenchfries class:
Package com.demo.factory.model;/** * Abstract specific products-frenchfries */public abstract class Frenchfries extends Abstractbasefood Implements Ifood { @Override public void Printmessage () { System.out.println ("--" + This.kind + "flavored French fries, \ t Price: "+ This.price +",. \ t Number: "+ this.num +", \ t total: "+ this.totalprice ());} }
Abstract specific product-hamburg class:
Package com.demo.factory.model;/** * Abstract specific products-hamburg */public abstract class Hamburg extends Abstractbasefood implements I Food { @Override public void Printmessage () { System.out.println ("--" + This.kind + "flavor burger, \ t Price:" + This.pri Ce + ",. \ t Number:" + this.num + ", \ t total:" + this.totalprice ());} }
Specific product-chinabeverage class:
Package Com.demo.factory.model.kfc;import com.demo.factory.model.beverage;/** * Specific product-chinabeverage */public class Chinabeverage extends Beverage {public chinabeverage (int num) { this.kind = "Cola"; This.price = 8.0f; This.num = num; }}
Specific product-chinachickenwings class:
Package Com.demo.factory.model.kfc;import com.demo.factory.model.chickenwings;/** * Specific product-chinachickenwings */public Class Chinachickenwings extends Chickenwings {public chinachickenwings (int num) { this.kind = "Orleans"; This.price = 2.5f; This.num = num; }}
Specific product-chinafrenchfries class:
Package Com.demo.factory.model.kfc;import com.demo.factory.model.frenchfries;/** * Specific product-chinafrenchfries */public Class Chinafrenchfries extends Frenchfries {public chinafrenchfries (int num) { this.kind = "normal"; This.price = 8.0f; This.num = num; }}
Specific product-chinahamburg class:
Package Com.demo.factory.model.kfc;import com.demo.factory.model.hamburg;/** * Specific product-chinahumburg */public class Chinahamburg extends Hamburg {public Chinahamburg (int num) { this.kind = "spicy"; This.price = 14.0f; This.num = num; }}
Abstract Factory Interface:
Package Com.demo.factory.itf;import Com.demo.factory.model.beverage;import com.demo.factory.model.ChickenWings; Import Com.demo.factory.model.frenchfries;import com.demo.factory.model.hamburg;/** * Abstract Factory Interface */public interface ikfcfactory { Hamburg createhamburg (int num); chickenwings createchickenwings (int num); frenchfries createfrenchfries (int num); Beverage createbeverage (int num);}
Actual factory:
Package Com.demo.factory.itf;import Com.demo.factory.model.beverage;import com.demo.factory.model.ChickenWings; Import Com.demo.factory.model.frenchfries;import Com.demo.factory.model.hamburg;import Com.demo.factory.model.kfc.chinabeverage;import Com.demo.factory.model.kfc.chinachickenwings;import Com.demo.factory.model.kfc.chinafrenchfries;import com.demo.factory.model.kfc.chinahamburg;/** * Actual Factory */public Class Chinakfcfactory implements Ikfcfactory { @Override public Hamburg Createhamburg (int num) { return new Chinahamburg (num); } @Override public chickenwings createchickenwings (int num) { return new chinachickenwings (num); } @Override public frenchfries createfrenchfries (int num) { return new chinafrenchfries (num); } @Override public beverage createbeverage (int num) { return new chinabeverage (num); }
Customer class:
Package Com.demo.factory.custom;import Com.demo.factory.itf.ikfcfactory;import Com.demo.factory.model.Beverage; Import Com.demo.factory.model.chickenwings;import Com.demo.factory.model.frenchfries;import com.demo.factory.model.hamburg;/** * Client class */public class Customer {private Ikfcfactory kfcfactory; Public Customer (Ikfcfactory kfcfactory) {this.kfcfactory = kfcfactory; } public float Orderhamburg (int num) {Hamburg Hamburg = Kfcfactory.createhamburg (num); Hamburg.printmessage (); return Hamburg.totalprice (); } public float orderchickenwings (int num) {chickenwings chickenwings = kfcfactory.createchickenwings (num); Chickenwings.printmessage (); return Chickenwings.totalprice (); } public float orderfrenchfries (int num) {frenchfries frenchfries = kfcfactory.createfrenchfries (num); Frenchfries.printmessage (); return Frenchfries.totalprice (); } public float orderbeverage (int num) {BESVerage beverage = kfcfactory.createbeverage (num); Beverage.printmessage (); return Beverage.totalprice (); }}
Design patterns that little thing reading notes (2)----Abstract Factory mode