Android design mode: adapter mode and android Design Mode

Source: Internet
Author: User

Android design mode: adapter mode and android Design Mode

The adapter mode is defined to convert an interface of a class into another interface that the customer expects. The adapter allows classes that are incompatible with the original interface to do nothing. Let's take a look at the two pictures below to make it clearer.



The adapter can implement interface decoupling. If the manufacturer wants to change the interface after a period of time, the adapter can encapsulate the changed part and the user does not need to change with the interface, you only need to change the adapter.

There is a need: There is a duck and a turkey, I want to use a turkey object to pretend to be a duck, but the turkey and duck methods are not exactly the same, then a converter is needed

Duck. java Duck Interface

package com.dzt.adapter;public interface Duck {public void quack();public void fly();}
Turkey. java Turkey Interface

package com.dzt.adapter;public interface Turkey {public void gobble();public void fly();}
Implement a duck class MallardDuck. java
package com.dzt.adapter;public class MallardDuck implements Duck {@Overridepublic void quack() {// TODO Auto-generated method stubSystem.out.println("MallardDuck------------------>quack");}@Overridepublic void fly() {// TODO Auto-generated method stubSystem.out.println("MallardDuck------------------>fly");}}
Implement a turkey-type WildTurkey. java

package com.dzt.adapter;public class WildTurkey implements Turkey {@Overridepublic void gobble() {// TODO Auto-generated method stubSystem.out.println("WildTurkey------------------>gobble");}@Overridepublic void fly() {// TODO Auto-generated method stubSystem.out.println("WildTurkey------------------>fly");}}
To convert a Turkey into a duck, you need to create an adapter TurkeyAdapter. java

Package com. dzt. adapter;/*** converts a Turkey to a Duck ** @ author Administrator **/public class TurkeyAdapter implements Duck {turkey Turkey; public TurkeyAdapter (turkey Turkey) {this. turkey = turkey;} @ Overridepublic void quack () {// TODO Auto-generated method stubturkey. gobble () ;}@ Overridepublic void fly () {// TODO Auto-generated method stubfor (int I = 0; I <5; I ++) turkey. fly ();}}
To convert a turkey to a duck, you need to implement the duck interface, which has the same method as the duck implementation class and pass in the object to be adapted (Turkey)

Test code

Package com. dzt. adapter;/*** adapter mode requirement: Because the proper duck cannot be found, you can only temporarily enter the Turkey to replace the duck, but the method for duck and Turkey is different, in this case, a "man-in-the-middle" is required for conversion. ** @ Author Administrator * @ data 2014.08.19 */public class AdapterModel1 {public static void main (String [] args) {WildTurkey wildTurkey = new WildTurkey (); mallardDuck mallardDuck = new MallardDuck (); Duck adapter = new TurkeyAdapter (wildTurkey); System. out. println ("turkey say .................. "); wildTurkey. gobble (); wildTurkey. fly (); System. out. println ("duck say .................. "); mallardDuck. quack (); mallardDuck. fly (); System. out. println ("adapter say .................. "); adapter. quack (); adapter. fly (); System. out. println ("-------------------------------------------------");}}
In this way, the method in the duck class can be used in the adapter. This mode can reduce the coupling between the customer and the system, and facilitate the maintainability of the program.

Code: http://download.csdn.net/detail/deng0zhaotai/7835473

Reference books: Head First design patterns


What is the adapter design mode?

Adapter Pattern converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. It is also called the converter mode, transformer mode, and Wrapper mode (package existing classes so that they can have interfaces as needed ).
There are two adapter modes:
(1) Object Adapter mode-in this adapter mode, the adapter holds an instance of the class it wraps. In this case, the adapter calls the physical entity of the wrapped object.
(2) Class adapter mode-in this adapter mode, the adapter inherits its own implemented class.
Regardless of the adapter, it aims to: retain the services provided by the existing class and provide interfaces to the customer to meet the customer's expectations. That is, it provides new interface services without changing the original system.

We recommend you to read an authoritative book on design patterns: "software secrets-the point of design patterns" Edited by Zheng AQI. The descriptions are in place, and the instances are easy to understand!

I wish you an early learning of the design model!

The brother helped me sort the Adapter in the java design mode)

For example, public interface ClassA
{
Public void methodA ();

Public void methodB ();

Public void methodB ();

}
For example, ClassB only wants to be interested in methodA in ClassA. In this case, use the Adapter
That is:
Public class ClassC implent ClassA
{
Public void methodA ();
Public void methodB ();
Public void methodB ();
}

Public class ClassB extends ClassC
{
Public void methodA ()

{
.....;
}
}

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.