Design Mode series-adapter Mode

Source: Internet
Author: User
ArticleDirectory
    • I. Review of the previous Article
    • Ii. Summary
    • Iii. Outline
    • Iv. Features and Use Cases of the adapter Mode
    • 5. Classic Implementation of the adapter Mode
    • 6. other solutions in the adapter Mode
    • VII. Adapter mode usage Summary
    • 8. Series progress
    • IX. next announcement
I. Review of the previous Article

Through the simple explanation in the previous article, we know that the intention of the combination mode is to organize complex objects in the form of a tree structure through the relationship between the whole and the local, shielding the internal details of objects

External display is a unified way to operate objects, which is a means and method for us to process more complex objects. This article takes the query control as an example to explain how to query the components inside the control and how to operate internal groups.

Elements, including the handler for adding elements, deleting and processing corresponding events. Of course, the combination mode is far more powerful than these, and we will certainly be in some instances laterCodeThe combination mode is used. Combination

Mode if conditions permit, we try to use the combination mode to process complex objects, which is far more effective than the inherited objects.

Combination Mode-It emphasizes how to organize the structure between the whole and the local, and organize the relationship between the whole and the local through a tree structure.

Applicable scenarios of the combination mode:

1. We want to operate on a complex object and operate on a simple object inside the complex object. We can consider object combination.

2. When an object is composed of multiple simple objects and may be part of another object, the combination mode is a good choice.

Ii. Summary

This article will describe the Classic Mode-adapter mode in structural mode, which is also one of the frequently used models in projects. When developing a system, we may encounter various requirements, our requirements and existing systems

Integration, or directly developing a system, or modifying an existing system to meet the requirements of the existing requirements. Therefore, in every situation, we have our own solutions. Let's take a look at several

Possible situations:

 

Based on the above situations, we know that developing a new system has a high cost and time investment. If our old system is currently operating very stably and the investment cost is very high, we want to continue using the old system

Yes, but we want to meet the needs of the new system. What should we do at this time? A better way is to convert the old system to the new interface call form through the adapter to complete the adaptation service. Of course, this article also focuses on this idea.

The following describes some usage of the adapter mode. Of course, I may explain some common usage of the adapter mode and some special usage, I learned from everyone.

This article describes the following parts:

1. Use Cases of the adapter mode.

2. Classic Implementation of the adapter mode.

3. encapsulation of multiple adapters.

4. Other considerations.

Iii. Outline

A. Review in the previous article.

B. Summary.

C. Outline of this article.

D. Features and Use Cases of the adapter mode.

E. Classic Implementation of the adapter mode.

F. Other solutions in the adapter mode.

G. Adapter mode usage summary.

H. Series progress.

I. Coming soon.

Iv. Features of the adapter mode and use cases 4.1. Features of the adapter Mode

The main solution of the adapter mode is the interface type to be called, which cannot meet the needs of our new system. At this time, we need to configure the interfaces of the old system through the adapter, allows you to call new interfaces.

. For this requirement, we can use an adapter. Of course, if multiple interfaces need to be reconfigured, we need to provide an adapter for each interface to complete the conversion. Of course, the specific call process,

We can encapsulate it accordingly. Call the adapter in a common way to complete the adaptation service. Let's take a look at the adaptation process.

The basic conversion process of the adapter is provided.

4.2 adapter mode scenarios

Based on the introduction of the features of the above adapter, we will analyze several applicable scenarios of the adapter mode:

1. When using third-party class libraries or third-party APIs, we use adapter conversion to meet the needs of existing systems.

2. When our old system is integrated with the new system, we find that the data of the old system cannot meet the requirements of the new system. At this time, we may need an adapter to complete the call request.

3. Data is synchronized between different databases. (Here I only analyzeProgramFor implementation. There are many other methods [Database Synchronization]).

5. Classic Implementation of the adapter Mode

This section describes the typical implementation code of the adapter mode. Here we will describe the query service in the project. The old system provides a query service method query (); however, my new system defines the underlying data access service layer.

The getlist () method is used, and the returned result set is encapsulated into a generic form. We will provide the relevant sample code here, so that you can understand the use process. Of course, here we will give

OutputClass adapter and Object AdapterThe two implementation methods are described as follows:

1. Class Adapter

The following sample code is provided for the configuration Transfer Process of the interface described above:

Query service of the old system

Public class query: IQUERY
{
Public object query ()
{
Return new object ();
}
}

Query service interface of the new system:

Public interface iselect
{
Object getlist ();
}

Use the class adapter to complete the configuration change operation:

Public class queryadapter: Query, iselect
{
Public object getlist ()
{
Return base. Query ();
}
}

The specific call test code is as follows:

Class Program
{
Static void main (string [] ARGs)
{
Iselect adapter = new queryadapter ();

Object o = Adapter. getlist ();

System. Threading. thread. Sleep (10000 );

}
}

2. Object Adapter

Query service of the old system

Public class query: IQUERY
{
Public object query ()
{
Return new object ();
}
}

Query service interface of the new system:

Public interface iselect
{
Object getlist ();
}

Use the class adapter to complete the configuration change operation:

Public class queryadapter: iselect
{
Private query = new query ();

Public object getlist ()
{
Return query. Query ();
}

}

The specific call test code is as follows:

Class Program
{
Static void main (string [] ARGs)
{
Iselect adapter = new queryadapter ();

Object o = Adapter. getlist ();

System. Threading. thread. Sleep (10000 );

}
}

The above is a simple implementation of the classic adapter mode, of course, the above does not consider the versatility and other aspects of the content, just gives a rough implementation, of course, for the above adapter creation process, we can use

This section describes several implementation schemes in the Creation Mode for corresponding improvement.

6. other solutions in the adapter Mode

The above provides the classic Implementation of the adapter mode. For more information, see the other methods described in this article. We provide other evolution methods of the adapter mode.

6.1 encapsulation of batch adapters

In the project, we may need to encapsulate interfaces of a batch of old systems. In this case, we can configure the adapter through the configuration file.

For example, in the above query, I may encapsulate the query method of the XML file and the query method of the database 2 old query service, and then I am in the actual project, use configuration to select which query method I use

The query service is completed. parameters are input or configuration section information is used for control. Let's take a look at the relevant sample code:

The query service interface defined by the old system:

Public interface IQUERY
{
Object Query ();
}

XML query and database implementation:

Public class xmlquery: IQUERY
{
Public object query ()
{
Throw new notimplementedexception ();
}
}

Public class dbquery: IQUERY
{
Public object query ()
{
Throw new notimplementedexception ();
}
}

New query service interface:

Public interface iselect
{
Object getlist ();
}

Specific adapter code implementation:

Public class xmladapter: iselect
{
Private xmlquery query = new xmlquery ();
Public object getlist ()
{
Return query. Query ();
}
}

Public class dbadapter: iselect
{
Private dbquery query = new dbquery ();
Public object getlist ()
{
Return query. Query ();
}
}

The factory is responsible for creating the service objects of the old system interface.

Public class adapterfactory
{
Public static iselect createadapter ()
{
Return (iselect) activator. createinstance (type. GetType (""));
}
}

Specific call test code:

Class Program
{
Static void main (string [] ARGs)
{
Iselect select = adapterfactory. createadapter ();

Object o = select. getlist ();
}
}

6.2 universal adapter

The query service interface defined by the old system:

Public interface IQUERY
{
Object Query ();
}

XML query and database implementation:

Public class xmlquery: IQUERY
{
Public object query ()
{
Throw new notimplementedexception ();
}
}

Public class dbquery: IQUERY
{
Public object query ()
{
Throw new notimplementedexception ();
}
}

The factory is responsible for creating the service objects of the old system interface.

Public class oldfactory
{
Public static IQUERY createquery ()
{
Return (IQUERY) activator. createinstance (type. GetType (""));
}
}

New query service interface:

Public interface iselect
{
Object getlist ();
}

Specific General adapter code implementation:

Public class comadapter: iselect
{
IQUERY query = oldfactory. createquery ();

Public object getlist ()
{
Return query. Query ();
}
}

The adapter creation process is implemented through the factory to achieve decoupling:

Public class adapterfactory
{
Public static iselect createadapter ()
{
Return new comadapter ();

}
}

Specific call test code:

Class Program
{
Static void main (string [] ARGs)
{
Iselect select = adapterfactory. createadapter ();

Object o = select. getlist ();
}
}

VII. Adapter mode usage Summary

We have provided a simple explanation of the adapter mode. In fact, the adapter mode is still widely used. The above also analyzes the use scenarios and features of the adapter mode, in fact, this article does not show examples between adapters.

Conversion:

The above describes the more common way to call the adapter in 2, there are other ways, such as using the dictionary, and then using the adapter in the form of attribute indexes, I have provided some simple general functions.

There are still many better ways to use your ideas. I hope you can point out that you have learned from everyone. The conversion between the adapters is provided here. Sometimes, we find that the two adapters also need to be converted accordingly, for example

The Query Interfaces of the above databases and XML files, for example, sometimes, I need to convert the data in the XML file to the database, or save the data in the database as XML. At this time, we need to define a pair

To adapt the corresponding adapters.

The conversion from an adapter to an adapter is completed by converting to a container. Specific Code implementation is not provided here

The next article will show the implementation of this part when talking about the bridge mode. Please pay attention to it! I believe that, as described above, we have some images for this adapter mode, but we have not mentioned class adapters and object adapters above.

The differences between them are described below:

Object Adapter: Not through inheritance, but through object combination. As long as we have learned the OO design principles, we know that combination is a recommended method than inheritance.

Class adapter: It is implemented through inheritance to encapsulate the methods of the old system. During the conversion process between the adapters, the Object Adapter can undoubtedly be completed, but the dependency increases. As the adaptation requires flexibility, it may be difficult to control the expansion through inheritance.

Generally, the class adapter is less flexible and the Object Adapter is more flexible. We recommend that you use dependency injection or configuration. The class adapter needs to inherit from the class of the old system to be adapted, which is undoubtedly not a good method.

8. Series progress

Creation type

1. Design Mode of system architecture skills-one-piece Mode

2. Design Mode of system architecture skills-factory Mode

3. Design Mode of system architecture skills-Abstract Factory Mode

4. Design Mode of system architecture skills-creator Mode

5. Design Mode of system architecture skills-prototype mode

Structural

1. Design Mode of system architecture skills-Combination Mode

2. Design Mode of system architecture skills-appearance Mode

3. Design Mode of system architecture skills-adapter Mode

4. Design Mode of system architecture skills-Bridge Mode

5. Design Mode of system architecture skills-decoration Mode

6. Design Mode of system architecture skills-enjoy the Yuan Model

7. Design Mode of system architecture skills-Agent Mode

Behavior Type

1. Design Mode of system architecture skills-command mode

2. Design Mode of system architecture skills-Observer Mode

3. Design Mode of system architecture skills-Strategy Mode

4. Design Mode of system architecture skills-responsibility Mode

5. Design Mode of system architecture skills-template Mode

6. Design Mode of system architecture skills-intermediary Mode

7. Design Mode of system architecture skills-interpreter Mode

IX. next announcement

In the next article, we will describe the Bridge Mode in the structural mode. I think there should be fewer bridging modes in the project. We will combine some problems encountered in the project, in the reconstruction process, it is found that the bridge mode is used to better solve the problem.

Determine the dependency .! In the next article, we will explain in detail.

 

Download this demo:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.