java--Adapter Mode (Adapter)--Design mode VI

Source: Internet
Author: User


1, Overview:

(1) The adapter mode of the object is the origin of the various modes:



(2) adapter mode:The adapter pattern transforms the interface of a class into another interface representation that the client expects to beeliminate the compatibility problems of classes caused by interface mismatch, mainly divided into three categories:Adapter mode for the ① classAdapter mode for ② objectsAdapter mode for ③ interface
   1, Class of adapter mode:
(1) The core idea is: There is a source class, has a method, to be adapted, the target interface istargettable, through the adapter class, extends the function of source to targettable,


(2) Code:





3, the adapter mode of the object:
(1) The basic idea and the class adapter mode is the same, only the adapter class is modified, this time does not inheritsource class, but holds instances of the source class to resolve compatibility issues


(2) Code:



 4, adapter mode for the interface:
(1) interface Adapter mode: Sometimes we write a socket with multiple abstract methods, when we write thatinterface, you must implement all the methods of the interface, which is obviously sometimes wasteful, because it is notall the methods we need, sometimes only need some, here in order to solve this problem, we introduceThe interface's adapter pattern, with the aid of an abstract class, the abstract class implements the interface and implements allmethod, and we do not deal with the original interface, only with the abstract class to get in touch, so we write aclass, inheriting the abstract class, overriding the method we need.


(2) Code:This is a good understanding, in the actual development, we also often encounter this interface defines too manymethod so that sometimes we don't need it in some implementation classes,



5, Summary:
(1) Summarize the application scenarios of some three kinds of adapters:             Adapter mode for the ① class: When you want to convert a class to a class that satisfies another new interface,You can use the class's adapter pattern, create a new class, inherit the original class, implement the new interfacecan beAdapter mode for the ② object: When you want an object to be converted to another new interface,you can create a wrapper class that holds an instance of the original class , and in the method of the wrapper class,the method that invokes the instance is the lineAdapter mode for the ③ interface: When you do not want to implement all the methods in an interface, you canCreate an abstract class wrapper, implement all the methods that we write to other classes when inheriting abstract classescan be




java--Adapter Mode (Adapter)--Design mode VI

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.