oop - Factory design pattern location of switch statements -
i trying wrap head around usefulness of factory design pattern.
like many implementations of design pattern (ie http://msdn.microsoft.com/en-us/library/ee817667.aspx) there switch statement in main() probes string decide concretecomputerfactory create (which later sent computerassembler.assemble(computerfactory factory) method).
the way see problem: 1. user of factory (main()) "knows" concrete factory implementations definition of design pattern supposed hidden from. 2. whenever new concretecomputerfactory introduced, abstraction doesn't hold grounds! have go client (main()) add if/case statement.
proposition: move if/switch statement/s computerassembler. (this has slight problem computerassembler has 2 reasons change: a. overall stuff related creation b. adding new concretecomputerfactory. but: better in client (main))
i assume don't grasp idea yet. hear why problems specified incorrect, , why proposition isn't better idea
thanks :)
the benefit of factory design pattern not come @ factory creation time, when factory , products used. code creates factory knows concrete type creating. it's after factory has been created, when factory , products used via base interfaces instead of concrete types, see benefit of factory pattern. code operates on base interfaces can create , use number of different concrete classes without changing code.
for example:
main() { // point isn't hide concrete types @ factory creation time factory* fact = new concretefactorya(); f(fact); } void f(factory* fact) { // benefit of factory pattern found, // factory *used*, not created product* p = fact.createproduct(); p.dosomething(); useproduct(p); }
if replace concretefactorya concretefactoryb, code creates factory has know change. don't have change f()
in way. that's benefit comes in. once i've created factory, rest of code can accommodate different concrete types without changes.
Comments
Post a Comment