设计模式
六大原则
- 开闭原则
- 扩展性
- 易于修改
- 里式代换原则
- 依赖倒转原则
- 接口隔离原则
常见的几种设计模式
-
单例模式
懒汉式:
class Singleton{ static private Singleton singleton = null; public Singleton getSingleton() { if (singleton == null) {// 第一层上锁 synchronized (Singleton.class) { if(singleton==null) // 第二层上锁 双重检验锁 singleton = new Singleton(); } } //当对象不是Null时候不用考虑线程安全了 return singleton; } }
饿汉式:
class Singleton{ static private Singleton singleton = new Singleton(); public Singleton getSingleton() { if (singleton == null) {// 第一层上锁 synchronized (Singleton.class) { if(singleton==null) // 第二层上锁 双重检验锁 singleton = new Singleton(); } } //当对象不是Null时候不用考虑线程安全了 return singleton; } }
-
工厂模式
简单工厂:
interface Car {void run();} class AoDi implements Car { @Override public void run() { System.out.println("我是奥迪汽车---我是奥迪A6l");}} class BenChi implements Car { @Override public void run() { System.out.println("我是奔驰汽车----我是奔驰E级");}} // 奔驰工厂 class BenChiFatory { static public Car createCar() { return new BenChi(); }} class AoDiFatory { static public Car createCar() { return new AoDi(); }} class CarFatory { static public Car createCar(String name) { Car car = null; switch (name) { case "奥迪": car = new AoDi(); break; case "奔驰": car = new BenChi(); break; default: break; } return car; } } public class Test005 { public static void main(String[] args) { Car car = BenChiFatory.createCar(); car.run(); // Car car = CarFatory.createCar("奔驰"); // car.run(); } }
抽象工厂:
-
代理模式
静态代理:
class YuShengJun implements Hose { @Override public void maifang() { System.out.println("我是余胜军,终于可以买房啦!!!"); } } // 静态代理 class Proxy implements Hose { // 代理对象 private YuShengJun yuShengJun; public Proxy(YuShengJun yuShengJun) { this.yuShengJun = yuShengJun; } @Override public void maifang() { System.out.println("我是中介,你买房开始交给我啦!!!"); yuShengJun.maifang(); System.out.println("我是中介,你买房开结束啦!!!"); } }
动态代理:
// jdk动态代理 class JdkProxy implements InvocationHandler { private Object tarjet; JdkProxy(Object tarjet) { this.tarjet = tarjet; } @Override public Object invoke(Object paramObject, Method paramMethod, Object[] paramArrayOfObject) throws Throwable { System.out.println("我是中介,你买房开始交给我啦!!!"); Object invoke = paramMethod.invoke(tarjet, paramArrayOfObject); System.out.println("我是中介,你买房开结束啦!!!"); return invoke; } } //cglib 动态代理 class Cglib implements MethodInterceptor{ @Override public Object intercept(Object oj, Method method, Object[] arrays, MethodProxy methodProxy) throws Throwable { System.out.println("我是中介,你买房开始交给我啦!!!"); Object invokeSuper = methodProxy.invokeSuper(oj, arrays); System.out.println("我是中介,你买房开结束啦!!!"); return invokeSuper; } } public class Test006 { public static void main(String[] args) { // Hose hose = new Proxy(new YuShengJun()); // hose.maifang(); YuShengJun yuShengJun = new YuShengJun(); // JdkProxy jdkProxy = new JdkProxy(yuShengJun); // Hose hose = (Hose) java.lang.reflect.Proxy.newProxyInstance(yuShengJun.getClass().getClassLoader(), yuShengJun.getClass().getInterfaces(), jdkProxy); // hose.maifang(); Cglib cglib = new Cglib(); Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(YuShengJun.class); enhancer.setCallback(cglib); Hose hose = (Hose) enhancer.create(); hose.maifang(); } }