Adapter mode in Java

Source: Internet
Author: User

Adapter mode in Java

We often encounter such a situation that there are two ready-made classes with no connection between them, but now we want to use one of the class methods, I also want to use another class method. One solution is to modify their respective interfaces, but this is what we are most reluctant to see. At this time, the Adapter mode will be used.

There are three adapter modes: Object Adapter, class adapter, and interface adapter.

The following is an example:

Public class DrawRectangle {// specifies the public void drawRectangle (String msg) {System. out. println ("draw Rectangle:" + msg );}}

Public interface IDrawCircle {// void drawCircle ();}

/*** Class adapter uses the Object Inheritance Method, is a static definition method * @ author stone **/public class DrawAdapter4Class extends DrawRectangle implements IDrawCircle {// draw both parties and circles @ Overridepublic void drawCircle () {System. out. println ("DrawAdapter4Class: drawCircle ");}}

/*** Object Adapter: the object combination is a dynamic combination. * Drawing both parties and circles * @ author stone * DrawAdapter is the adapter, DrawRectangle is the adapter, and is the adapter. The adapter will be the adapter and the matching target (DrawCircle) adapt **/public class DrawAdapter4Object implements IDrawCircle {// draw both parties and circles private DrawRectangle drawRectangle; public DrawAdapter4Object (DrawRectangle drawRectangle) {this. drawRectangle = drawRectangle;} @ Overridepublic void drawCircle () {System. out. println ("DrawAdapter4Object: drawcircle");} public void drawRectangle (String msg) {drawRectangle. drawRectangle (msg );}}

Interface adapter

/** Interface adapter: There are abstract methods in the interface. We only want to implement a few of them and do not want to implement them all. * therefore, we provide a default empty implementation and inherit from it, override the method we want to implement */public interface IDraw {void drawCircle (); void drawRectangle ();}

/** Default Implementation of the interface adapter */public class DefaultDrawAdapter implements IDraw {// Implementation of Overridepublic void drawCircle () {}@ Overridepublic void drawRectangle () {}}

Public class Test {public static void main (String [] args) {// Object Adapter DrawAdapter4Object objAdapter = new DrawAdapter4Object (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 {@ Overridepublic void drawCircle () {System. out. println ("drawCircle in MyDrawAdapter ");}}}

Print

DrawAdapter4Object: drawcircledraw Rectangle:  in DrawAdapter4Object--------------DrawAdapter4Class: drawCircledraw Rectangle: in DrawAdapter4Class--------------drawCircle in MyDrawAdapter


Related Article

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.