Example parse the adapter pattern in Java design pattern programming using _java

Source: Internet
Author: User
Tags static class

Usually we often encounter this situation, there are two of ready-made classes, there is no connection between them, but we now want to use one of the methods of the class, but also want to use the method of another class. One solution is to modify their respective interfaces, but that is the last thing we want to see. This time the adapter model will come in handy.

Adapter mode is also called adapter mode, which is one of the 23 design patterns proposed by GOF. The adapter pattern is one of the stereotype patterns that can change the interface form of an existing class (or an external class) through the adapter pattern.
There are three modes of adapter mode, one is object adapter, one is class adapter, the other is interface adapter
The following examples illustrate:
1. Class Adapter

Class diagram

public class DrawRectangle {//Painting party public 
  void DrawRectangle (String msg) { 
    System.out.println ("Draw Rectangle:" + msg); 
  } 

Public interface Idrawcircle {//Draw round interface 
  void Drawcircle (); 
} 


/** 
 * Class adapters are statically defined by the way object is inherited 
 * @author Stone */ 
 public 
class Drawadapter4class extends DrawRectangle implements Idrawcircle {//can draw both sides and draw circles 
 
  @Override public 
  void Drawcircle () { 
    System.out.println ("drawadapter4class:drawcircle"); 
  } 
   
 

2. Object Adapters

Class Diagram:

/** 
 * Object adapters: The way in which objects are grouped, the way they are dynamically grouped. 
 * Can draw both sides and can draw round 
 * @author Stone 
 * Drawadapter is the adapter, DrawRectangle belongs to adapter, is the person who is fit, The adapter will be fitted with the appropriate target (drawcircle) * */Public 
class Drawadapter4object implements Idrawcircle {//can draw and draw circles 
   
  private DrawRectangle DrawRectangle; 
  Public Drawadapter4object (DrawRectangle drawrectangle) { 
    this.drawrectangle = DrawRectangle; 
  } 
   
  @Override public 
  void Drawcircle () { 
    System.out.println ("drawadapter4object:drawcircle"); 
  } 
   
  public void DrawRectangle (String msg) { 
    drawrectangle.drawrectangle (msg); 
  }  
   
} 

3. Interface Adapter
class Diagram

 
 * * Interface Adapter: There are abstract methods in the interface, we just want to implement a few of them, do not want to implement all, 
 * So provide a default null implementation, and then inherit from it, rewrite the implementation of the method we want to implement 
 * * Public 
interface Idraw { 
  void drawcircle (); 
  void DrawRectangle (); 
} 

 
 * * * Interface Adapter default implementation 
 * 
/public class Defaultdrawadapter implements Idraw {//Painting square draw circle is empty to achieve 
 
  @Override 
  public void Drawcircle () { 
     
  } 
 
  @Override public 
  void DrawRectangle () { 
     
  } 
} 


public class Test {public 
  static void Main (string[] args) { 
    //object Adapter 
    drawadapter4object objadapter = new Drawa Dapter4object (New DrawRectangle ()); 
    Objadapter.drawcircle (); 
    Objadapter.drawrectangle ("in Drawadapter4object"); 
     
    System.out.println ("--------------"); 
     
    Class Adapter 
    Drawadapter4class clzadapter = new Drawadapter4class (); 
    Clzadapter.drawcircle (); 
    Clzadapter.drawrectangle ("in Drawadapter4class"); 
     
    System.out.println ("--------------"); 
     
    Interface Adapter 
    Mydrawadapter mydrawadapter = new Mydrawadapter (); 
    Mydrawadapter.drawcircle (); 
    Mydrawadapter.drawrectangle (); 
  } 
   
  Static Class Mydrawadapter extends Defaultdrawadapter { 
 
    @Override public 
    void Drawcircle () { 
      System.out.println ("Drawcircle in Mydrawadapter");}} 
 


Print

Drawadapter4object:drawcircle 
Draw rectangle:in drawadapter4object 
-------------- 
drawadapter4class: Drawcircle 
Draw rectangle:in drawadapter4class 
-------------- 
drawcircle in Mydrawadapter 


three features of adapter mode:

1 Adapter object implements the original interface
The 2 adapter object combines an object that implements the new interface (this object can also not implement an interface, just a simple object)
3 A specific method of calling an adapter's original interface method to an instance of a new interface

Some people think that the explanation of design patterns is too simple to look at, but if you really want to use it in the development of the project, it really does not apply. In fact, we do not have to deliberately use the design pattern in the project, but should start from the actual design problems, see which mode can solve our problems, use which mode. Do not use mode for the use of mode, so that the trifles, in general, as long as the following a certain design principles can be, design patterns are summed up in accordance with these principles, familiar with these principles, the pattern will naturally have.

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.