常用的设计模式
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;iT 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 }