Initial knowledge of Java design pattern adapter pattern _java

Source: Internet
Author: User
Tags socket

Body

We know that the most important and difficult UI control in Android is the ListView list control, and you have to use the adapter adapter to use it flexibly, so I think it's necessary to learn about the adapter pattern in Java (no matter if you can use it later), After all, the Java language is a very important foundation for Android development.

Fully understand the adapter pattern, there is a lot of knowledge to learn, for example: Adapter mode has the class adapter pattern and the object's adapter pattern in two different forms. But as a beginner, I will simply learn about the introduction of the adapter mode, the future will continue to improve. Hope to struggle on the code farm children's shoes do not spit groove →_→

First, Adapter Introduction

• Converts the interface of a class into another interface that the customer expects. Adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces.
• Adapter patterns are very common in modern Java frameworks. This pattern applies to scenarios where you want to use an existing class, but the class does not meet the interface requirements, or you need to create a reusable class that does not have other classes that provide the appropriate interface.

ii. Examples of apples and oranges

The idea of an adapter can be illustrated by the following simple example. This example would make an orange "fit" into an apple. As shown in the following illustration:

As you can see in the lower part of the above illustration, the adapter contains an example of an orange and inherits the Apple class. The orange object is placed in the adapter, so the orange behaves like an apple. The corresponding logical diagram is as follows:

Three, the socket box plug Example

In the image above, we can use the middle adapter to make the right plug connect to the left side of the socket successfully.

Four, the code implementation of the plug adapter

/** Adapter Mode (Adapter): Converts the interface of a class into another interface that the customer expects.
Adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces.
    * * Class adapterdemo{public static void Main (string[] args) {//power supply a begins work powera Powera = new Poweraimpl ();
    Start (Powera);
    Powerb Powerb = new Powerbimpl ();
    Poweraadapter pa = new Poweraadapter (powerb);
  Start (PA); }//Definition method: Power supply a work public static void start (Powera powera) {System.out.println (".....
    Some repetitive code ... ");
    Powera.insert (); System.out.println (".....)
  Some repetitive code ... \ n "); }/** public static void Start (Powerb powerb) {System.out.println ("...).
    Some repetitive code ... ");
    Powerb.connect (); System.out.println (".....)
  Some repetitive code ... "); }////Define Adaptor class class Poweraadapter implements powera{private Powerb powerb;//Adapter Interface public Poweraadapter (Powe
  RB powerb) {this.powerb = Powerb;
  //Implementation Interface Powera, it is necessary to implement Powera inside the method public void Insert () {powerb.connect (); }/** power supply a interface */interface powera{public void Insert ();} class Poweraimpl implements Powera{public void Insert () {System.out.println ("Power a interface insertion, starting work"); }/** power supply B Interface/interface powerb{public void Connect (), class Powerbimpl implements powerb{public void Connect (
  {SYSTEM.OUT.PRINTLN ("power supply B interface is connected, start working");
 }
}

In this example, we want Powerb to call the code in the Start () method in Powera, and of course, we don't want to repeat 23 or 25 lines of code that are commented out. This is the time to use adapter mode.

The above code explains:

Line 30th: Start to define the adapter, but also the beginning of the core code;

33rd, 34 lines: Through the construction method will powerb pass in;

The 37th line of code: since it is the implementation of interface Powera, it is necessary to implement the Powera inside the method insert ();

Line 38th: In the Powera Insert () method, we call the Connect () method of the Powerb;

Next, the 10th, 11, 12 line of code means: in the new one powerb, we put it into the adapter Poweraadapter, start the adapter, and then Powerb will be executed 16th, 24, 18 lines of code.

Note: 16, 24, 18 This order is not wrong, because we in the adapter is the equivalent of 24 lines of code has been replaced for line 17th code.

The operation effect is as follows:


Similarly, if I also want to use Powera as a powerb, you can define an adapter Powerbadapter and implement a two-way adapter.

Five, small summary

The code annotated in line 23rd and 25 above represents a lot of repetitive code that does not conform to the object-oriented way of thinking. We now envision an example where our project is on the line and the customer is using it, but it adds some new requirements later. And object-oriented has an OO principle is: On the change to close (after the line, the code as far as possible not to modify, otherwise there may be a ripple effect, resulting in other calls here the method of the code may be a problem), to expand the open (their own definition of new methods, others have not called, we can certainly modify). At this point, we can use the adapter to reduce these duplicate code.

Vi. principles of OO design

• Interface-oriented programming (for abstract programming)
• Package Changes
• Multi-use combination, less inheritance
• Open to extension for modification closure

Personal feeling, these design principles, need to continue to deepen understanding in practice, here is not too much description of OH ~

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.