博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式
阅读量:4963 次
发布时间:2019-06-12

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

 

常用的设计模式

singleton           //单例模式decorate            //装饰模式adapter             //适配器模式factory             //工厂模式MVC                 //model - view - controller,模型(数据)-视图(界面)-控制器Pool                //池化Proxy               //代理

 

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  * TVSet33  */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     @Test12     public void testBufferedWriter() throws Exception{13         FileWriter fw = new FileWriter("d:/arch/a.txt");14         //非缓冲区writer15         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
T unwrap(Class
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 }

 

转载于:https://www.cnblogs.com/yihaifutai/p/6785519.html

你可能感兴趣的文章
Bear and Blocks (dp+思维)
查看>>
jQuery源码分析第一章---匿名函数
查看>>
高精度求A*B(FFT)
查看>>
hdu 6298 Maximum Multiple(规律)
查看>>
汇总java生态圈常用技术框架、开源中间件,系统架构及经典案例等
查看>>
使用Servlet上传文件
查看>>
PHP数据类型转换
查看>>
Promise深入浅出之个人拙见
查看>>
GlusterFS群集存储项目
查看>>
Maven的POM简单理解
查看>>
Jenkins中的Job配置里缺少“触发远程构建(例如,使用脚本)”选项的问题解决...
查看>>
GitLab修改时区
查看>>
翻译: Clustered Index Design Considerations 聚集索引设计注意事项
查看>>
Laravel Session保存机制和terminate中间件
查看>>
org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.ap解决方案
查看>>
以checked选中作为判断条件的各种写法
查看>>
Linux ln命令 - 建立文件/目录链接
查看>>
Httpservletrequest
查看>>
Jquery.ajax报parseerror Invalid JSON错误的原因和解决方法:不能解析
查看>>
杭电2602 Bone Collector
查看>>