Pattern: simple implementation of adapter

Source: Internet
Author: User

In Android programming, various adapters are everywhere, and their complicated design is amazing!


Based on your own experience, let's talk about the adapter design mode.


Welcome!


An example of an image


Now you have a notebook on your hand and need to be charged. Otherwise, the last game that grabs the treasure may be ruined. What should I do?

It's easy. Find an adapter for charging!

As a result, the adapter has been found, and the power supply is ready for use. Now, we can try again!


This process is simple, but it contains the adapter mentioned today ).

If there is no adapter, you cannot directly connect the 220 V voltage to your favorite notebook ?!


An adapter is simply a tool that integrates existing resources for others' use.


Software Implementation


There are two specific implementation methods:Inheritance or aggregation(Can be said to be a delegate ).


Power. Java


package mark.zhang;public class Power {public void supply() {System.out.println("power is suppling voltage......"); }}

Iconvertor. Java

package mark.zhang;public interface IConvertor {public abstract void convert();}

PC. Java

package mark.zhang;public class PC {Adapter mAdapter;public void setAdapter(Adapter adapter) {mAdapter = adapter;mAdapter.convert();work();}public void work() {System.out.println("pc is working......");}}

Next, we can design the adapter.


AggregationAdapter. Java


package mark.zhang;public class Adapter implements IConvertor {Power power;public Adapter() {power = new Power();}@Overridepublic void convert() {power.supply();}}


Simple implementation of the power adapter.


Inheritance implementationAdapter. Java


package mark.zhang;public class Adapter extends Power implements IConvertor {@Overridepublic void convert() {supply();}}


The client calls client. Java


package mark.zhang;public class Client {public static void main(String[] args) {PC pc = new PC();Adapter a = new Adapter();pc.setAdapter(a);}}

It can be seen that the adapter implements the interface method in any way, and the purpose is to call the supply of power.


However, there are still many shortcomings in this simple implementation.


For example, the classes are too specific (relative to abstraction), the program is not flexible, and there is no idea of abstract (or interface) programming.


Fortunately, today we will only introduce the adapter, complex implementation, and I will talk about it later.









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.