KNOW: Design Pattern

Strategy Pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern, whereby an algorithm's behavior can be selected at runtime.

The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Wiki

KNOW: Design Pattern_第1张图片

Observer Pattern

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Wiki

KNOW: Design Pattern_第2张图片

KNOW: Design Pattern_第3张图片

Factory Pattern

Simple Factory

simple factory is not a Design Pattern; it's more of a programming idiom.

simple factory is called directly by the class which wants to create an object (the calling class is referred to as the "client").  The simple factory returns one of many different classes.  All the classes that a simple factory can return either inherit from the same parent class, or implement the same interface.

KNOW: Design Pattern_第4张图片

Factory Method Pattern

Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Below is a Factory Method because the children of “Creator” are responsible for implementing the “Create” method. Another key point is that the creator is returning onlyone object. The object could be one of several types, but the types all inherit from the same parent class.
KNOW: Design Pattern_第5张图片

Abstract Factory Pattern

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

An Abstract Factory is a class with several Factory methods.

KNOW: Design Pattern_第6张图片

Simple Factory vs. Factory Method Pattern vs. Abstract Factory Pattern

In common:

They are responsible for creating objects.

Factory Method Pattern:

The client expects an implementation of an interface or abstract class, but doesn't know exactly what concrete class the factory will return.

Abstract Factory Pattern:

Here, there is one more level of abstraction. The client does not even know what factory it's going to use. First, it gets a Factory and then it calls a Factory method. The following example will explain this.

Abstract Factory Pattern add one more level of abstraction on Factory Pattern. It's just a class which includes multiple Factory Method classes.

http://www.codeproject.com/Articles/35789/Understanding-Factory-Method-and-Abstract-Factory

http://corey.quickshiftconsulting.com/1/post/2009/5/first-post.html

Singleton Pattern

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.

KNOW: Design Pattern_第7张图片

C++ simple Implementation (MSDN): Only works for single thread program.

// Declaration
class Singleton {
public: 
    static Singleton* Instance();
protected: 
    Singleton();
private:
    static Singleton* _instance;
}

// Implementation (ch12 part 2 about static data member)
Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance() {
    if (_instance == 0) {
        _instance = new Singleton;
    }
    return _instance;
}

C++ Singleton Pattern: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern

Thread-safe for singleton pattern

http://www.codeproject.com/Articles/96942/Singleton-Design-Pattern-and-Thread-Safety

Deep Dive:

MySingleton * GetInstance()
{
      if (m_pOnlyOneInstance == NULL)
      {
            EnterCriticalSection();
            if (m_pOnlyOneInstance == NULL)
            // Solution 1 and 2 gaps addressed by moving
            // critical section block and by re-doing this check!
            {
                  m_pOnlyOneInstance = new MySingleton();
            }
            LeaveCriticalSection();
      }
      return m_pOnlyOneInstance;
}

Disadvantage of Singleton Pattern

http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons?page=1&tab=votes#tab-top

Delegation Pattern

wiki

In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is anInversion of Responsibility in which a helper object, known as adelegate, is given the responsibility to execute a task for thedelegator.

iOS Delegate Sample: http://blog.csdn.net/b_end_an/article/details/9351177

Composite Pattern

1. The composite pattern allows you to compose objects into tree structures to represent part-whole hierarchies.

2. Composite lets clients treat individual objects and compositions of objects uniformly.

Example: File and Folder

KNOW: Design Pattern_第8张图片

  • Component - Component is the abstraction for leafs and composites. It defines the interface that must be implemented by the objects in the composition. For example a file system resource defines move, copy, rename, and getSize methods for files and folders.
  • Leaf - Leafs are objects that have no children. They implement services described by the Component interface. For example a file object implements move, copy, rename, as well as getSize methods which are related to the Component interface.
  • Composite - A Composite stores child components in addition to implementing methods defined by the component interface. Composites implement methods defined in the Component interface by delegating to child components. In addition composites provide additional methods for adding, removing, as well as getting components.
  • Client - The client manipulates objects in the hierarchy using the component interface.

http://www.oodesign.com/composite-pattern.html

Model-View-Controller

KNOW: Design Pattern_第9张图片

Objective C definition: http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html



你可能感兴趣的:(!!!Interview,Topics)