Java design mode----Primary Adapter mode

Source: Internet
Author: User

Statement

Welcome reprint, but please keep the original source of the article →_→

Life One number: http://www.cnblogs.com/smyhvae/

Article Source: http://www.cnblogs.com/smyhvae/p/3930271.html

Contact information: [Email protected]

Body

We know that the most important and difficult UI control in Android is the ListView list control, and to be able to use it flexibly, you have to use the adapter adapter, so I think it is necessary to learn the adapter mode in Java (whether it can be used later), After all, the Java language is a very important foundation for Android development.

Fully understand the adapter mode, there is a lot of knowledge to learn, for example: Adapter mode has the class adapter mode and the object adapter mode two different forms. But as a beginner, I will simply learn the basics of the adapter mode, and will continue to improve. Hope to struggle in the yards on the road of children's shoes mo Spit Groove →_→


First, the adapter introduction:

    • Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
    • The adapter pattern is very common in modern Java frameworks. This pattern is useful for scenarios where you want to use an existing class, but that class does not conform to the requirements of the interface, or you need to create a reusable class that is compatible with other classes that do not provide the appropriate interface.

Ii. examples of apples and oranges:

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

In the lower half of the section, you can see that the adapter contains an orange instance 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:

Third, the socket box Plug example:

, we can make the right plug connected to the left outlet successfully through the middle adaptor.

Four, the plug adapter code implementation:

1 /**2 Adapter Mode (Adapter): Transforms the interface of one class into another interface that the customer wants. 3 the adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. 4 */5 classadapterdemo{6      Public Static voidMain (string[] args) {7         //power supply A starts to work8Powera Powera =NewPoweraimpl ();9 start (Powera); Ten powerb powerb = new  Powerbimpl ();  one Poweraadapter pa = new  poweraadapter (powerb);  Start (PA);  -     } -     //Definition method: Power supply a Works the      Public Static voidStart (Powera powera) { -System.out.println (".... Some duplicate code ... "); - Powera.insert (); -System.out.println (".... Some duplicate code ... \ n "); +     } -  +     /** A Public static void start (Powerb powerb) { at System.out.println (".... Some duplicated code ... "); - Powerb.connect (); - System.out.println (".... Some duplicated code ... "); -     } -     */ - }29  //define adapter class  - classPoweraadapterImplementspowera{ to     PrivatePowerb Powerb;//interfaces to be adapted +      -      PublicPoweraadapter (Powerb powerb) { the          This. Powerb =Powerb; *     }powera // Implementation of interface, it is necessary to implement Powera inside the method of the  public void  Insert () { powerb.connect ();    * + } A /** the power supply A interface + */ - Interfacepowera{ $      Public voidInsert (); $ } - classPoweraimplImplementspowera{ -      Public voidInsert () { theSystem.out.println ("Power a connector plugged in, starting to work"); -     }Wuyi } the /** - power supply B interface Wu */ - Interfacepowerb{ About      Public voidConnect (); $ } - classPowerbimplImplementspowerb{ -      Public voidConnect () { -System.out.println ("Power B interface is connected, start working"); A     } +}

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 the 23 or 25 lines of code that are commented out. You can use the adapter mode at this time.

The above code explains:

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

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

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

The 38th line of code: in the Powera Insert () method, we call the Connect () method of Powerb ;

Then, the 10th, 11, 12 lines of code means: in the new one powerb, we put it into the adapter poweraadapter inside, start the adapter, and then Powerb will go to execute the code 16, 24, the line.

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

The results are as follows:

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

Five, Small summary:

The code that is commented out in line 23rd and 25 above indicates that it is a lot of duplicated code and does not conform to the object-oriented way of thinking. We now envision an example where our project is on-line and the customer is using it, but it has added some new requirements later. Object-oriented has an OO principle is: To modify the closed (after the line, the code try not to modify, otherwise there may be a chain reaction, resulting in other calls here the code of the method may be problematic), open to the extension (their own definition of the new method, others have not yet called, we can certainly modify). At this point, we can use the adapter to reduce these duplicated code.

VI: OO design principles:

    • interface-oriented programming (for abstract programming)
    • Package changes
    • Multi-use combination, less inheritance
    • Open to extension for modification close

Personal feeling, these design principles, need to deepen the understanding in practice, here is not too much to describe the OH ~

Reference blog:

Http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

Http://www.importnew.com/6923.html

Java design mode----Primary 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.