Pages

Sunday, November 13, 2011

Facade vs. factory

Factory is used when we want to take control of object instantiation, for example if our component works with a set of related or dependent objects we define an interface with CreateXXX() methods.This ensures we don't have "x = new XXX();" scattered through our code, but x = factory.CreateXXX(); - complete control of every created instance outside the component (which enables our component to work with different sets of classes). This is "Abstract Factory" creational pattern.

Facade is a "structural pattern" used to simplify some otherwise complex interfaces. For example, you make a list of all types of emails your applications will send (Welcome, ForgotPassword, NewComment) and create MailFacade class with a method for each email type. This way, client code has only "high level" knowledge about "email types", but not about how these are composed and sent.

No comments:

Post a Comment