vb.net - IOC , Class Factory, Open/Closed -


i have question ioc, factories, , open/closed principle.

consider, if will, following factory

public function podocument( _type string) ipodocument        dim d new podocument       if _type = "service"           d.header = new servicepoheader()          d.details = new servicepolineitems()       else if _type = "merchandise"           d.header = new merchandisepoheader()          d.details = new merchandisepolineitems()       end if       return d  end function  

this working me , can nicely have webpage show information heterogeneous collections.

my challenge today told me customer order service , merchandise together. come on, among have seen coming?

so write new set of providers handle added complexity, changed factory include case new type, i'm off , running.

however, have violated open/closed principle changing factory, released production. there way set not changing factory?

thanks in advance.

yes. simplest example case define factory class each _type, , name them servicefactory, merchandisefactory, etc, or put <podocumenttype("service")> etc on them.

then find factories (for example, using reflection), put them in dictionary(of string, ipodocumentfactory) , select correct 1 based on key.

in more complicated case, ipodocumentfactory interface may include cancreate() method in addition create(). can select factory on list based on opinion current situation.

note discovery , list resolution support provided out of box di frameworks such unity or autofac.


Comments