Java Adapter mode

Source: Internet
Author: User

is a structural pattern, and its main function is to convert 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.
For example, your phone only 2.5mm interface (seemingly on the Nokia dry out), but you can only buy 3.5mm, then you need to buy an adapter (─.─| | |
Purpose of Intention:
Retain the services provided by existing classes and provide interfaces to customers to meet customer expectations
When you can use it:
1 you want to use a class that already exists, and its interface doesn't fit your needs. No 2.5mm
2 You want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those whose interfaces might not necessarily be compatible).
3 You want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces. The object adapter can be adapted to its parent class interface-only for object adapter.

How to achieve:
Class 1 Adapters

1 public class Adapter extends Adaptee implements target{2     @Override 3 public     void GetHeadset2 () {4         This.get Headset3 (); 5     } 6 public     static void Main (String args[]) {7         target target = new Adapter ();  8         Target.getheadset2 (); 9     }10}11 interface target{12     void GetHeadset2 ();}14 class adaptee{15     public void GetHeadset3 () {         System.out.println ("I'm 3.5mm headphones Oh");     }18}

2 Object Adapter

1 public class Adapter implements target{2     private Adaptee adaptee, 3 public     Adapter () {4         super (); 5         th Is.adaptee = new Adaptee (); 6     } 7     @Override 8 public     void GetHeadset2 () {9         adaptee.getheadset3 (),     }11 public     static void Main (String args[]) {         target target = new Adapter ();         Target.getheadset2 ();     }15}16 interface target{17     void GetHeadset2 ();}19 class Adaptee{20 public     void GetHeadset3 () {         System.out.println ( "I'm a 3.5mm headset, oh");     }23}

The difference between an object's adapter pattern is that the adapter role encapsulates the adaptee role rather than the inheritance that the class's adapter pattern takes. The principle is basically similar
3 Default Adapter Mode
The default adapter pattern is a special adapter pattern, but this adapter is implemented by an abstract class, and all methods specified in the target interface are implemented in an abstract class, but many methods are implemented as NULL methods. And the specific subclass inherits this abstract class.

Java Adapter mode

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.