C language and design mode: adapter Mode

Source: Internet
Author: User

Definition: converts an interface of a class to another interface that the customer wants. The adapter mode allows classes that cannot work together due to interface incompatibility. Type: Structure Mode classification: Class adapter mode and Object Adapter mode: Class adapter mode matches the interface through multiple inheritance, while C #, VB.. NET, JAVA, and other languages do not support multiple inheritance. A class only allows one parent class. So we mainly introduce the Object Adapter mode. Class diagram: Code: [csharp] // Target class Target {public virtual void Request () {Console. WriteLine ("common Request! ") ;}} [Csharp] // Adaptee class Adaptee {public void SpecificRequest () {Console. WriteLine (" special request! ") ;}} [Csharp] // Adapter class Adapter: Target {private Adaptee adaptee = new Adaptee (); // create a private Adaptee object public override void Request () {adaptee. specificRequest () ;}} [csharp] // client code Target = new Adapter (); // instantiate the object target. request (); Console. read (); advantages and applicability: the two classes share the same or similar things and want to reuse these existing classes. However, different interfaces have different requirements from the Reuse Environment, when both parties are not easy to modify, you should consider using the adapter mode. The adapter mode allows the customer code to call the same interface in a unified manner, which is simpler, more direct, and more compact. Note: The adapter mode is used after software development or maintenance. 1. During the software design phase, different interfaces are prevented in advance so that no mismatch will occur. 2. If an interface inconsistency problem occurs, you should first consider rebuilding the unified interface in a timely manner; 3. Adaptation is considered only when the original design and Code cannot be changed.

Related Article

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.