博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
结构型模式
阅读量:6328 次
发布时间:2019-06-22

本文共 6026 字,大约阅读时间需要 20 分钟。

hot3.png

结构模式

适配器模式

  • 类的适配器模式

    创建一个Source类,拥有一个方法待适配

    public class Source {  public void method1(){      System.out.println("method1");  }}

    创建目标接口Targetable

    public interface Targetable {  void method1();  void method2();}

    创建Adapter类,将Source类的功能扩展到Targetable

    public class Adapter extends Source implements Targetable {  @Override  public void method2() {      System.out.println("method2");  }}
  • 对象的适配器方式

    创建一个Source

    public class Source {  public void method1(){      System.out.println("method1");  }}

    创建目标接口Targetable

    public interface Targetable {  void method1();  void method2();}

    创建Adapter

    public class Adapter  implements Targetable {  private Source source;  public Adapter(Source source){      this.source=source;  }  @Override  public void method1() {      source.method1();  }  @Override  public void method2() {      System.out.println("method2");  }}
  • 接口的适配器模式

    创建目标接口Targetable

    public interface Targetable {  void method1();  void method2();}

    创建一个Source

    public abstract class Source implements Targetable {    public void method1(){    }  }

    创建Adapter

    public class Adapter  extends Source {    @Override    public void method2() {        System.out.println("method2");    }  }

装饰模式

  • 装饰模式

    创建Sourceable接口

    public interface Sourceable {    void method();}

    创建Source

    public class Source implements Sourceable {    @Override    public void method() {        System.out.println("the original method!");    }}

    创建Decorator

    public class Decorator implements Sourceable {    private Sourceable source;    public Decorator(Sourceable source) {        this.source = source;    }    @Override    public void method() {        System.out.println("before decorator!");        source.method();        System.out.println("after decorator!");    }}

代理模式

  • 代理模式

    创建Sourceable接口

    public interface Sourceable {     void method();}

    创建Source

    public class Source implements Sourceable {    @Override    public void method() {        System.out.println("the original method!");    }}

    创建Proxy

    public class Proxy implements Sourceable {    private Sourceable source;    public Proxy(Sourceable source) {        this.source = source;    }    @Override    public void method() {        System.out.println("before Proxy!");        source.method();        System.out.println("after Proxy!");    }}

外观模式

  • 外观模式 创建CPU

    public class CPU {  public void startup() {      System.out.println("cpu startup!");  }  public void shutdown() {      System.out.println("cpu shutdown!");  }}

    创建Disk

    public class Disk {  public void startup(){      System.out.println("disk startup!");  }  public void shutdown(){      System.out.println("disk shutdown!");  }}

    创建Memory

    public class Memory {  public void startup(){      System.out.println("memory startup!");  }  public void shutdown(){      System.out.println("memory shutdown!");  }}

    创建Computer

    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 interface Sourceable {     void method();}

    创建实现类

    public class Source implements Sourceable {    @Override    public void method() {        System.out.println("this is the Source");    }}

    创建抽象类

    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 TreeNode {    private String name;    private TreeNode parent;    private Vector
    children = new Vector
    (); 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
    getChildren(){ return children.elements(); }}

享元模式

  • 享元模式

    public class ConnectionPool {    private Vector
    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
    (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; } }}

转载于:https://my.oschina.net/chenbkit/blog/1609190

你可能感兴趣的文章
【HDOJ 3652】B-number
查看>>
android代码混淆笔记
查看>>
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
查看>>
BMP文件的读取与显示
查看>>
Flash文字效果
查看>>
各种排序算法总结篇(高速/堆/希尔/归并)
查看>>
使用c#訪问Access数据库时,提示找不到可安装的 ISAM
查看>>
Highcharts X轴纵向显示
查看>>
windows 注册表讲解
查看>>
【算法】论平衡二叉树(AVL)的正确种植方法
查看>>
35.angularJS的ng-repeat指令
查看>>
一些泛函分析题目
查看>>
基于DDD的现代ASP.NET开发框架--ABP系列之1、ABP总体介绍
查看>>
WCF 设计和实现服务协定(01)
查看>>
【原】东拼西凑PBR(1):PBR基础
查看>>
react 从零开始搭建开发环境
查看>>
scala recursive value x$5 needs type
查看>>
ps -ef |grep 输出的具体含义
查看>>
markdown编辑
查看>>
ASCII 在线转换器
查看>>