结构型模式

出处:https://blog.csdn.net/zhangerqing/article/details/8239539

一、适配器模式

  适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题 ,包括类的适配器模式、对象的适配器模式、接口
的适配器模式

  • 类的适配器模式

 

public class Source {

    public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable { /* 与原类中的方法相同 */
public void method1(); /* 新类的方法 */
public void method2();
} //实现接口
public class Adapter extends Source implements Targetable { @Override
public void method2() {
System.out.println("this is the targetable method!");
}
} //Adapter类继承Source类,实现Targetable接口,下面是测试类:
public class AdapterTest { public static void main(String[] args) {
Targetable target = new Adapter();
target.method1();
target.method2();
}
} //this is original method!
//this is the targetable method!
  • 对象的适配器模式

  将Adapter类作修改,这次不继承Source类,而是持有Source类的实例,以达到解决兼容性的问题

//修改adapter
public class Wrapper implements Targetable { private Source source; public Wrapper(Source source){
super();
this.source = source;
}
@Override
public void method2() {
System.out.println("this is the targetable method!");
} @Override
public void method1() {
source.method1(); //直接调用了对象的method1 进行重写
}
} //测试类:
public class AdapterTest { public static void main(String[] args) {
Source source = new Source();
Targetable target = new Wrapper(source);
target.method1();
target.method2();
}
}
  • 接口的适配器模式

 借助于一个抽象类,该抽象类实现了该接口所有的方法,而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法

//原接口
public interface Sourceable { public void method1();
public void method2();
} //抽象类Wrapper2实现了接口的方法,重写后方法体为空
public abstract class Wrapper2 implements Sourceable{ public void method1(){}
public void method2(){}
} //子类分别重写需要的方法
public class SourceSub1 extends Wrapper2 {
public void method1(){
System.out.println("the sourceable interface's first Sub1!");
}
}
public class SourceSub2 extends Wrapper2 {
public void method2(){
System.out.println("the sourceable interface's second Sub2!");
}
} public class WrapperTest { public static void main(String[] args) {
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2(); source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
} //the sourceable interface's first Sub1! method`()为空,用的abstract里的方法
//the sourceable interface's second Sub2!

 总结:

  • 类的适配器模式:当希望将转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
  • 对象的适配器模式:当希望将一个对象转换成满足另一个新接口对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
  • 接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

二、装饰模式

装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例

public interface Sourceable {
public void method();
}
//被装饰类
public class Source implements Sourceable { @Override
public void method() {
System.out.println("the original method!");
}
}
//装饰类实现接口
public class Decorator implements Sourceable { private Sourceable source; public Decorator(Sourceable source){
super();
this.source = source;
}
@Override
public void method() {
System.out.println("before decorator!");
source.method();
System.out.println("after decorator!");
}
} //测试类:
public class DecoratorTest { public static void main(String[] args) {
Sourceable source = new Source();
Sourceable obj = new Decorator(source);
obj.method();
}
} //before decorator!
//the original method!
//after decorator!

总结:装饰器模式适用于扩展一个类的功能,能够动态的为一个对象增加功能,而且还能动态撤销。(继承不能做到这一点,继承的功能是静态的,不能动态增删)但产生过多相似的对象,不易排错!

三、代理模式

代理模式就是找一个代理类替原对象进行一些操作

public interface Sourceable {
public void method();
}
public class Source implements Sourceable { @Override
public void method() {
System.out.println("the original method!");
}
}
public class Proxy implements Sourceable { private Source source;
public Proxy(){
super();
this.source = new Source();
}
@Override
public void method() {
before();
source.method();
atfer();
}
private void atfer() {
System.out.println("after proxy!");
}
private void before() {
System.out.println("before proxy!");
}
} //测试类:
public class ProxyTest { public static void main(String[] args) {
Sourceable source = new Proxy();
source.method();
} }
//before proxy!
//the original method!
//after proxy!

总结:如果在使用的时候需要对原有的方法进行改进,可以采用一个代理类调用原有方法,并且对产生的结果进行控制,这种方式就是代理模式。

    装饰器模式通常的做法是将原始对象作为一个参数传给装饰者的构造器,而代理模式通常在一个代理类中创建一个被代理类的对象 

    装饰器模式关注于在一个对象上动态的添加方法,然而代理模式关注于控制对对象的访问

四、外观模式

  将类与类之间的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口

public class CPU {

    public void startup(){
System.out.println("cpu startup!");
} public void shutdown(){
System.out.println("cpu shutdown!");
}
}
public class Memory { public void startup(){
System.out.println("memory startup!");
} public void shutdown(){
System.out.println("memory shutdown!");
}
}
public class Disk { public void startup(){
System.out.println("disk startup!");
} public void shutdown(){
System.out.println("disk shutdown!");
}
}
public class Computer {
private CPU cpu;
private Memory memory;
private Disk disk; public Computer(){
cpu = new CPU();
memory = new Memory();
disk = new Disk();
} public void startup(){
System.out.println("start the computer!");
cpu.startup();
memory.startup();
disk.startup();
System.out.println("start computer finished!");
} public void shutdown(){
System.out.println("begin to close the computer!");
cpu.shutdown();
memory.shutdown();
disk.shutdown();
System.out.println("computer closed!");
}
} public class User { public static void main(String[] args) {
Computer computer = new Computer();
computer.startup();
computer.shutdown();
}
} /*start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the computer!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!*/

五、桥接模式

  将抽象化与实现化解耦,使得二者可以独立变化

public interface Sourceable {
public void method();
} //分别定义两个实现类:
public class SourceSub1 implements Sourceable { @Override
public void method() {
System.out.println("this is the first sub!");
}
}
public class SourceSub2 implements Sourceable { @Override
public void method() {
System.out.println("this is the second sub!");
}
} //定义一个桥,持有Sourceable的一个实例:
public abstract class Bridge {
private Sourceable source; public void method(){
source.method();
} public Sourceable getSource() {
return source;
} public void setSource(Sourceable source) {
this.source = source;
}
}
public class MyBridge extends Bridge {
public void method(){
getSource().method();
}
} //测试类:
public class BridgeTest { public static void main(String[] args) { Bridge bridge = new MyBridge(); /*调用第一个对象*/
Sourceable source1 = new SourceSub1();
bridge.setSource(source1);
bridge.method(); /*调用第二个对象*/
Sourceable source2 = new SourceSub2();
bridge.setSource(source2);
bridge.method();
}
} //this is the first sub!
//this is the second sub!

六、组合模式

将多个对象组合在一起进行操作,常用于表示树形结构中,例如二叉树,数等

public class TreeNode {

    private String name;
private TreeNode parent;
private Vector<TreeNode> children = new Vector<TreeNode>(); public TreeNode(String name){
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public TreeNode getParent() {
return parent;
} public void setParent(TreeNode parent) {
this.parent = parent;
} //添加孩子节点
public void add(TreeNode node){
children.add(node);
} //删除孩子节点
public void remove(TreeNode node){
children.remove(node);
} //取得孩子节点
public Enumeration<TreeNode> getChildren(){
return children.elements();
}
}
public class Tree { TreeNode root = null; public Tree(String name) {
root = new TreeNode(name);
} public static void main(String[] args) {
Tree tree = new Tree("A");
TreeNode nodeB = new TreeNode("B");
TreeNode nodeC = new TreeNode("C"); nodeB.add(nodeC);
tree.root.add(nodeB);
System.out.println("build the tree finished!");
}
}

七、享元模式

  实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用。

   FlyWeightFactory负责创建和管理享元单元,当一个客户端请求时,工厂需要检查当前对象池中是否有符合条件的对象,如果有,就返回已经存在的对象,如果没有,则创建一个新对象,FlyWeight是超类

//数据库连接池源码
public class ConnectionPool { private Vector<Connection> pool; /*公有属性*/
private String url = "jdbc:mysql://localhost:3306/test";
private String username = "root";
private String password = "root";
private String driverClassName = "com.mysql.jdbc.Driver"; private int poolSize = 100;
private static ConnectionPool instance = null;
Connection conn = null; /*构造方法,做一些初始化工作*/
private ConnectionPool() {
pool = new Vector<Connection>(poolSize); for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
pool.add(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
} /* 返回连接的连接池 */
public synchronized void release() {
pool.add(conn);
} /* 返回连接池中的一个数据库连接 */
public synchronized Connection getConnection() {
if (pool.size() > 0) {
Connection conn = pool.get(0);
pool.remove(conn);
return conn;
} else {
return null;
}
}
}

  通过连接池的管理,实现了数据库连接的共享,不需要每一次都重新创建连接,节省了数据库重新创建的开销,提升了系统的性能!

最新文章

  1. 2014年百度之星程序设计大赛 - 初赛(第二轮)JZP Set
  2. 【转载】WEB前端开发规范文档
  3. SQLSERVER如何查看索引缺失
  4. ssh config host
  5. 替换Notes,K2 BPM为vivo打造新平台
  6. js跨域及解决方案
  7. 常州培训 day6 解题报告
  8. CodeColorer支持的语言
  9. 经典好文:android和iOS平台的崩溃捕获和收集
  10. 读《MacTalk&amp;#183;人生元编程》及Mac经常使用软件
  11. python (3):wxPython打包app,报错
  12. 操作数组的工具类Arrays
  13. 【足迹C++primer】48、函数引用操作符
  14. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)
  15. 浅谈服务间通信【MQ在分布式系统中的使用场景】
  16. 账号配置vue版本的扫码下单以及对店铺进行装修的步骤
  17. MQ消息队列之MSMQ
  18. 笨办法30Else 和 If
  19. 课程四(Convolutional Neural Networks),第二 周(Deep convolutional models: case studies) —— 2.Programming assignments : Keras Tutorial - The Happy House (not graded)
  20. 【原创】python模拟腾讯网页登录

热门文章

  1. Linux下RAID磁盘阵列的原理与搭建
  2. Docker学习(10) Docker的远程访问
  3. Python+Selenium学习笔记4 - submit&amp;get_attribute
  4. 使用Jprofiler分析Java项目的内存开销情况并利用强制回收控制内存
  5. RGB Color Codes Chart
  6. 深入理解ES8的新特性SharedArrayBuffer
  7. JAVA 进行图片中文字识别(准确度高)!!!
  8. 尚硅谷Java——宋红康笔记【day1-day5】
  9. springcloud webflux
  10. Sublime Text 4 破解笔记