Six adapter modes in Design Mode)

Source: Internet
Author: User
Overview

In order to meet the needs of actual projects, due to changes in the application environment, some existing objects need to be put into the new development environment, at this time, the new environment and the interface of this object cannot be directly connected, that is, they cannot know each other and exchange information. To meet this "change", how can we leverage the good implementation of existing objects, at the same time, the interface can meet the requirements of the new environment. At this time, the adapter mode is no better. Let's learn the adapter mode.

 

Purpose

Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together because the interface is incompatible.

 

Structure chart

 

 

 

Living instance 1

We all need to pump our bicycles. Now there are two kinds of bicycle ribbon ports, and the commonly used air duct is suitable for the masses. Sometimes it is not very suitable for us to ride, we will use an intermediate conversion device to compress the gas. In fact, this conversion device serves as an adapter.

Living instance 2

Power adapter

 

Applications in. net

In. net has a library that has been implemented and is a very important adapter, that is, dataadapter, The Adapter Between dataadapter user dataset and the data source for data retrieval and storage, dataadapter provides this adapter by ing fill and update. We want to obtain the data source SQL Server, orcle, access, DB2. These are different in the Organization. When we want to get a unified dataset, dataadapter is of good use, we don't have to pay attention to the data details of the database, so we can use the data flexibly.

 

Corresponding code
Class program {static void main (string [] ARGs) {Target target = new adapter (); target. request (); // for the client, the request () console of target is called. read () ;}} class target {Public Virtual void request () {console. writeline ("common request") ;}} class adaptee {public void specificrequest () {console. writeline ("special request");} class adapter: Target {// create a private adaptee object private adaptee = new adaptee (); Public override void request () {// calling request () actually calls specificrequest () adaptee. specificrequest ();}}

Result After running:

 

When using the adapter Mode

When you want to use an existing class, but if its interface, that is, its method is not the same as your requirements, that is, the two classes do the same thing or are similar, however, if you want to use different interfaces, you should consider using the adapter mode.

Adapter category: Class adapter and Object Adapter (commonly used)

 

For Class adapters:

 

1. Use a specific adapter class to match adaptee and taget. The result is that when we want to match a class and all its subclasses, the class adapter will not be competent.

2. Enable the adapter to redefine partial behavior of adaptee because the adapter is a subclass of adaptee.

3. Only one object is introduced, and no additional pointer is required to indirectly obtain adaptee.

 

For object adapters:

 

1. Allow an adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses (if any. The adapter can also add functions to all adaptee at a time.

2. making it difficult to redefine adaptee behavior. This requires generating a subclass of adaptee and making the adapter reference this subclass instead of referencing the adaptee itself.

Advantages

1. The client can call the same interface through the adapter. The client is more transparent, simple, and loosely coupled.

2. reuse existing classes to solve the inconsistency between existing classes and the reuse environment.

3. decouple the target and the adaptor class, and introduce the adapter to reuse the existing adaptor class without modifying the original code.

4. The same adapter can configure the adapter and its subclass to the target interface, and one adapter can adapt multiple different adaptors to the same target.

Disadvantages

It is troublesome to change the adapter.

Summary

In short, by using the adapter mode, you can fully enjoy the fun of migrating class libraries and reusing class libraries.

This article references online learning materials

Http://www.cnblogs.com/Terrylee/

 

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.