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一起记录分享自己 ...
随机推荐
- PS 颜色表大全-颜色中文名(1)
颜色中文名 鸨色#f7acbc 赤白橡#deab8a 油色#817936 绀桔梗#444693 踯躅色#ef5b9c 肌色#fedcbd 伽罗色#7f7522 花色#2b4490 桜色#feeeed ...
- Entity Framework 学习笔记(一)安装
1.通过 VS2013 下载,这个没有限制,因为我用的vs是2013 Entity Framework包
- 【BZOJ 3172】 [Tjoi2013]单词
Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整数N,表示有多少个单词,接下来N ...
- java第六课 oop
java oop 1.面向过程的结构化程序设计弊端:方法和数据结构都是毫无规律的定义在程序中任何位置 方法定义和方法要处理的数据结构也都是分开定义 2.对象:每new一次,就创建1个新对 ...
- 使用C#选择文件夹、打开文件夹、选择文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- EF+lambda表达式 实现LIKE模糊查询
s => s.XianWID.StartsWith(str) 匹配以str开头的 s => s.XianWID.EndsWith(str) 匹配以str结尾的 s => s.Xian ...
- poj 3440 Coin Toss 概率问题
这题主要是推导数学公式!!! 将概率问题转化为圆心所在的面积! 代码如下: #include<iostream> #include<stdio.h> #include<a ...
- linux查看磁盘使用情况
# 查看磁盘使用情况 $ df -l # 查看某个目录在哪个分区,比如查看/root文件夹在哪个分区 $ df /root # 查看linux系统具体分区情况 $ fdisk -l
- if语句写在while语句外面效率更高
为了排除某些特殊的文件后缀名,一开始我自然而然的这样写,判断每一个文件的后缀名: // 去除后缀名 foreach (const QString &strKey, local_map.keys ...
- SQL 两张结构一样的表合并查询 .
select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...