设计模式-结构型模式-适配器
来源:互联网 发布:软件开发工程师学习班 编辑:程序博客网 时间:2024/06/09 08:54
结构型模式:结构型对象模式不是对接口或实现进行组合的.而是描述了如何对一些对象进行组合,从而实现新功能的一些方法.
adapter适合:将一个类的接口转换成客户所需要的另一个接口,使原本由于接口不兼容,不能一块工作的类多能一块工作.
使用情况:1、你想使用一个已经存在的类,而它的接口不符合你的要求。
2、你想创建一个可以复用的类,该类与其它的类或者接口协同工作。
代码:
package structure.adapter;
/**
* A interface
*/
public interface Shape {
public void Draw();
public void Border();
}
package structure.adapter;
/**
* The Adaptee in this sample
*/
public class Text {
private String content;
public Text() {
}
public void SetContent(String str) {
content = str;
}
public String GetContent() {
return content;
}
}
package structure.adapter;
/**
* The Class Adapter in this sample
*/
public class TextShapeClass extends Text implements Shape {
public TextShapeClass() {
}
public void Draw() {
System.out.println("Draw a shap ! Impelement Shape interface !");
}
public void Border() {
System.out.println("Set the border of the shap ! Impelement Shape interface !");
}
public static void main(String[] args) {
TextShapeClass myTextShapeClass = new TextShapeClass();
myTextShapeClass.Draw();
myTextShapeClass.Border();
myTextShapeClass.SetContent("A test text !");
System.out.println("The content in Text Shape is :" + myTextShapeClass.GetContent());
}
}
package structure.adapter;
/**
* The Object Adapter in this sample
*/
public class TextShapeObject implements Shape {
private Text txt;
public TextShapeObject(Text t) {
txt = t;
}
public void Draw() {
System.out.println("Draw a shap ! Impelement Shape interface !");
}
public void Border() {
System.out.println("Set the border of the shap ! Impelement Shape interface !");
}
public void SetContent(String str) {
txt.SetContent(str);
}
public String GetContent() {
return txt.GetContent();
}
public static void main(String[] args) {
Text myText = new Text();
TextShapeObject myTextShapeObject = new TextShapeObject(myText);
myTextShapeObject.Draw();
myTextShapeObject.Border();
myTextShapeObject.SetContent("A test text !");
System.out.println("The content in Text Shape is :" + myTextShapeObject.GetContent());
}
}
- 设计模式 - 结构型模式 - 适配器模式
- 设计模式->结构型模式->适配器模式
- 结构型设计模式---适配器
- 设计模式-结构型-适配器
- 设计模式-结构型模式-适配器
- 适配器模式(结构型设计模式)
- 设计模式--结构型--适配器模式
- 结构型设计模式---适配器模式(adapter)
- 设计模式-结构型之适配器模式
- 结构型设计模式-适配器模式
- 结构型设计模式之适配器模式
- 结构型设计模式-适配器模式
- 适配器模式(结构型设计模式)
- 设计模式 结构型设计模式之适配器模式
- 结构型设计模式---Adapter模式(适配器模式)
- 设计模式 一 适配器模式 Adapter(结构型模式)
- 设计模式(6) 结构型模式和适配器模式(ADAPTER)
- 一起学java设计模式--适配器模式(结构型模式)
- 包含子目录的makefile例子
- Linux音频编程指南
- zoj 2146
- unable to load bean jar包冲突
- 总结的一些jquery获取更改dom元素常用方法
- 设计模式-结构型模式-适配器
- 总结的一些jquery获取更改dom元素常用方法
- MYSQL SHOW的跟中犀利
- POSIX定时器:timer_settime()的使用
- 闲着无聊,做了个模拟几何画板网格吸附功能的程序
- sql 数字转化 学习心得。
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- Jquery提供了获得和设置元素的value的方法。如input, textarea等元素的value值。
- 原创:体验spring(三)