Design Pattern 4: Adapter Pattern)

Source: Internet
Author: User
Tags log log

In software systems, due to changes in the application environment, we often need to put "some existing objects" in the new environment for application, however, the interfaces required by the new environment are not met by these existing objects. What if we can use the good implementation of existing objects while meeting the interfaces required by the new application environment? This is the problem to be solved in the adapter mode.

Objective: to convert an interface of a class into another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

The following uses the logging program as an example to describe the Adapter mode. Assume that we want to record logs in software development, including database record logs DatabaseLog and text file record logs WriteLog. The existing Log interfaces are:

Class chart implementation:

                        

Public abstract class Log

{

Public abstract void WriteLog ();

}

 

Public class DatabaseLog: Log

{

Public override void WirteLog ()

{

Console. WriteLine ("called database log ");

}

}

Public class FileLog: Log

{

Public override void WirteLog ()

{

Console. WriteLine ("called File log ");

}

}

 

However, a new log interface needs to be introduced during the development process, but the new log interface is different from the previous one. This interface is as follows: public interface Ilog

{

Void Write ();

}

 

Since neither of the two interfaces can be modified, how can we adapt the original objects to the new interfaces?

There are actually two ways: one is the class adaptation mode and the other is the object adaptation mode. The class adaptation mode is implemented through the class inheritance mechanism, while the object adaptation mechanism is implemented through the combination of objects.

1. Implementation of the class image in the class Adaptation Mode:

                    

 

Specific implementation:

Public class DatabaseLogAdapter: DatabaseLog, Ilog

{

Public void Write ()

{

This. WirteLog ();

}

}

Public class FileLogAdapter: FileLog, Ilog

{

Public void Write ()

{

This. WirteLog ();

}

}

 

  1. Implementation class diagram of object Adaptation Mode:

                

Code implementation:

// Object adaptation

Public class LogAdapter: Ilog

{

Private Log;

Public LogAdapter (Log log)

{

This. log = log;

}

Public void Write ()

{

Log. WriteLog ();

}

}

The above two adaptation methods show that the class adaptation method is implemented through class inheritance, and it also has all the actions of interface ILog, these violate the single responsibility principle of the class in the object-oriented design principle, and the Object Adapter is implemented by means of object combination, it conforms to the object-oriented spirit, therefore, the object adaptation mode is recommended.

Implementation points:

  1. The Adapter mode is mainly used in scenarios where you want to reuse some existing classes, but the interfaces are inconsistent with the requirements of the reuse environment. It is useful in code reuse and class library migration.
  2. The Adapter mode has two forms of implementation structure: Object Adapter and class Adapter. However, the class Adapter adopts the "Multi-inheritance" implementation method, resulting in poor high coupling, the Object Adapter adopts the "Object combination" method, which is more loosely coupled.
  3. The Adapter mode requires us to use the "interface-oriented programming" style as much as possible, so that we can easily adapt to it later.

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.