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