Design mode-Adapter mode (Adapter)

Source: Internet
Author: User

What is adapter mode?

The adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces.

Simply put, it is the things that need to be in front of, do not use, and short time can not transform it, so we will find a way to adapt it. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.
code example:
<summary>///Define client-expected interface///</summary> public class Target {///<summary> Use virtual adornments so that subclasses can rewrite//</summary> public virtual void Request () {Console.wri        Teline ("This is a common request"); }}///<summary>///define the classes that need to be adapted////</summary> public class Adaptee {public void Spec        Ificrequest () {Console.WriteLine ("This is a special request."); }}///<summary>//define Adapter///</summary> public class Adapter:target {//Establish a private ad        Eptee Object Private Adaptee adaptee = new Adaptee (); <summary>/////By rewriting the request () method on the surface, it becomes the actual call to Specificrequest ()///</summary> public ov erride void Request () {adaptee.        Specificrequest (); }}//client code class program {static void Main (string[] args) {//For the client, the call is the target's RequeST () Target target = new Adapter (); Target.            Request ();        Console.read (); }    }



Applicable scenarios:1, the system needs to use the existing classes, and these classes of interfaces do not conform to the interface of the system.

2. Want to create a reusable class to work with some classes that are not much related to each other, including some that might be introduced in the future.

3, two classes do things the same or similar, but with different interfaces.

4, the old system development class has implemented some functions, but the client can only be accessed in the form of a different interface, but we do not want to manually change the original class.

5, the use of third-party components, component interface definitions and their own definition of the different, do not want to modify their own interfaces, but to use the functionality of the third-party component interface

Other design modes:

Design mode-agent mode (proxy)

Design pattern-Decorative mode (Decorator)

Design pattern--Dimitri Law (LOD/LKP)

Design Patterns-Dependency reversal principle

Design mode-open/Close principle (OCP)

Design Patterns-Single Responsibility principle (SRP)



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design mode-Adapter mode (Adapter)

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.