Design mode first bullet-adapter mode

Source: Internet
Author: User

1. Scene

2. Category

Class 2.1 Agent

Existing classes with special features but not conforming to our existing standard interface class Adaptee {public void Specificrequest () {System.out.println ("The adapted class has special features ...");}}  Target interface, or standard interface interface target {public void request ()}//target class, only provides normal function class Concretetarget implements target {public Vo ID request () {System.out.println ("ordinary 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 Concretetarg ET (); concretetarget.request ();//Use a special function class, that is, the adaptation class target adapter = new adapter (); Adapter.request ();}}

  

2.2 Object Proxies

//adapter class, directly associated with the appropriate class, while implementing a standard interfaceclassAdapterImplementstarget{//Direct correlation is adapted class    Privateadaptee adaptee; //you can pass through the constructor to the adapted class object that needs to be adapted.     PublicAdapter (adaptee adaptee) { This. Adaptee =adaptee; }         Public voidrequest () {//here is the way to accomplish special functions using delegates         This. Adaptee.specificrequest (); }}  //Test Class Public classClient { Public Static voidMain (string[] args) {//using normal function classesTarget Concretetarget =NewConcretetarget ();                Concretetarget.request (); //use a special function class, the adaptation class,//you need to first create an object of the appropriate class as a parameterTarget adapter =NewAdapter (Newadaptee ());    Adapter.request (); }}

Design mode first bullet-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.