In the previous article, silk had a RMB factory. At that time, silk suddenly wanted to go to the United States for a lap.
Intent: provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
Applicability:
1. diaosi determines the amount
2. You can select the corresponding currency in different countries.
Let's first look at a class chart.
1. AbstractFactory
Declares an operation interface for creating abstract product objects.
2. ConcreteFactory
Create a specific product object.
3. AbstractProduct
Declare an interface for a type of product object.
4. ConcreteProduct
Define a product object that will be created by a specific factory. Implements the abstractProduct interface.
5. Client
Only interfaces declared by the AbstractFactory and AbstractProduct classes are used.
Specific implementation:
Package creatingtype. abstractfactory; public interface Money {/*** print banknote */public void printMoney ();}
Package creatingtype. abstractfactory; public interface MoneyFactory {// instantiate RMB public Money getRMB (); // instantiate USD public Money getUSD ();}
Package creatingtype. abstractfactory; public abstract class RMB implements Money {/*** print banknote */public abstract void printMoney ();}
Package creatingtype. abstractfactory; public abstract class USD implements Money {/*** print banknote */public abstract void printMoney ();}
package creatingtype.abstractfactory;public class HundredFactory implements MoneyFactory { public Money getRMB() { return new RMB100(); } public Money getUSD() { return new USD100(); }}
Package creatingtype. abstractfactory; public class RMB100 extends RMB {public void printMoney () {System. out. println ("You got a RMB 100 million ");}}
Package creatingtype. abstractfactory; public class USD100 extends RMB {public void printMoney () {System. out. println ("You earned $100 ");}}
package creatingtype.abstractfactory;public class FiftyFactory implements MoneyFactory { public Money getRMB() { return new RMB50(); } public Money getUSD() { return new USD50(); }}
Package creatingtype. abstractfactory; public class RMB50 extends USD {public void printMoney () {System. out. println ("You got 50 RMB a piece ");}}
Package creatingtype. abstractfactory; public class USD50 extends USD {public void printMoney () {System. out. println ("You earned $50 ");}}
Package creatingtype. abstractfactory; // Abstract Factory mode public class MainClass {// The Abstract Factory mode is the most pumping and generic factory mode in all forms. The abstract factory mode can provide an interface to the client // so that the client does not have to specify the product type with/body, allows you to create product pairs/images for multiple product families. Public static void main (String [] args) {MoneyFactory ohf = new HundredFactory (); Money rmb100 = ohf. getRMB (); rmb100.printMoney (); Money usd100 = ohf. getUSD (); usd100.printMoney (); MoneyFactory ff = new polictyfactory (); Money rmb50 = ff. getRMB (); rmb50.printMoney (); Money usd50 = ff. getUSD (); usd50.printMoney ();}}