Abstract Factory mode (dedicated to silk)

Source: Internet
Author: User

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 ();}}


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.