设计模式之适配器模式
2015-08-11 10:52
477 查看
设计模式之--适配器模式
1,定义:将一个类的接口转换成客户希望的另外一个接口;
2,分类:
对象适配器模式:不是通过继承方式,而是通过对象组合方式来进行处理;
类适配器模式:通过继承的方法实现,将旧系统的方法进行封装。对象适配器在进行适配器之间的转换过程时,无疑使用类适配器也能完成,但是依赖性会变大,并且根据适配的要求的灵活性,可能通过继承系统会膨胀到难以控制;
3,适用情况:
1):当想要使用一个已经存在的类,但是该类的接口不符合现有的需求时;
2):当需要创建一个可以被复用的子类,该类能够与其他无关的类甚至无法预见的类协同工作时;
3):当需要使用一些已经存在的子类,但是不可能对所有的子类都进行子类化以匹配他们的我接口时,对象适配器可以对其父类接口进行适配;
4,代码:
1),基本实现代码:
主类:Main.class:
public class Main {
public static void main(String[] args) {
Target target = new Target();
target.Request();
target = new Adapter();
target.Request();
}
}
客户希望的接口类:Target.class:
public class Target {
public void Request(){
System.out.println("客户发起了普通基本需求");
}
}
希望被复用的类:Adaptee.class:
public class Adaptee {
public void SpecificRequest() {
// TODO Auto-generated method stub
System.out.println("客户发起了特殊的需求");
}
}
适配器类:Adapter.class:
public class Adapter extends Target{
private Adaptee adaptee = new Adaptee();
@Override
public void Request() {
// TODO Auto-generated method stub
adaptee.SpecificRequest();
}
}
运行结果:
2),实例代码:
主类:Main.class:
public class Main {
public static void main(String[] args) {
System.out.println("小面出国之前使用笔记本:");
Adapter adapter = new Adapter220V();
adapter.PowerSupply();
System.out.println("小面出国之后使用笔记本:");
adapter = new Adapter110V();
adapter.PowerSupply();
}
}
要被复用的类--110V电源接口类:PowerPort110V.class:
/*110V电源接口*/
public class PowerPort110V {
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
System.out.println("110V电源接口向外输出了110V的电压");
}
}
要被复用的类--220V电源接口类:[b]PowerPort220V.class:[/b]
/*220V电源接口*/
public class PowerPort220V {
public void powerSupply(){//适配器提供电源
System.out.println("110V的电源接口向外输出了220V的电压");
}
}
适配器接口类:[b]Adapter.class:[/b]
/*适配器接口*/
public interface Adapter {
void PowerSupply();
}
适配器类:110V接口的适配器类[b]:Adapter110V.class:[/b]
/*110V接口的适配器类*/
public class Adapter110V extends PowerPort110V implements Adapter{
NoteBook noteBook = new NoteBook();
@Override
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
super.PowerSupply();
System.out.println("适配器将电压转换成了笔记本需要的");
noteBook.Work();
}
}
适配器类:220V接口的适配器类:Adapter220V.class:
/*220V接口的适配器类*/
public class Adapter220V extends PowerPort220V implements Adapter{
NoteBook noteBook = new NoteBook();
@Override
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
super.powerSupply();
System.out.println("适配器将电压转换成了笔记本所需要的");
noteBook.Work();
}
}
要使用复用代码的类:NoteBook.class:
/*笔记本类*/
public class NoteBook {
public void Work(){
System.out.println("笔记本开始工作了");
}
}
运行结果:
1,定义:将一个类的接口转换成客户希望的另外一个接口;
2,分类:
对象适配器模式:不是通过继承方式,而是通过对象组合方式来进行处理;
类适配器模式:通过继承的方法实现,将旧系统的方法进行封装。对象适配器在进行适配器之间的转换过程时,无疑使用类适配器也能完成,但是依赖性会变大,并且根据适配的要求的灵活性,可能通过继承系统会膨胀到难以控制;
3,适用情况:
1):当想要使用一个已经存在的类,但是该类的接口不符合现有的需求时;
2):当需要创建一个可以被复用的子类,该类能够与其他无关的类甚至无法预见的类协同工作时;
3):当需要使用一些已经存在的子类,但是不可能对所有的子类都进行子类化以匹配他们的我接口时,对象适配器可以对其父类接口进行适配;
4,代码:
1),基本实现代码:
主类:Main.class:
public class Main {
public static void main(String[] args) {
Target target = new Target();
target.Request();
target = new Adapter();
target.Request();
}
}
客户希望的接口类:Target.class:
public class Target {
public void Request(){
System.out.println("客户发起了普通基本需求");
}
}
希望被复用的类:Adaptee.class:
public class Adaptee {
public void SpecificRequest() {
// TODO Auto-generated method stub
System.out.println("客户发起了特殊的需求");
}
}
适配器类:Adapter.class:
public class Adapter extends Target{
private Adaptee adaptee = new Adaptee();
@Override
public void Request() {
// TODO Auto-generated method stub
adaptee.SpecificRequest();
}
}
运行结果:
2),实例代码:
主类:Main.class:
public class Main {
public static void main(String[] args) {
System.out.println("小面出国之前使用笔记本:");
Adapter adapter = new Adapter220V();
adapter.PowerSupply();
System.out.println("小面出国之后使用笔记本:");
adapter = new Adapter110V();
adapter.PowerSupply();
}
}
要被复用的类--110V电源接口类:PowerPort110V.class:
/*110V电源接口*/
public class PowerPort110V {
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
System.out.println("110V电源接口向外输出了110V的电压");
}
}
要被复用的类--220V电源接口类:[b]PowerPort220V.class:[/b]
/*220V电源接口*/
public class PowerPort220V {
public void powerSupply(){//适配器提供电源
System.out.println("110V的电源接口向外输出了220V的电压");
}
}
适配器接口类:[b]Adapter.class:[/b]
/*适配器接口*/
public interface Adapter {
void PowerSupply();
}
适配器类:110V接口的适配器类[b]:Adapter110V.class:[/b]
/*110V接口的适配器类*/
public class Adapter110V extends PowerPort110V implements Adapter{
NoteBook noteBook = new NoteBook();
@Override
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
super.PowerSupply();
System.out.println("适配器将电压转换成了笔记本需要的");
noteBook.Work();
}
}
适配器类:220V接口的适配器类:Adapter220V.class:
/*220V接口的适配器类*/
public class Adapter220V extends PowerPort220V implements Adapter{
NoteBook noteBook = new NoteBook();
@Override
public void PowerSupply() {//适配器提供电源
// TODO Auto-generated method stub
super.powerSupply();
System.out.println("适配器将电压转换成了笔记本所需要的");
noteBook.Work();
}
}
要使用复用代码的类:NoteBook.class:
/*笔记本类*/
public class NoteBook {
public void Work(){
System.out.println("笔记本开始工作了");
}
}
运行结果:
相关文章推荐
- 更新和删除文档
- 最齐全的Android studio 快捷键(亲测可用)
- 黑马程序员-集合框架(二)
- 链队列
- 安装Win10正式版后foobar2000打不开的两种解决办法
- codeforce 315 div2
- UTF-8
- iOS中.m中,@interface部分的作用
- 错误笔记150811--引用问题
- hdu 1358 Period(kmp)
- hdoj 1863 畅通工程
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
- 算法竞赛入门经典: 第三章 数组和字符串 3.4竖式问题
- 21-IO流-17-IO流(字符流-缓冲区-自定义MyBufferedReader-readLine方法)
- APP搜索附近功能的一种解决方案-基于百度LBS云服务
- ZigBee路由协议分析(机制,算法,发现,建立)
- winform之自定义控件
- CodeForces 344A Magnets
- MySQL出现同步延迟如何解决?优化?
- apache的扩展模块安装