设计模式
Posted 艺海浮台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式相关的知识,希望对你有一定的参考价值。
1、单例模式
1 /** 2 * 单例模式 3 * @author feigu 4 * 5 */ 6 public class MonkeyKing { 7 /** 8 * 9 *懒汉式 10 */ 11 private static MonkeyKing instance; 12 public static Object lock=new Object(); 13 //空参构造私有化 14 private MonkeyKing(){}; 15 public static MonkeyKing getInstance(){ 16 if(instance==null){ 17 synchronized (lock) { 18 if(instance==null){ 19 instance=new MonkeyKing(); 20 } 21 return instance; 22 } 23 } 24 return instance; 25 26 } 27 /** 28 * 饿韩式 29 */ 30 private static MonkeyKing instance=new MonkeyKing(); 31 private MonkeyKing(){}; 32 public static MonkeyKing getInstance(){ 33 return instance; 34 } 35 }
2、Builder模式(链式编程)
1 public class PhoneBuilder{ 2 public void test1(){ 3 Phone p=new Phone() 4 .setBrand("三星") 5 .setName("苹果") 6 .setPrice(899) 7 .setProductArea("江苏"); 8 System.out.println(p.getName()); 9 10 } 11 } 12 13 class Phone { 14 private String name; 15 private String brand; 16 private String productArea; 17 private float price; 18 public String getName() { 19 return name; 20 } 21 public Phone setName(String name) {//有返回值注意 22 this.name = name; 23 return this; 24 } 25 public String getBrand() { 26 return brand; 27 } 28 public Phone setBrand(String brand) { 29 this.brand = brand; 30 return this; 31 } 32 public String getProductArea() { 33 return productArea; 34 } 35 public Phone setProductArea(String productArea) { 36 this.productArea = productArea; 37 return this; 38 } 39 public float getPrice() { 40 return price; 41 } 42 public Phone setPrice(float price) { 43 this.price = price; 44 return this; 45 } 46 47 }
3、适配器模式
1 public class TestWindow { 2 public static void main(String[] args) { 3 Window w = new Window(); 4 w.setWindowListener(new WindowAdapter(){ 5 public void onMax() { 6 System.out.println("hello"); 7 } 8 }); 9 } 10 } 11 12 //预实现 13 class WindowAdapter implements WindowListener{ 14 public void onMin() { 15 } 16 17 public void onMax() { 18 } 19 20 public void onClose() { 21 } 22 23 public void onResize() { 24 } 25 } 26 27 28 29 //接口 30 public interface WindowListener { 31 public void onMin(); 32 public void onMax(); 33 public void onClose(); 34 public void onResize(); 35 } 36 //窗口类 37 /** 38 * 窗口类 39 */ 40 public class Window { 41 42 private int size; 43 private int location; 44 45 public int getSize() { 46 return size; 47 } 48 49 public void setSize(int size) { 50 this.size = size; 51 } 52 53 public int getLocation() { 54 return location; 55 } 56 57 public void setLocation(int location) { 58 this.location = location; 59 } 60 61 public void setWindowListener(WindowListener l){ 62 l.onMax(); 63 } 64 }
4、工厂设计模式
1 /** 2 * 工厂设计模式 3 */ 4 public class TestFactory { 5 6 @Test 7 public void test1(){ 8 TVSet tv = TVSetFactory.productTV(); 9 System.out.println(tv.getBrand()); 10 } 11 } 12 13 /** 14 * 工厂设计模式 15 */ 16 public class TVSetFactory { 17 18 /** 19 * 静态工厂方法 20 */ 21 public static TVSet productTV(){ 22 TVSet tv = new TVSet(); 23 tv.setBrand("三星"); 24 tv.setName("xxx"); 25 tv.setPrice(500.f); 26 return tv ; 27 } 28 } 29 30 31 /** 32 * TVSet 33 */ 34 public class TVSet { 35 private String name; 36 private String brand; 37 private float price; 38 39 public String getName() { 40 return name; 41 } 42 43 public void setName(String name) { 44 this.name = name; 45 } 46 47 public String getBrand() { 48 return brand; 49 } 50 51 public void setBrand(String brand) { 52 this.brand = brand; 53 } 54 55 public float getPrice() { 56 return price; 57 } 58 59 public void setPrice(float price) { 60 this.price = price; 61 } 62 63 }
5、装饰模式
1 public class TestDecorate { 2 3 @Test 4 public void testFileWriter() throws Exception{ 5 //非缓冲区writer 6 FileWriter fw = new FileWriter("d:/arch/a.txt"); 7 fw.write("hello"); 8 fw.close(); 9 } 10 11 @Test 12 public void testBufferedWriter() throws Exception{ 13 FileWriter fw = new FileWriter("d:/arch/a.txt"); 14 //非缓冲区writer 15 BufferedWriter bw = new BufferedWriter(fw); 16 bw.write("hello"); 17 //bw.flush(); 18 bw.write("world"); 19 //bw.flush(); 20 21 bw.close(); 22 fw.close(); 23 } 24 }
6、
//连接池 public ConnectionPool(String driver,String url,String user,String password){ try { this.driver=driver; this.url=url; this.user=user; this.password=password; Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void init() { // TODO Auto-generated method stub for(int i=0;i<Max;i++){ try { //原生连接 Connection conn = DriverManager.getConnection(url, user, password); connections.add(new WrappedConnection(conn, this)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //回收连接 public synchronized void add(WrappedConnection c) { connections.add(c); } public synchronized Connection get() { if(connections.isEmpty()){ return null; } return connections.remove(0); } } //连接池测试 public class TestConnectionPool { public static void main(String[] args) { // TODO Auto-generated method stub MyDataSource ds=new MyDataSource(); try { Connection conn = ds.getConnection(); PreparedStatement pst = conn.prepareStatement("insert into student values(7,‘lis‘,‘henan‘)"); pst.executeUpdate(); pst.close(); conn = ds.getConnection(); pst = conn.prepareStatement("insert into student values(8,‘lise‘,‘henan‘)"); pst.executeUpdate(); pst.close(); conn = ds.getConnection(); pst = conn.prepareStatement("insert into student values(9,‘lisr‘,‘henan‘)"); pst.executeUpdate(); pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //池连接包装类 public class WrappedConnection extends ConnectionAdapter { private Connection rawConn; private ConnectionPool pool; public WrappedConnection(Connection rawConn, ConnectionPool pool) { super(); this.rawConn = rawConn; this.pool = pool; } @Override public void commit() throws SQLException { rawConn.commit(); } @Override public void rollback() throws SQLException { rawConn.rollback(); } //关闭连接 @Override public void close() throws SQLException { pool.add(this); rawConn.close(); } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { return rawConn.prepareStatement(sql); } } //适配器 public class ConnectionAdapter implements Connection { @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } 。。。。 } //数据源 public class MyDataSource implements DataSource { private ConnectionPool pool; public MyDataSource() { String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/big"; String user="root"; String password="root"; pool=new ConnectionPool(driver,url,user,password); //初始化连接池 pool.init(); } @Override public Connection getConnection() throws SQLException { // TODO Auto-generated method stub return pool.get(); } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub }
以上是关于设计模式的主要内容,如果未能解决你的问题,请参考以下文章