Design Pattern Learning Notes (ii)--adapter Adapter mode

Source: Internet
Author: User
Tags inheritance

GOF "design pattern" is described in the adapter mode:

Converts the interface of a class into another interface that the customer expects. The adapter mode makes it possible for classes that cannot work together because of incompatible interfaces.

This passage roughly says: We need a way to create a new interface for an object that is functionally correct but not interface. For example, customers give us the following requirements:

1 Create a class for points, lines, and squares that have a "display" behavior.

2 Customer objects do not have to know whether they own the point, line, or square. It only needs to know one of these shapes.

That is, we're going to cover these concrete shapes with a higher level of concept, which can be called "shapes that can be displayed." Therefore, we need to create an interface shape:

interface Shape{
  publicvoid display();
}

Now the customer suddenly has let us give this system to add a circle of function. This looks very simple, just define a circle class to implement the shape interface, but we have to write a display method for it, this is not a simple matter, if we happen to find a xxcircle class, it has a way to complete this function, Then there is no better way to use it, then we will use the adapter mode. The Xxcircle code is as follows:

class XXCircle{
  public void displayCircle(){
    System.out.println("通过XXCircle.displayCircle()画圆");
  }
}

There are two types of adapter modes:

1 Object Adapter mode, which relies on an object (adapter) that contains another object (the Adaptive object).

class CircleObject implements Shape{
  public XXCircle circle;
  public CircleObject(XXCircle xxcircle){
  circle=xxcircle;
  }
  public void display(){
    circle.displayCircle();
  }
}
public class Client {
  public static void main(String[] args){
    XXCircle circle=new XXCircle();
    CircleObject co=new CircleObject(circle);
    co.display();
  }
}

2 class Adapter mode, it is implemented through multiple inheritance (there is no more inheritance in Java, it is implemented through interfaces).

class CircleClass extends XXCircle implements Shape{
  public void display(){
    super.displayCircle(); 
  }
}
public class Client {
  public static void main(String[] args){
    CircleClass cc=new CircleClass();
    cc.display();
  }
}

Summary: The adapter pattern is a very common pattern that converts the interface of one (or more) classes into an interface that we need the class to have. It is implemented by creating a class that has the required interface, and then wrapping the method of the original class, which actually contains the appropriate object. It mainly applies to the following situations:

1 you want to use a subroutine or method written by another person because you need the functionality it performs.

2 You cannot add this subroutine directly to the program.

3 the interface or invocation of a subroutine is not exactly the same as the related object that needs to be used.

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.