Java Design pattern Application-Factory mode

Source: Internet
Author: User

There are three types of factory models: Simple Factory, factory method, abstract factory

First, the abstract factory

1, a factory can produce a variety of products;

2, do not change the factory can not produce new products.

 Packagecom.coshaho.learn.factory; Public classSimplefactory {//Absolute Alarm     Public classAbsolutealarm {}//Historical Value Alarm     Public classHistoryalarm {}//Simple Factory     Public classAlarmfactory { Publicabsolutealarm Createabsolutealarm () {return NewAbsolutealarm (); }                 Publichistoryalarm Createhistoryalarm () {return NewHistoryalarm (); }    }}

Second, the factory method

1, each factory can only produce one product;

2, want to produce new products only need to create a new factory;

3, too many products, may lead to too many factories.

 Packagecom.coshaho.learn.factory; Public classFactoryMethod {//Absolute Alarm     Public classAbsolutealarm {}//Historical Value Alarm     Public classHistoryalarm {}//Absolute Alarm Factory     Public classAbsolutealarmfactory { Publicabsolutealarm Createabsolutealarm () {return NewAbsolutealarm (); }    }        //Historical Value Alarm Factory     Public classHistoryalarmfactory { Publichistoryalarm Createhistoryalarm () {return NewHistoryalarm (); }    }}

Third, abstract factory

1, the abstract factory can produce a series of products;

2, product series in each category of products have a variety of models;

3. The concrete factory inheriting from the abstract factory can only produce one series products;

4, add a specific factory can add a product line;

5, do not change the abstract factory code can not increase or decrease product types in the product line.

 Packagecom.coshaho.learn.factory; Public classAbstractfactory {//Alarms     Public InterfaceAlarm {}//Absolute Alarm     Public classAbsolutealarmImplementsAlarm {}//Report     Public InterfaceReport {}//Excel Report     Public classExcelreportImplementsReport {}//Abstract Factory     Public InterfaceIfactory { PublicAlarm createalarm ();  PublicReport CreateReport (); }        //Specific Factory     Public classFactory { PublicAlarm Createalarm () {return NewAbsolutealarm (); }         PublicReport CreateReport () {return NewExcelreport (); }    }}

Java Design pattern Application-Factory mode

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.