Structural model (a) Adapter mode (Adapter)

Motivation (Motivation)

In the software system, due to changes in the application environment, often we need to be "some of the existing object" on the application of the new environment, new environmental requirements but these interfaces are not satisfied with the existing objects.

How to deal with this "migration changes"? How to achieve both good use of existing objects, while new interfaces to meet the application environment required?

Intent (Intent)

Converting the interface of a class clients expect another interface. Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together.

Example application says Adapter

image

This is actually a call one delegate, was originally sent the request to MyStack, but MyStack actually delegated to list to deal with. MyStack here is actually Adapter (adapter objects), list that is Adaptee (adapted object), and is IStack customer expectations interface. Too direct, nothing to say.

Structure (Structure)

The adapter has two structures

-1 Object Adapter (more common)

image

Object adapter using a combination of the object program, and its relationship to the Adapter is Adaptee combination relation, i.e. in the above example list is MyStack and combination relation.

OO preferentially used in combination mode, combined mode is not applicable, consider inheritance. Because the combination mode more loosely coupled, tightly coupled and inheritance, any changes should lead to changes in the parent class subclass.

The above example is the object adapter.

-2, Class Adapter

image

The following example is based adapter.

image

Adapter inherits ArrayList, also inherited IStack interfaces, both in the ArrayList methods can be used, IStack the interface where the method can also be used, so you feel a little nondescript. This class violates the principle of class should have a single responsibility, it has both the responsibility ArrayList, but also responsibilities IStack, so this class adaptation is not very common, it is not recommended.

Note: If a method should be commissioned to two or more objects, or two or more classes need to delegate, the object adapter, only need to add several properties can be achieved inside the adapter.

image

For class adapter, because C # class can only be a single inheritance, it can not inherit from two or more than two classes, the class adapter will not be used here.

application:

实现一个对栈的操作,有一个IStact接口,里面有三个方法Push(进栈)、Pop(出栈)和GetTopItem(取最顶层元素),这个IStact接口将相当于上面的Target,想要实现进栈出栈的操作,如果自己去实现数据结构显得比较麻烦,在此可以将net提供的ArrayList类拿来一用,ArrayList类就是被适配的对象,相当于上面的Adaptee。在写一个适配类StactAdapter类完成功能就可以了。

/// <summary>
/// 栈的接口
/// </summary>
public interface IStack
{
    void Push(object item);
    void Pop();
    Object GetTopItem();
}
/// <summary>
/// 对象适配器
/// </summary>
public class StactAdapter : IStack
{
    ArrayList list;
    /// <summary>
    /// 构造函数中实例化ArrayList
    /// </summary>
    public StactAdapter()
    {
        list = new ArrayList();
    }
    /// <summary>
    /// 进栈
    /// </summary>
    /// <param name="item">压入栈的元素</param>
    public void Push(object item)
    {
        list.Add(item);
    }
    /// <summary>
    /// 出栈
    /// </summary>
    public void Pop()
    {
        list.RemoveAt(list.Count - 1);
    }
    /// <summary>
    /// 取最顶层的元素
    /// </summary>
    /// <returns></returns>
    public Object GetTopItem()
    {
        return list[list.Count - 1];
    }
}
/// <summary>
/// 客户调用
/// </summary>
public class App
{
    static void Main(string[] args)
    {
        IStack myStack = new StactAdapter();
        myStack.Push("oec2003");
        myStack.Push("oec2004");
        myStack.Push("oec2005");
        myStack.Pop();
        Console.WriteLine(myStack.GetTopItem());
    }
}

Several points Adapter Model

      Adapter mode is mainly used in "want to reuse some of the existing classes, but the interface and in the case of multiplexing environmental requirements inconsistent" reuse legacy code migration library is very useful.

      GoF23 that define the structure of the two modes Adapter: class object adapter and the adapter. But class adapter uses "multiple inheritance" of implementation, had a poor high coupling, it is generally not recommended. Object Adapter by way of "Object combination", more in line with the spirit of loosely coupled.

      Adapter pattern can be achieved is very flexible and not be confined to two structures GoF23 defined. For example, can the Adapter Mode "existing object" as a new method of parameter interfaces, adapted to achieve the purpose.

      Adapter model itself requires us to use the "Interface-oriented programming" style as much as possible, so as to be easily adapted at a later stage.

Guess you like

Origin www.cnblogs.com/springsnow/p/11303907.html