java设计模式—Adapter模式
1、核心意图:
package qinysong.pattern.adapter;
public class Point {
private int coordinateX;
private int coordinateY;
public Point(int coordinateX, int coordinateY){
this.coordinateX = coordinateX;
this.coordinateY = coordinateY;
}
public String toString(){
return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";
}
public int getCoordinateX() {
return coordinateX;
}
public int getCoordinateY() {
return coordinateY;
}
}
package qinysong.pattern.adapter;
public interface Shape {
public Point getBottomLeftPoint();
public Point getTopRightPoint();
}
package qinysong.pattern.adapter;
public class TextView {
public int getCoordinateX() {
System.out.println("TextView.getCoordinateX()...");
return 10;
}
public int getCoordinateY() {
System.out.println("TextView.getCoordinateY()...");
return 20;
}
public int getHeight() {
System.out.println("TextView.getHeight()...");
return 30;
}
public int getWidth() {
System.out.println("TextView.getWidth()...");
return 40;
}
public boolean isEmpty(){
return false;
}
}
package qinysong.pattern.adapter;
public class TextShape implements Shape {
private TextView textView;
public TextShape(TextView textView){
this.textView = textView;
}
//通过TextView的实例进行协调实现
public Point getBottomLeftPoint() {
System.out.println("TextShape.getBottomLeftPoint()...");
int coordinateX = textView.getCoordinateX();
int coordinateY = textView.getCoordinateY();
return new Point(coordinateX, coordinateY);
}
//通过TextView的实例进行协调实现
public Point getTopRightPoint() {
System.out.println("TextShape.getTopRightPoint()...");
int coordinateX = textView.getCoordinateX();
int coordinateY = textView.getCoordinateY();
int height = textView.getHeight();
int width = textView.getWidth();
return new Point(coordinateX + width, coordinateY + height);
}
}
package qinysong.pattern.adapter;
public class Client {
public static void main(String[] args){
System.out.println("Client.main begin ..........");
System.out.println("Client.main 以下是通过实例委托方式实现的Adapter");
Shape shape = new TextShape(new TextView());
Point bottomLeft = shape.getBottomLeftPoint();
Point topRight = shape.getTopRightPoint();
System.out.println("Client.main shape's bottomLeft:" + bottomLeft);
System.out.println("Client.main shape's topRight:" + topRight);
System.out.println(" Client.main 以下是通过类继承方式实现的Adapter");
Shape shape2 = new TextShape2();
bottomLeft = shape2.getBottomLeftPoint();
topRight = shape2.getTopRightPoint();
System.out.println("Client.main shape2's bottomLeft:" + bottomLeft);
System.out.println("Client.main shape2's topRight:" + topRight);
System.out.println("Client.main end ..........");
}
}
package qinysong.pattern.adapter;
public class TextShape2 extends TextView implements Shape {
//通过所继承的TextView,进行协调实现
public Point getBottomLeftPoint() {
System.out.println("TextShape2.getBottomLeftPoint()...");
int coordinateX = getCoordinateX();
int coordinateY = getCoordinateY();
return new Point(coordinateX, coordinateY);
}
//通过所继承的TextView,进行协调实现
public Point getTopRightPoint() {
System.out.println("TextShape2.getTopRightPoint()...");
int coordinateX = getCoordinateX();
int coordinateY = getCoordinateY();
int height = getHeight();
int width = getWidth();
return new Point(coordinateX + width, coordinateY + height);
}
//注意: 这一点体现了类模式的优势,可以很方便地重定义父类TextView中的方法
public int getCoordinateX() {
System.out.println("TextShape2.getCoordinateX()...");
return 100;
}
}
java设计模式—Adapter模式的更多相关文章
- Java设计模式——组合模式
JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...
- java设计模式--单列模式
java设计模式--单列模式 单列模式定义:确保一个类只有一个实例,并提供一个全局访问点. 下面是几种实现单列模式的Demo,每个Demo都有自己的优缺点: Demo1: /** * 单列模式需要满足 ...
- 3.java设计模式-建造者模式
Java设计模式-建造者模式 在<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal repr ...
- Java设计模式-代理模式之动态代理(附源代码分析)
Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...
- Java设计模式——外观模式
JAVA 设计模式 外观模式 用途 外观模式 (Facade) 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式是一种结构型模式. 结构
- 【设计模式】Java设计模式 -工厂模式
[设计模式]Java设计模式 -工厂模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目 ...
- 【设计模式】Java设计模式 - 原型模式
[设计模式]Java设计模式 - 原型模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起 ...
- 【设计模式】Java设计模式 - 桥接模式
[设计模式]Java设计模式 - 桥接模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起 ...
- 【设计模式】Java设计模式 - 组合模式
Java设计模式 - 组合模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自己 ...
随机推荐
- how to get sharepoint lookup value
SPFieldLookup lookUp1 = properties.ListItem.ParentList.Fields.GetField("Leave_x0020_Type") ...
- Vue引发的getter和setter
Vue引发的getter和setter 公司的新项目决定使用Vue.js来做,当我打印出Vue实例下的data对象里的属性时,发现了一个有趣的事情: 它的每个属性都有两个相对应的get和set方法,我 ...
- 基于Vuforia的Hololens图像识别
微软官方Hololens开发文档中有关于Vuforia的内容,https://developer.microsoft.com/en-us/windows/holographic/getting_sta ...
- linux 批量重命名文件
模拟结果文件路径结构大概是:/当前目录/模型名/字模型名模拟/模拟温度/模拟结果文件. 模拟结果文件命名时相同的.模拟结果文件需要修改模拟结果文件的后缀名. 附shell脚本: find -type ...
- [转载]用.NET开发的磁力搜索引擎——Btbook.net
去年10月份开始研究相关的协议与资料,中途乱七八糟的事情差点没坚持下来,寒假里修修补补上礼拜把Btbook发布了,经过社交网络的推广之后,上线第三天UV就达到了两万多,也算是对这几个月工作的一点肯定吧 ...
- 团体程序设计天梯赛-练习集L1-018. 大笨钟
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜 ...
- OneAPM 云监控部署与试用体验
作为 Zabbix 骨灰级粉丝,一直以来对第三方监控(APM)都是拒绝的.一来觉得收费,二来担心数据被人所知,三来觉得 Zabbix 牛逼到无可取代.但是,随着 APM 市场的火爆,我决定「放下身段」 ...
- 想要上市,SaaS 企业应该重点关注什么?(下)
前言:那些非常期待能在纳斯达克敲钟的 SaaS 服务提供商们,希望能从已经上市的「前辈」身上学到一些东西.对企业的销售主管来说,他们控制着影响整个公司长期收益的多个因素,同时,他们也对潜在投资者和金融 ...
- FF浏览器来帮助我们录制脚本
有时我们录制一个页面的脚本,我们需要知道这个页面哪些请求是耗时最大的?这个时候FF浏览器的网络分析功能就可以派上用场了,打开火狐浏览器按F12: 点击重新载入,可以看到下面的信息: 看到最耗时的操作了 ...
- http://www.cnblogs.com/doit8791/p/4093808.html
http://www.cnblogs.com/doit8791/p/4093808.html