Ten most commonly used design patterns

来源:互联网 发布:christmas lights 知乎 编辑:程序博客网 时间:2024/05/14 09:41

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

   Only works when:
   1.use a subset of the system's capabilities
   2.interact with it in a particular way

   Variations:
   1.reduce the number of objects a client must work with
   2.provide new functions
   3.hide the system

2. The Adapter Pattern
   Convert the interface of a class into another interface that the clients expect.It lets classes work together that could not otherwise because of incompatible interfaces.

   When to use:
   Create a new interface for an object that does the right stuff but has the wrong interface.

   How to use:
   1.define the interface and then implement in derived classes
   2.the derived class contains the existing class which to be used but has wrong interface, then passes request made to it on through to that class
  
   It frees you from worrying about existing interfaces.

   Two types:
   1.Object Adapter pattern - it relies on one object containing another.
   2.Class Adapter pattern - implement useing multiple inheritance

   Difference: A Facade simplifies an interface while an Adapter converts the interface into a preexisting interface.

3. The Bridge Pattern
   De-couple an abstraction from its implementation so that the two can vary independently.
   -- This pattern is a bit hard to understand at first! But it is a challenging pattern to learn cause it is so powerful.
 
   *** implementation here means the objects that the abstract class and its derivations use to implement themselves with, not the derivations of the abstract class,which are called concrete classes.

   You cant always see the variations at the beginning of the problem.
   Bottom line: During requirements definition, explore for variations early and often!

   It is more useful to focus on the context of the pattern - the problem it is trying to solve.This lets you know when and why to use it.
   You can identify when to apply a pattern before knowing exactly how to implement it.
  
   The bridge pattern is useful when:
     You have an abstraction that has different implementations. It allows the abstraction and the implementation to vary independently of each other.

   Commonality/Variability analysis:
   Commonality - the search for common elements that helps us understand how family members are the same.Thus, the process of finding out how things are common defines the family in which these elements belong(where things vary).
                 It seeks structure that is unlikely to change over time.
   Variability - reveals how family members vary.It only makes sense within a given commonality.
                 It captures structure that is likely to change.

   It is suggested that using commonality/variability analysis as a primary tool in creating objects is a better approach than looking at just nouns and verbs.