百知教育 - 孙帅 - 21_设计模式
01_单例模式
-
概念:
此类只创建一个对象,即为 单例模式 -
代码:
package day28; public class TestSingleton{ public static void main(String[] args) throws Exception{ ClassA a1 = ClassA.newInstance(); ClassA a2 = ClassA.newInstance(); System.out.println(a1 == a2); Thread t1 = new Thread(ClassB::newInstance); Thread t2 = new Thread(ClassB::newInstance); t1.start(); t1.join(); t2.start(); t2.join(); ClassC a3 = ClassC.newInstance(); ClassC a4 = ClassC.newInstance(); System.out.println(a3 == a4); } } //单例模式的饿汉式实现(浪费空间) class ClassA{ private static final ClassA instance = new ClassA(); public static ClassA newInstance(){ return instance; } private ClassA(){} } enum ClassA2{ //等价于ClassA INSTANCE } //单例模式的懒汉式实现(并发效率低) class ClassB{ private static ClassB instance = null; public static synchronized ClassB newInstance(){ if(instance == null) instance = new ClassB(); return instance; } private ClassB(){ System.out.println("ClassB()"); try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } } } //此单例模式的实现既保证了空间利用率又保证了并发效率 class ClassC{ private static class Holder{ public static final ClassC instance = new ClassC(); } public static ClassC newInstance(){ return Holder.instance; } private ClassC(){} }
-
运行结果:
02_工厂模式
-
理解:
在原有代码需要进行添加新类等修改时,通过修改配置文件,利用反射,在不修改原有代码的同时创建新的对象,保证原有代码的寿命。 -
代码:
package day28; import java.io.FileReader; import java.io.BufferedReader; import java.util.Properties; import java.io.IOException; public class TestFactory{ public static void main(String[] args){ Animal a = creatAnimal(); a.eat(); } static Animal creatAnimal(){ FileReader fr = null; try{ fr = new FileReader("TestFactory_config.txt"); Properties ps = new Properties(); ps.load(fr); String className = ps.getProperty("ClassName").trim(); Class c = Class.forName(className); Object o = c.newInstance(); return (Animal)o; }catch(Exception e){ return null; }finally{ try{ fr.close(); }catch(IOException e){ e.printStackTrace(); } } } } abstract class Animal{ public abstract void eat(); } class Dog extends Animal{ public void eat(){ System.out.println("Dog eat"); } } class Cat extends Animal{ public void eat(){ System.out.println("Cat eat"); } } class Tiger extends Animal{ public void eat(){ System.out.println("Tiger eat"); } }
-
运行结果:
03_代理模式之静态代理
-
关键:
- 目标类与代理类要 实现同一接口。
- 代理类中维护一个目标类的 引用。
-
优点:
各司其职、职责单一 -
代码:
package day28; public class TestProxy1{ public static void main(String[] args){ Service s = getService(1); s.m1(); s.m2(); s.m3(); } public static Service getService(int i){ Service target = new Target(); Service proxy = new Proxy(target); if(i == 0) return target; if(i == 1) return proxy; return null; } } interface Service{ void m1(); void m2(); void m3(); } class Target implements Service{ public void m1(){ System.out.println("Target m1"); } public void m2(){ System.out.println("Target m2"); } public void m3(){ System.out.println("Target m3"); } } class Proxy implements Service{ Service target; public Proxy(Service target){ this.target = target; } public void m1(){ long t1 = System.nanoTime(); target.m1(); long t2 = System.nanoTime(); System.out.println(t2-t1); } public void m2(){ long t1 = System.nanoTime(); target.m2(); long t2 = System.nanoTime(); System.out.println(t2-t1); } public void m3(){ long t1 = System.nanoTime(); target.m3(); long t2 = System.nanoTime(); System.out.println(t2-t1); } }
-
运行结果:
04_动态代理
-
代码:
package day28; import java.lang.reflect.Proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.util.List; import java.util.ArrayList; public class TestProxy2{ public static void main(String[] args){ /* Service t1 = new Target(); Service p1 = (Service)creatProxy(t1); p1.m1(); p1.m2(); p1.m3(); */ Service2 t2 = new Target2(); Service2 p2 = (Service2)creatProxy(t2); p2.m4(); p2.m5(); p2.m6(); System.out.println(); List list = new ArrayList(); List lp = (List)creatProxy(list); lp.add("hello"); lp.remove(0); } public static Object creatProxy(Object target){ Object o = Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler(){ public Object invoke(Object proxy , Method method , Object[] args){ long t1 = System.nanoTime(); try{ //调用目标对象的相应方法 Object result = method.invoke(target , args); //IllegalAccessException、InvocationTargetException long t2 = System.nanoTime(); System.out.println(t2-t1); return result; }catch(ReflectiveOperationException e){ return null; } } } ); return o; } } interface Service{ void m1(); void m2(); void m3(); } class Target implements Service{ public void m1(){ System.out.println("Target m1"); } public void m2(){ System.out.println("Target m2"); } public void m3(){ System.out.println("Target m3"); } } interface Service2{ void m4(); void m5(); void m6(); } class Target2 implements Service2{ public void m4(){ System.out.println("Target2 m4"); } public void m5(){ System.out.println("Target2 m5"); } public void m6(){ System.out.println("Target2 m6"); } }
-
运行结果: