学习:java设计模式—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;
}
}
public interface Shape {
public Point getBottomLeftPoint();
public Point getTopRightPoint();
}
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;
}
}
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);
}
}
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 ..........");
}
}
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模式相关推荐
- 设计模式学习笔记——适配器(Adapter)模式
设计模式学习笔记--适配器(Adapter)模式 @(设计模式)[设计模式, 适配器模式, adapter, 适配器] 设计模式学习笔记适配器Adapter模式 基本介绍 适配器案例 类适配器模式 类 ...
- Java 设计模式 Adapter 对象适配器 模式
Java 设计模式 Adapter 对象适配器 模式 Adapter模式用于现有的程序无法直接使用,需要做适当的变换之后才能使用的情况. 涉及到的角色 Target对象:定义所需的方法. Client ...
- Java 设计模式——状态模式
概述 很多人在说状态模式的时候总拿策略模式来进行对比,可能他们的类图会有一点类似,可我却不认为他们有多么相像.你可以阅读<Java设计模式--策略模式>这篇博客,并与本文对比,以找到蛛丝马 ...
- java设计模式代理模式_Java中的代理设计模式
java设计模式代理模式 代理对象或代理对象为另一个对象提供占位符,以控制对该对象的访问. 代理充当原始对象的轻量级版本或简化版本. 它支持与原始对象相同的操作,但可以将那些请求委托给原始对象以实现它 ...
- java设计模式工厂模式_Java中的工厂设计模式
java设计模式工厂模式 Welcome to the Factory Design Pattern in Java tutorial. Factory Pattern is one of the C ...
- java设计模式工厂模式_Java中的复合设计模式
java设计模式工厂模式 Composite pattern is one of the Structural design pattern. Composite design pattern is ...
- java设计模式工厂模式_Java中的桥梁设计模式
java设计模式工厂模式 Today we will look into Bridge Design Pattern in java. When we have interface hierarchi ...
- java设计模式工厂模式_Java中的外观设计模式
java设计模式工厂模式 Facade Design Pattern is one of the Structural design patterns (such as Adapter pattern ...
- Java设计模式 -11- 外观模式(Facade模式)
Java设计模式 -11- 外观模式(Facade模式) 前言 外观模式的定义与特点 优点: 缺点: 外观模式的结构与实现 1. 模式的结构 2. 模式的实现 外观模式的应用实例 外观模式的应用场景 ...
- 设计模式-Adapter模式
目录 相关概念 对象适配器 类适配器 一个例子 总结 适配器模式(Adapter Pattern)是结构型模式.主要用来解决接口不兼容的问题,将一个类的接口变换成客户端所期待的另一种接口,从而使原本因 ...
最新文章
- 【NLP新闻-2013.06.03】New Book Where Humans Meet Machines
- Redis对象类型与编码
- laydate 时间控件去掉秒以及解决在移动端不能滑动的问题
- IncrediBuild 加速原理
- 在git 服务器挂载、创建新的项目、克隆新的项目
- 第003讲 无序列表 有序列表 框架
- db2数据库基础知识
- 华为NP课程笔记8-BGP2
- 引用百度新闻热门搜索html,百度新闻搜索技巧(之一)
- 一致 先验分布 后验分布_先验概率、似然函数与后验概率
- unity webgl优化
- 2+22+222+2222
- ACE1.0动态搜索框OpenHarmony组件开发大赛参与组件-Search_DialogJS
- 程序员面试的奇葩问题
- 向日葵远程操控的实现
- Telnet,DHCP,静态路由
- 数据结构——马踏棋盘题解(贪心算法)(C语言)
- Elastic Job 入门详解
- 采用业界最小的运算放大器设计麦克风电路
- 计算机第五章word试题及答案,职称计算机word练习试题及答案(4)