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一起记录分享自己 ...
随机推荐
- iOS 通览(二)
一.关键词 extern:C语言的函数外部声明. 如果你要在一个.c或者.m中使用另外一个.c文件的函数的话,需要在文件中写入目标函数的外部引用的声明. 二.自定义View 自定义View添加控件对象 ...
- 【BZOJ 1010】 [HNOI2008]玩具装箱toy
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- 微软职位内部推荐-Pricipal Dev Manager for Application Ecosystem & Service
微软近期Open的职位: Location: China, BeijingDivision: Operations System Group Engineering Group OverviewOSG ...
- python 新时代
今天看到有关python的文章,感觉很好奇,学了python很久了,但是还没有真正的用过,只是写一些小程序 看了这篇文章以后真的感觉自己所了解都是皮毛,在此与大家分享: 原文链接:http://www ...
- c# DirectoryInfo类 详解
DirectoryInfo类和Directory类之间的关系与FileInfo类和File类之间的关系十分类似.下面介绍一下DirectoryInfo类的常用属性. DirectoryInfo类的常用 ...
- ios App 加急审核
下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...
- WinForm控件选择器
jQuery和Css的控件选择器用起来非常畅快,相信用过的人都会有这种感觉,而WinForm则是通过Name来实现窗体中控件的选择,在选择单个控件的时候是很方便,但是当选择具有一类特征的控件时,则显得 ...
- 读书笔记 (二) ———Fundamentals of Multiagent Systems with NetLogo Examples by Prof. Jose M Vidal
chapter 2 分布式约束1 分布式约束满足 1.1 过滤算法 1.2 基于归结的调和算法 consistency 1.3 异步回溯 1.4 异步弱承诺? 1.5 分布式突破?2 分布式受限优化 ...
- javascript中跨源资源共享
来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(十) 通过XHR实现Ajax通信的一个主要限制,来源于跨域安全策略.默认情况下,XHR对 ...
- uva 10271
DP 状态转移方程 dp[i][j] = min(dp[i-1][j], dp[i-2][j-1] + w)) dp[i][j] 指的是前i个筷子组成j组所花费的最小值 考虑第i个筷子是否参与第j组 ...