java - How does Abstract Factory uses delegation -
the difference between abstract factory , factory design pattern abstractfactory pattern uses composition delegate responsibility of creating object class while factory design pattern uses inheritance , relies on derived class or sub class create object.
below typical example of abstract factory(http://www.oodesign.com/abstract-factory-pattern.html) 1 please explain me abstract factory using object composition?
abstract class abstractproducta{ public abstract void operationa1(); public abstract void operationa2(); } class producta1 extends abstractproducta{ producta1(string arg){ system.out.println("hello "+arg); } // implement code here public void operationa1() { }; public void operationa2() { }; } class producta2 extends abstractproducta{ producta2(string arg){ system.out.println("hello "+arg); } // implement code here public void operationa1() { }; public void operationa2() { }; } abstract class abstractproductb{ //public abstract void operationb1(); //public abstract void operationb2(); } class productb1 extends abstractproductb{ productb1(string arg){ system.out.println("hello "+arg); } // implement code here } class productb2 extends abstractproductb{ productb2(string arg){ system.out.println("hello "+arg); } // implement code here } abstract class abstractfactory{ abstract abstractproducta createproducta(); abstract abstractproductb createproductb(); } class concretefactory1 extends abstractfactory{ abstractproducta createproducta(){ return new producta1("producta1"); } abstractproductb createproductb(){ return new productb1("productb1"); } } class concretefactory2 extends abstractfactory{ abstractproducta createproducta(){ return new producta2("producta2"); } abstractproductb createproductb(){ return new productb2("productb2"); } }
as understood subclass's concretefactory1
, concretefactory1
returning object's client. , typically working factory class more 1 product.
where client code be
abstractfactory factory = new concretefactory2(); abstractproducta proda = factory.createproducta();
could 1 please explain me object composition/delegation happen in abstract factory?
lets take sentence , figure out is.
abstractfactory pattern uses composition delegate responsibility of creating object class
abstract factory can termed "factory of factory pattern". here 1 more class there, lets call factoryoffactory
creates/holds multiple factories depending on type of request , returns end product.
class factoryoffactory { enum type { p1, p2} public abstractproducta createproducta(type t) { switch(t) { case p1: return new concretefactory1().createproducta(); case p2: return new concretefactory2().createproducta(); .... } } public abstractproductb createproductb(type t) { switch(t) { case p1: return new concretefactory1().createproductb(); case p2: return new concretefactory2().createproductb(); .... } } }
the definition of composition is
composition special case of aggregation. in more specific manner, restricted aggregation called composition. when object contains other object, if contained object cannot exist without existence of container object, called composition.
here container factoryoffactory
, contained objects different implementation of factory classes such concretefactory1
, concretefactory2
etc. factoryoffactory
delegates request respective factory implementation depending on type
Comments
Post a Comment