结构模式
适配器模式
-
类的适配器模式
创建一个
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; } }}