Java Presentation Adapter (adapter) mode

Source: Internet
Author: User

Why use Patterns: patterns are a way of doing things, that is, the way to achieve a goal, or technology.
The purpose of the adapter model is to retain the services provided by existing classes and to provide customers with interfaces to meet customer needs. Class Adapter: The client defines the interface and implements the interface, the method in its own class library has a better implementation, but the method name is not the same, not for the user, you can define a class implementation of this interface, the method of the interface can be delegated to their own class library methods to achieve the effect, To meet the needs of customers. This approach is called interface adaptation.

A class Adaptee {public    void Specificrequest () {        System.out.println ("the" adapter class has special functionality) that already exists and has a special function but does not conform to our existing standard interface ...");    }} Target interface, or standard interface interface target {public    void request ();} Target class, only provide normal function class Concretetarget implements target {public    void request () {        System.out.println ("Normal class has normal function ...");    }} The adapter class inherits the appropriate class, while implementing the standard interface class Adapter extends Adaptee implements target{public    void request () {        Super.specificrequest ();    }}  Test class public class Client {public    static void Main (string[] args) {        //Use normal function class        Target concretetarget = new Concretetarget ();        Concretetarget.request ();                Using a special function class, the adaptation class        Target adapter = new adapter ();        Adapter.request ();    }}

Object adapters: Instead of using multiple inheritance or inheritance implementations, instead of using a direct association, or a delegate in a way that inherits, instead uses the global parameter to save the object that is being adapted, using that object to fit the content//share in front of the content

The adapter class, which is directly associated with the appropriate class, implements the standard interface class Adapter implements target{    //directly associated with the appropriate class    private adaptee adaptee;        The    Adapter (adaptee adaptee) {        this.adaptee = adaptee        ) can be passed through the constructor to the appropriate matching class object that needs to be adapted. public void request () {        //Here is the way to accomplish special functions using a delegate        this.adaptee.specificRequest ();}    }  Test class public class Client {public    static void Main (string[] args) {        //Use normal function class        Target concretetarget = new Concretetarget ();        Concretetarget.request ();                Using a special function class, the adaptation class,        //requires first creating an object of the appropriate class as the parameter        target adapter = new Adapter (new Adaptee ());        Adapter.request ();    }}

Java Presentation Adapter (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.