Java design mode-Adapter mode

Source: Internet
Author: User

Adapter mode (Adapter): transforms the interface of one class into another interface that the customer wants.

The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.


The following is the sample code:

/** * Adapter Mode * @author Admin * */public class Adapter {public static void start (Powera p) {        //Only accept call to interface A, this will use POWERADAPT ER adapter to allow interface B to also call P.chong ();} public static void Main (string[] args) {//TODO auto-generated method Stubstart (New  Poweraimpl ()); Start (new Poweradapter (New Powerbimpl ()));}} Class Poweradapter implements powera{        //In order to implement Interface B can be converted into interface a called private Powerb p1;public Poweradapter (Powerb p) {p1 = P;} @Overridepublic void Chong () {//TODO auto-generated method Stubp1.check ();}} Interface powera{public void Chong ();} Class Poweraimpl implements powera{@Overridepublic void Chong () {System.out.println ("Powera charging");}} interface powerb{public void check (); Class Powerbimpl implements powerb{@Overridepublic void Check () {System.out.println ("Powerb charging Check");}}

Example results:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java design mode-Adapter mode

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.