- 什么事设计模式
- 单例模式
单例对象的类必须保证只有一个实例的存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如全局信息配置。 但是何时产生实例,不好判断。
这个实例在getInstance第一次被方位的时候创建实例。
public class Singleton { private Singleton(){ System.out.println("single is create"); } private static Singleton instance = new Singleton(); public static Singleton getInstance(){ return instance; } } }以下实例也会被创建
public class Singleton2 { public static int STATUS = 1; private Singleton2() { System.out.println("single is create"); } private static Singleton2 instance = new Singleton2(); public static Singleton2 getInstance() { return instance; } public static void main(String[] args) { System.out.println(Singleton2.STATUS); } }
single is create
1
可以使用同步的方法,只有getInstance情况下才调用实例。采用延迟加载的机制,但是在高并发会影响效率public class LazySingleton { public static int STATUS = 1; private LazySingleton() { System.out.println("single is create"); } private static LazySingleton instance = null; public static synchronized LazySingleton getInstance() { if (instance == null) instance = new LazySingleton(); return instance; } // public static void main(String[] args) { // System.out.println(LazySingleton.STATUS); // } }使用静态内部类,当外部类被初始化的时候,静态内部类并不能被初始化,只有调用getInstance的时候静态内部类才能够被初始化。
public class StaticSingleton { public static int STATUS = 1; private StaticSingleton() { System.out.println("single is create"); } private static class SingletonInner { private static StaticSingleton instance = new StaticSingleton(); } public static StaticSingleton getInstance() { return SingletonInner.instance; } public static void main(String[] args) { System.out.println(StaticSingleton.STATUS); } }
- 不变模式
一个类创建后,再整个生命周期都不会发生变化,就是不变类。不变模式不需要同步。
public final class Pruduct {//确保无子类 private final String no; private final String name; private final double price; //创建对象,必须指定数据,创建后就无法进行修改。 public Pruduct(String no, String name, double price) { this.no = no; this.name = name; this.price = price; } public String getNo() { return no; } public String getName() { return name; } public double getPrice() { return price; } }不变模式
java.lang.String
java.lang.Boolean
java.lang.Byte
java.lang.Character
java.lang.Double
java.lang.Float
java.lang.Integer
java.lang.Long
java.lang.Short
- Future模式
package designPattern; public class Client { public Data request(final String querystr){ final FutureData future = new FutureData(); new Thread(){ public void run(){ RealData realData = new RealData(querystr); future.setRealData(realData); } }.start(); return future; } public static void main(String[] args) { Client c = new Client(); Data data = c.request("alan"); System.out.println("请求完毕"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(data.getResult()); } }
package designPattern; public class FutureData implements Data { protected RealData realData = null; protected boolean isReady = false; public synchronized void setRealData(RealData realdata) { if (isReady) { return; } this.realData = realData; isReady = true; notifyAll(); } @Override public String getResult() { while (!isReady) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } return realData.result; } }
package designPattern; public class RealData implements Data { protected final String result; public RealData(String para) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < 10; i++) { sb.append(para); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } result = sb.toString(); } @Override public String getResult() { return result; } }
package designPattern; public interface Data { public String getResult(); }
- 生产者消费者
是一种非常经典的多线程设计模式。它为多线程间的协作提供了良好的解决方案。生产者和消费者之间通过共享内存缓冲区进行通信。