Design mode: Adapter mode

Source: Internet
Author: User

Original address: http://leihuang.org/2014/12/06/adapter/


Structural mode how to design the static structure between objects, how to complete the inheritance, implementation and dependency between objects, which is related to the system design is robust (robust): such as easy to understand, easy to maintain, easy to modify, low coupling and other issues. As the name of the Structural model, the pattern under its classification gives a variety of object relationship structures that can be applied in different situations.

    • Default Adapter Mode
    • Adapter mode
    • Bridge mode
    • Composite mode
    • Decorator mode
    • Facade mode
    • Flyweight mode
    • Proxy mode

What is an adapter?

For example, for example, programmers like to use Linux programming, but, we sometimes need to use QQ, and Linux under no QQ, how to do? So there is wine this thing. The wine here is an adapter (Wineadapter), and Linux is the adaptation source ( Linuxadaptee). Here we define the Windows platform as an interface (iwindows).

However, there are two implementations of the adapter pattern, one is the class adapter (adapter inheritance adaptation source), and the second is the object adapter (the adapter contains an object with an adaptation source)

Class Adapter

Iwindows interface

Public interface Iwindows {public    void coding ();    public void QQ ();

Linuxadaptee class

public class Linuxadaptee {public    void coding () {        System.out.println ("Print ' Hello World '");}    }

Wineadapter class

public class Wineadapter extends Linuxadaptee implements iwindows {public    void coding () {        super.coding ();        You can also define the encoding method in wine    }    @Override public    void qq () {        System.out.println ("QQ");    }

Object Adapter

The class structure diagram of the object adapter below

If implemented, only the Wineadapter adapter is different

public class Wineadapter implements Iwindows {    private linuxadaptee Linux = null;    Public Wineadapter (Linuxadaptee Linux) {        this.linux = Linux;    }    public void Coding () {        linux.coding ();    }    @Override public    void qq () {        System.out.println ("QQ");}    }

2014-12-06 22:11:17

Brave,happy,thanksgiving!


Design mode: Adapter mode

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.