Happy design mode adapter mode (JAVA)

Source: Internet
Author: User


Design mode-Adapter mode


Adapter Mode definition

The adapter mode, which transforms the interface of a class into the interface form that the client or product wants, is an interface that is inherently incompatible or even irrelevant interfaces that cannot work together to fulfill the needs or needs of the customer.


Usage scenarios for Adapter mode

1, when you use an already existing class, and his interface does not meet your needs.

2. You want to create a reusable class that can work in conjunction with its related classes or classes that are not visible.


Adapter role

Target: Destination interface

Targetimpl: Target Implementation class

Adapter: Adapter

Adaptee: The person being adapted


Code parsing:

Package com.design.pattern.adapter;/** * Target Interface class * Created by SDC on 2017/9/3. */public interface Target {/** * processing method */public void spider ();
Package com.design.pattern.adapter;/** * Created by SDC on 2017/9/3. */public class Targetimpl implements Target {@Override public void spider () {//dosomething System.ou    T.println ("dosomething"); }}
Package com.design.pattern.adapter;/** * Adapter * Created by SDC on 2017/9/3.    */public class Adaptee {public void spiderfor () {System.out.println ("Adaptee adapter Method"); }}
Package com.design.pattern.adapter;/** * is adapter 3 * Created by SDC on 2017/9/3.    */public class Adapteer implements Target {//adapter private adaptee adaptee = new Adaptee ();    @Override public void Spider () {adaptee.spiderfor (); }}
Package com.design.pattern.adapter;/** * Created by SDC on 2017/9/3.       */public class Main {public static void Main (string[] args) {Target adapteer = new Adapteer ();       Adapteer.spider ();       Target Target1 = new Targetimpl ();    Target1.spider (); }}


Such a simple design pattern is done. Sometimes the project, there are a lot of duplicated code, every day to create new duplicate code, try to extract the code, or you can use some practical mode to make some simple design, perhaps a different effect.

This article is from the "no kuibu to thousands of Miles" blog, please be sure to keep this source http://shangdc.blog.51cto.com/10093778/1962321

Happy design mode adapter mode (JAVA)

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.