Various adapters in Design Mode

Source: Internet
Author: User

Various adapters in Design Mode

Seven structural modes: adapter mode, decoration mode, proxy mode, appearance mode, bridging mode, combination mode, and meta mode. The object's adapter mode is the origin of various modes. Let's look at the figure below:

 

The adapter mode converts an interface of a class to another interface that the client expects. The purpose is to eliminate the compatibility issues of the class caused by interface mismatch. There are three main types: Class adapter mode, Object Adapter mode, and interface adapter mode.

Class adapter mode:

 

Code:

 

Public class Source {public void method1 () {System. out. println ("this is original method! ") ;}} Public interface Targetable {/* is the same as the method in the original class */public void method1 ();/* method of the new class */public void method2 ();} public class Adapter extends Source implements Targetable {@ Override public void method2 () {System. out. println ("this is the targetable method! ") ;}} The Adapter class inherits the Source class and implements the Targetable interface. The following is a test class: public class AdapterTest {public static void main (String [] args) {Targetable target = new Adapter (); target. method1 (); target. in this way, the implementation class of the Targetable interface has the function of the Source class.


 

Object Adapter mode:

 

Public class Wrapper implements Targetable {private Source source Source; public Wrapper (source Source source) {super (); this. source = source ;}@ Override public void method2 () {System. out. println ("this is the targetable method! ") ;}@ Override public void method1 () {source. method1 () ;}} test class:


 

Interface adapter mode:

Application Scenario: Sometimes an interface we write has multiple abstract methods. When we write the implementation class of this interface, we must implement all the methods of this interface, which is obviously sometimes a waste, because not all methods are needed, sometimes we only need some. To solve this problem, we introduce the adapter mode of the interface, with the help of an abstract class, this abstract class implements this interface and implements all the methods. Instead of dealing with the original interface, we only get in touch with this abstract class. Therefore, we write a class that inherits this abstract class, rewrite the method we need. Take a look at the class diagram:

Code:

 

Public interface Sourceable {public void method1 (); public void method2 ();} abstract class Wrapper2: public abstract class Wrapper2 implements Sourceable {public void method1 () {} public void method2 () {}} public class SourceSub1 extends Wrapper2 {public void method1 () {System. out. println ("the sourceable interface's first Sub1! ") ;}} Public class SourceSub2 extends Wrapper2 {public void method2 () {System. out. println (" the sourceable interface's second Sub2! ") ;}} Public class WrapperTest {public static void main (String [] args) {Sourceable source1 = new SourceSub1 (); Sourceable source2 = new SourceSub2 (); source1.method1 (); source1.method2 (); source2.method1 (); source2.method2 () ;}} public class AdapterTest {public static void main (String [] args) {Source source Source = new Source (); targetable target = new Wrapper (source); target. method1 (); target. method2 ();}}

 

Class adapter mode: When you want to convert a class to a class that meets another new interface, you can use the class adapter mode to create a new class that inherits the original class, implement the new interface.

Object Adapter mode: When you want to convert an object to an object that meets another new interface, you can create a Wrapper class and hold an instance of the original class. In the Wrapper class method, call the method of the instance.

Interface adapter mode: if you do not want to implement all the methods in an interface, you can create an abstract class Wrapper to implement all the methods. When writing other classes, inherit the abstract class.


 

 

 

 

 

 

 

 

 

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.