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一起记录分享自己 ...
随机推荐
- 权限管理数据库设计_Rev1
贴出来自身接触项目以来所接触过的一些企业管理信息系统权限部门的一个通用数据库设计初稿: 设计的文字解释以及各部分的作用等确定可行会再进行描述: 图: 如果有不同意见请轻拍!
- ios App 加急审核
下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...
- JPA2 关于 PagingAndSortingRepository
And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd): Or --- 等价于 SQL 中的 ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
- Form表单学习网站
HTML表单 -- form标签 -- 与浏览者交互:http://www.dreamdu.com/xhtml/tag_form/
- 团体程序设计天梯赛-练习集L1-018. 大笨钟
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜 ...
- C/C++ 框架,类库,资源集合
很棒的 C/C++ 框架,类库,资源集合. Awesome C/C++ Standard Libraries Frameworks Artificial Intelligence Asynchrono ...
- Eclipse不能自动编译 java文件的解决方案
前段时间出现了eclipse 不自动编译java文件的问题,在网上找了好长时间,总算把问题解决了,现在把这个问题的解决方法总结一下. 1,看看project -- Build Automaticall ...
- [itint5]下一个排列
http://www.itint5.com/oj/#6 首先,试验的时候要拿5个来试,3,4个都太少了.好久没做所以方法也忘了,是先从后往前找到第一个不合顺序的,然后在后面找到比这个大的最小的来交换, ...
- SOCKS5协议
SOCKS5 是一个代理协议,这种协议对本身所代理的内容并不关心,可用于穿越防火墙. 例如我有一台web服务器,用户可以登陆上去查询公司的关键数据,这样的服务器我肯定是不想放到公网上让别人能随便访问, ...