Adapter design mode

Source: Internet
Author: User
You are welcome to reprint it. Please indicate the source. 1. Core intent:Converts an interface of a class to another interface that the customer wants, so that classes that are originally incompatible with the interface cannot work together can work together. The goal of this mode is to coordinate the original class (Adaptee) with the Client through a proxy (Adapter here) to achieve compatibility. Its core is to solve the consistency problem. 2. Nearby instances:In our real life, we can easily see examples in this regard. For example, if we want to deal with a foreigner, such as a Korean, if we haven't learned Korean, this Korean has never learned Chinese, in this case, it is difficult for us to communicate directly. There are two ways to achieve the purpose of communication: 1) transform the Korean to allow them to communicate in Chinese; 2) Ask a translator to coordinate the language between us and the Korean. Obviously, the first way is to transform this Korean man at a higher cost. We should not only re-train him to learn Chinese, but also have time, attitude, and other factors. The second method is to ask for a translation, which is easy to implement, with low cost and flexibility. When we want to change to a Japanese, we can change the translation. 3. Motivation description:The implementation of a drawing editor is described in the motivation of this mode. The editor can draw and arrange basic elements (such as straight lines, polygon, and body) to generate images and charts, for the implementation of these primitive classes, straight lines/polygon are relatively easy to implement, but the implementation of the body is very troublesome. To reduce development costs and ensure quality, the Adapter class TextShape is defined by using the Adapter mode, to reuse the existing text editor TextView in the graphic toolbox. 4. Java implementation analysis:In the GOF design mode, the Adapter can be divided into the class mode and the object mode. The class mode is implemented through multiple inheritance, and the object mode is implemented through delegation. Because there is no multi-Inheritance Mechanism in Java, to implement the Adapter of the class mode, you need to change accordingly: implement the Target Interface by inheriting the Adaptee class. There are two problems with this change: 1) the Target must be an interface rather than a class; otherwise, the Adapter cannot implement implements; 2) the Adapter inherits the implementation of Adaptee rather than private inheritance, this indicates that the Adapter is a subclass of Adaptee. For Java code of the Adapter-like mode and Object Adapter mode, refer to the Code section below this article. 5. Class Mode/Object Mode Analysis:Because there are two Adapter implementation methods (even in Java), which one is better in practice? The analysis shows that the two modes have two main characteristics and are complementary: A. the characteristics of Adaptee are as follows:Because the Adapter is a subclass of Adaptee, it is convenient for the Adapter to redefine some methods in Adaptee to meet its own feature requirements. Because the Adapter is not a subclass of Adaptee, if the Adapter has special requirements for some methods in Adaptee, it is necessary to create a subclass of Adaptee and let the Adapter use this subclass. B. Hierarchy extension of Adaptee:Because the Adapter is a subclass of Adaptee, after compilation, it cannot replace the implemented parent class Adaptee. Therefore, if there is an Adaptee class hierarchy, the corresponding class hierarchy of the Adapter is required, and it is inconvenient to expand the Adaptee. Object Mode because the Adapter is not a subclass of Adaptee, but is used, you can still change the Adaptee used by the Adapter when the system is running, as long as they have the same type. Therefore, it is convenient to expand Adaptee. 6. Java code example-Object Mode implementation:Class Point, indicating the Point in the screen Coordinate
package qinysong.pattern.adapter;public class Point {  private int coordinateX;  private int coordinateY;  public Point(int coordinateX, int coordinateY){    this.coordinateX = coordinateX;    this.coordinateY = coordinateY;  }  public String toString(){    return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";  }  public int getCoordinateX() {    return coordinateX;  }  public int getCoordinateY() {    return coordinateY;  }}

Class Shape, indicating the primitive excuse, corresponding to the Target in Adapter Mode

package qinysong.pattern.adapter;public interface Shape {  public Point getBottomLeftPoint();  public Point getTopRightPoint();}

Class TextView, the text component class in the toolbox, and the existing class, corresponding to Adaptee in Adapter Mode

package qinysong.pattern.adapter;public class TextView {  public int getCoordinateX() {    System.out.println("TextView.getCoordinateX()...");    return 10;  }  public int getCoordinateY() {    System.out.println("TextView.getCoordinateY()...");    return 20;  }  public int getHeight() {    System.out.println("TextView.getHeight()...");    return 30;  }  public int getWidth() {    System.out.println("TextView.getWidth()...");    return 40;  }  public boolean isEmpty(){    return false;  }}

Class TextShape, Adapterpackage qinysong. pattern. adapter implemented by object mode;

Public class TextShape implements Shape {private TextView textView; public TextShape (TextView textView) {this. textView = textView;} // implement public Point getBottomLeftPoint () {System. out. println ("TextShape. getBottomLeftPoint ()... "); int coordinateX = textView. getCoordinateX (); int coordinateY = textView. getCoordinateY (); return new Point (coordinateX, coordinateY);} // implement public Point getTopRightPoint () {System. out. println ("TextShape. getTopRightPoint ()... "); int coordinateX = textView. getCoordinateX (); int coordinateY = textView. getCoordinateY (); int height = textView. getHeight (); int width = textView. getWidth (); return new Point (coordinateX + width, coordinateY + height );}}

Clients in Client-like and Adapter Mode

Package qinysong. pattern. adapter; public class Client {public static void main (String [] args) {System. out. println ("Client. main begin .......... "); System. out. println ("Client. main The following is the Adapter implemented through instance delegation "); Shape shape = new TextShape (new TextView (); Point bottomLeft = shape. getBottomLeftPoint (); Point topRight = shape. getTopRightPoint (); System. out. println ("Client. main shape's bottomLeft: "+ bottomLeft); System. out. println ("Client. main shape's topRight: "+ topRight); System. out. println ("Client. main The following is the Adapter implemented through class inheritance "); Shape shape2 = new TextShape2 (); bottomLeft = shape2.getBottomLeftPoint (); topRight = shape2.getTopRightPoint (); System. out. println ("Client. main shape2's bottomLeft: "+ bottomLeft); System. out. println ("Client. main shape2's topRight: "+ topRight); System. out. println ("Client. main end .......... ");}}

7. Java code example-class mode implementation:Similar to the example in the preceding Object Mode implementation, the class Point, Shape, and TextView are the same, omitted. The following is an example code of TextShape2 class. The Adapter that implements the Class Mode

Package qinysong. pattern. adapter; public class TextShape2 extends TextView implements Shape {// coordinates the implementation of public Point getBottomLeftPoint () {System. out. println ("TextShape2.getBottomLeftPoint ()... "); int coordinateX = getCoordinateX (); int coordinateY = getCoordinateY (); return new Point (coordinateX, coordinateY);} // pass the inherited TextView, implement public Point getTopRightPoint () {System. out. println ("TextShape2.getTopRightPoint ()... "); int coordinateX = getCoordinateX (); int coordinateY = getCoordinateY (); int height = getHeight (); int width = getWidth (); return new Point (coordinateX + width, coordinateY + height);} // note: this reflects the advantages of the class mode. You can easily define the public int getCoordinateX () {System in the TextView of the parent class. out. println ("TextShape2.getCoordinateX ()... "); return 100 ;}}

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.