Traditional design mode (Seven) adapter mode (with appearance mode)

Source: Internet
Author: User
Tags apd

Remember the decorator pattern we talked about in the previous chapters, we wrapped them up and gave them new responsibilities. Now, for different purposes, wrap some objects: make their interface look like nothing else. This allows the interface of the class to be converted into the desired interface in the design so that different interfaces can be implemented.

In the project, the adapter pattern generally plays the role of facilitator.

1. The client invokes the adapter through the target interface to make a request to the adapter

2. Adapters use the adapter interface to convert the request to one or more calling interfaces of the person being fitted

3. The customer receives the result of the call, but does not realize that all of this is the adapter in the act of converting

There is also a special situation, in the event of a new system and old, the old part of the expectation of the old vendor interface, but we have used the new manufacturer's interface to write this part, this time what to do? It's confusing to use an adapter, where an unpackaged interface is being used. This situation requires creating a two-way adapter that supports both sides of the interface.

For the project on this section, let's choose the bank payment driver.

<summary>///Payment driver///</summary> public interface Paychanneldriver {void paycal
    
        Lback ();
           
    String Replacehtmlcode (String order_no, String order_create_time,string currency, String Extra, string goods_desc); ///<summary>///Bank Payment driver///</summary> public class Cmbpaychanneldriver:p aychanneldriver {public void Paycallback () {Console.WriteLine () {* * Cmbpaychanneldriv
        Er "); public string Replacehtmlcode (string order_no, String order_create_time, string currency, String extra, str
        ing Goods_desc) {return ""; 
    }///<summary>///third party payment driver///</summary> public class Otherpaychanneldriver
        {public void Otherpaycallback () {Console.WriteLine (' This is Otherpaychanneldriver '); public string Getreuqesturl (String ordEr_no, String order_create_time, String goods_desc) {return ""; }///<summary>///adapter///</summary> public class Paychanneldriveradapter:paych
        Anneldriver {otherpaychanneldriver OCD;
        Public Paychanneldriveradapter (Otherpaychanneldriver otherpcd) {this.ocd = OTHERPCD; public void Paycallback () {OCD.
        Otherpaycallback (); public string Replacehtmlcode (string order_no, String order_create_time,string currency, String extra, String go ODS_DESC) {return OCD.
        Getreuqesturl (Order_no, Order_create_time,goods_desc); }
     
    }

Test to see

Ordinary Bank drive
           paychanneldriver pd = new Cmbpaychanneldriver ();
           Third party payments do not support bank
           -driven otherpaychanneldriver od = new Otherpaychanneldriver ();
           Adapter
           paychanneldriver APD = new Paychanneldriveradapter (OD);
           Drive Support Adapter
           PD. Paycallback ();
           Apd. Paycallback ();

Summary: Adapter mode converts the interface of a class into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work seamlessly.

Design principle: When you are designing a system, regardless of any object, you should pay attention to what it interacts with, and notice how it interacts with these classes. Don't allow too many classes to be coupled so that you don't have to modify a part of the system to affect other parts. If many classes depend on each other, the system becomes a fragile system that costs a lot of maintenance and is too complex to be understood by others.

Now let's look at the pattern.

Can an adapter in adapter mode only encapsulate a single class? The job of adapter mode is to convert an excuse into another. While most of the adapter patterns take an example of an adapter being packaged by an adaptor, we all know that the world is actually much more complex and needs to have one adapter wrap multiple adapters. This involves the appearance pattern.

The appearance pattern provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use. Examples of the words will not repeat.

In a nutshell, the appearance mode makes the interface simpler based on the adapter pattern.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

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.