Abstract Factory of design pattern

Source: Internet
Author: User

Abstraction Factory (abstract Factory)

Defined:

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.

Class Diagram:

  

  

Example:

  

Abstract classabstractproducta{ Public Abstract voidoperationA1 ();  Public Abstract voidoperationA2 ();}classProductA1 extends abstractproducta{ProductA1 (String Arg) {System. out. println ("Hello"+Arg); } //Implement the code here     Public voidoperationA1 () {};  Public voidoperationA2 () {};}classProductA2 extends abstractproducta{ProductA2 (String Arg) {System. out. println ("Hello"+Arg); } //Implement the code here     Public voidoperationA1 () {};  Public voidoperationA2 () {};}Abstract classabstractproductb{//Public abstract void operationB1 (); //Public abstract void operationB2 ();}classProductB1 extends abstractproductb{ProductB1 (String Arg) {System. out. println ("Hello"+Arg); } //Implement the code here}classProductB2 extends abstractproductb{ProductB2 (String Arg) {System. out. println ("Hello"+Arg); } //Implement the code here}Abstract classabstractfactory{Abstractabstractproducta createproducta (); AbstractABSTRACTPRODUCTB CREATEPRODUCTB ();}classConcreteFactory1 extends abstractfactory{abstractproducta createproducta () {return NewProductA1 ("ProductA1"); } ABSTRACTPRODUCTB Createproductb () {return NewProductB1 ("ProductB1"); }}classConcreteFactory2 extends abstractfactory{abstractproducta createproducta () {return NewProductA2 ("ProductA2"); } ABSTRACTPRODUCTB Createproductb () {return NewProductB2 ("ProductB2"); }}//Factory Creator-an Indirect The instantiating the factoriesclassfactorymaker{Private StaticAbstractfactory pf=NULL; Staticabstractfactory getfactory (String choice) {if(Choice.equals ("a") ) {PF=NewConcreteFactory1 (); }Else if(Choice.equals ("b") ) {PF=NewConcreteFactory2 (); } returnPF; }}//Client Public classclient{ Public Static voidMain (String args[]) {abstractfactory PF=factorymaker.getfactory ("a"); Abstractproducta Product=pf.createproducta (); //More function calls on product    }}

Scenario: An object family or a group of objects that have no relationships have the same constraints, and you can use an abstract factory.

Abstract Factory of design pattern

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.