Head First Design Patterns - Facade Pattern

The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

A facade not only simplifies an interface, it decouples a client from a subsystem of components.

Facades and adapters may wrap multiple classes, but a facade's intent is to simplify, while an 

adapter's is to convert the interface to something different.

The principle of least knowledge - talk only to your immediate friends.

the principle tell us that we should only invoke methods that belong to:

  • The object itself
  • Objects passed in as a parameter to the method.
  • Any objects the method creates or instantiates
  • Any components of the object

Summarize:

  • when you need to use an existing class and its interface is not the one you need, use an adapter
  • when you need to simplify and unify a large interface or complex set of interfaces, use a facade.
  • An adapter changes an interface into one a client expects.
  • A facade decouples a client from a complex subsytem.
  • Implementing an adapter may require little work or a great deal of work depending on the size and complexity  of  the     target  interface.
  • Implementing a facade requires that we compose the facade with its subsystem and usedelegation to perform the work   of the facade.
posted @ 2012-04-24 16:35  qiangzhu  阅读(426)  评论(0编辑  收藏  举报