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)