Java的适配器模式
来源:互联网 发布:spacex知乎 编辑:程序博客网 时间:2024/05/07 02:59
将一个东西转换为在此基础上改善的一个东西,就是适配器模式的用武之地。
1 原有类具有的功能
package com.javapatterns.adapter.classAdapter;
public class Adaptee ...{
/**//*
* 原有类具有的功能
*/
public void sampleOperation1()
...{
System.out.println("this is 220v");
}
}
public class Adaptee ...{
/**//*
* 原有类具有的功能
*/
public void sampleOperation1()
...{
System.out.println("this is 220v");
}
}
2 现在需要新的功能,通过接口创建。
package com.javapatterns.adapter.classAdapter;
public interface Target ...{
/** *//**
* Class Adaptee contains operation sampleOperation1.
*/
void sampleOperation1();
/** *//**
* Class Adaptee doesn't contain operation sampleOperation2.
*/
void sampleOperation2();
}
public interface Target ...{
/** *//**
* Class Adaptee contains operation sampleOperation1.
*/
void sampleOperation1();
/** *//**
* Class Adaptee doesn't contain operation sampleOperation2.
*/
void sampleOperation2();
}
3 在新类中添加实现具体的新功能
package com.javapatterns.adapter.classAdapter;
public class Adapter extends Adaptee implements Target ...{
/** *//**
* Class Adaptee doesn't contain operation sampleOperation2.
*/
public void sampleOperation2()...{
System.out.println("This is 5v");
}
}
public class Adapter extends Adaptee implements Target ...{
/** *//**
* Class Adaptee doesn't contain operation sampleOperation2.
*/
public void sampleOperation2()...{
System.out.println("This is 5v");
}
}
4 测试
/**//*
* test.java
*
* Created on 2008年3月3日, 下午7:03
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.javapatterns.adapter.classAdapter;
/** *//**
*
* @author rulinma
*/
public class test ...{
/** *//** Creates a new instance of test */
public test() ...{
}
public static void main(String[] args)
...{
Adapter a = new Adapter();
a.sampleOperation2();
a.sampleOperation1();
}
}
* test.java
*
* Created on 2008年3月3日, 下午7:03
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.javapatterns.adapter.classAdapter;
/** *//**
*
* @author rulinma
*/
public class test ...{
/** *//** Creates a new instance of test */
public test() ...{
}
public static void main(String[] args)
...{
Adapter a = new Adapter();
a.sampleOperation2();
a.sampleOperation1();
}
}
运行结果:
run-single:
This is 5v
this is 220v
生成成功(总时间:1 秒)
这样,我们就可以在使用Adaptee类的情况下通过Adapter的接口实现,从而得到新的功能。
其他文献:http://www.cn-java.com/www1/?action-viewnews-itemid-3518
- Java的适配器模式
- JAVA设计模式-类的适配器模式
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式(适配器模式)
- Java模式-适配器模式
- Java模式(适配器模式)
- SIGBUS和SIGSEGV
- MFC CArchive实现机制
- C++字符串完全指引一
- 新年最新的100句超牛的语言(转)
- avi的压缩
- Java的适配器模式
- 雪灾让我们认清韩日企业的“嘴脸”
- Java io 流:常见的io类组合形式
- 用户登录图片验证码
- JavaScript 中的类与继承(1)
- Order Overfill
- Oracle 基本数据类型 - Oracle 学习笔记 2
- Linux发行版图标大全
- 关闭U盘和移动硬盘自动播放的全套方法