Different adapter model (design model three)

Foreword

Adapter mode, is a seemingly simple pattern, but also the most complex patterns.
View the role of the adapter directly see its evolution, perhaps easier to understand.

Start driving

Adapter roughly divided into two categories, one for the class adapter, the other for the object adapter.

//新接口
public interface ITarget {
    void Process();
}
//旧接口
public interface Iadapter
{
    void doSomeThing();
    void doOtherThing();
}
//旧接口的实现类
public class oldClass : Iadapter
{
    public void doOtherThing()
    {
        throw new NotImplementedException();
    }

    public void doSomeThing()
    {
        throw new NotImplementedException();
    }
}
//新的接口实现
public class Adapter : oldClass, ITarget
{
    public void Process()
    {
        doSomeThing();
        doOtherThing();
    }
}

Explanation:

class effect
ITarget A new demand interfaces
Iadapter The old interface
oldClass The old interface class
Adapter The new interface implementation class

As can be seen from the above, the new interface requirements, the need to call inside the old interface implementation. This is one of its scenarios.
It is easy to think of a question, why not just extend the old interface it? In fact, a more realistic scenario, look at this picture below.

Here, in this way, we can say that the adaptation of a polymorphic scene.
In this manner the adapter class considered, significant limitations exist, see below red part.

In the red section, circled oldclass, which means that we are only oldclass been adapted. If there are other Iadapter derived class, then it would be a huge structural problems, it is not recommended.
Object Adapter:

//新接口
public interface ITarget {
    void Process();
}
//旧接口
public interface Iadapter
{
    void doSomeThing();
    void doOtherThing();
}
//旧接口的实现类
public class oldClass : Iadapter
{
    public void doOtherThing()
    {
        throw new NotImplementedException();
    }

    public void doSomeThing()
    {
        throw new NotImplementedException();
    }
}
//新的接口实现
public class Adapter :  ITarget
{
    public Iadapter Iadapter;

    public Adapter(Iadapter Iadapter) {
        this.Iadapter = Iadapter;
    }
    public void Process()
    {
        Iadapter.doSomeThing();
        Iadapter.doOtherThing();
    }
}

See the following figure:

so we all fit under the category Iadapter achieve this interface, another benefit ratio adapter class is not multiple inheritance in high-level languages (diamond inheritance) can be achieved, but also more realistic the complexity of the situation a little bit.

For more complex object adapter is still useful, but very difficult to achieve class adapter.
transfer:

static void Main(string[] args)
{
    Iadapter adapter = new oldClass();
    ITarget target = new Adapter(adapter);
    target.Process();
}

uml 图

Paint, follow up on.

to sum up

Scene mode adapter is designed to fit new needs, new requirements can reuse the old interface or class.
Role: to retain existing class service offered, to provide an interface to the new requirements, to meet the new demand expectations

Guess you like

Origin www.cnblogs.com/aoximin/p/12081147.html