zoukankan      html  css  js  c++  java
  • java常见的设计模式

    1、单例模式(为什么说这个很重要,面试时有、框架思想中也经常出现)

    //双重检查
    public class TestSingleTon {
        public static TestSingleTon testSingleTon=null;
        
        private TestSingleTon() {
            
        }
        public  static TestSingleTon  getInstance() {
            if(testSingleTon==null) {
                synchronized (TestSingleTon.class) {
                    if(testSingleTon==null) {
                        testSingleTon=new TestSingleTon();
                    }
                }            
            }
            return testSingleTon;
        }
    
    }

    2、代理模式

    懵逼中。懵逼树下你和我。。。

    代理模式其实很简单,就是中介公司。你需要得到目标对象,得靠代理对象获得。然后这个代理对象还可以帮你做其他的事。就像中介代替租客找房源,房间卫生、合同、物业都给

    解决了。

    关键点:代理对象是对目标对象的一个扩展,并会调用目标对象。

    (1)静态代理 

    public interface Excute {
        public void excute(); 
    }
    
    
    public class Target implements Excute{
    
        @Override
        public void excute() {
            System.out.println("目标方法被调用了!!!");
        }
    
    }
    
    
    public class StaticProxy implements Excute{
         
        public Excute excute;
        public StaticProxy(Excute excute) {
            this.excute=excute;
        }
        @Override
        public void excute() {
            System.out.println("目标方法执行之前!!!");
            excute.excute();
            System.out.println("目标方法执行之后!!!");
        }
    
    }
    
    public class Test {
        public static void main(String[] args) {
            StaticProxy staticProxy=new StaticProxy(new Target());
            staticProxy.excute();
        }
    
    }

      (2)动态代理

    public interface Excute {
        public void excute(); 
    }
    
    public class Target implements Excute{
    
        @Override
        public void excute() {
            System.out.println("目标方法被调用了!!!");
        }
    
    }
    
    public class DynamicProxy {
        private Object object;
        public DynamicProxy(Object object) {
            this.object=object;
        }
        public Object  getProxyInstance() {
             Object proxyInstance=Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),
                    new InvocationHandler() {                
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            // TODO Auto-generated method stub]
                            System.out.println("目标方法调用之前!!!");
                            Object invoke = method.invoke(object, args);
                            System.out.println("目标方法调用之后!!!");
                            return invoke;
                        }
                    });
             return proxyInstance;
        }
    
    }
    
    
    public class Test {
        public static void main(String[] args) {       
            Excute proxyInstance = (Excute) new DynamicProxy(new Target()).getProxyInstance();
            proxyInstance.excute();
            
        }
    
    }

      (3)Cglib代理 

    public interface Excute {
        public void excute(); 
    }
    
    
    public class Target implements Excute{
    
        @Override
        public void excute() {
            System.out.println("目标方法被调用了!!!");
        }
    
    }
    
    public class CglibProxy implements MethodInterceptor{
        private Object object;
        public CglibProxy(Object object) {
            this.object=object;
        }
        
        public Object getCglibProxyInstance() {
            Enhancer enhancer=new Enhancer();
            enhancer.setSuperclass(object.getClass());
            enhancer.setCallback(this);
            return enhancer.create();
        }
        @Override
        public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("目标方法被调用之前!!!");
            Object returnVal = arg1.invoke(object, arg2);
            System.out.println("目标方法被调用之后!!!");
            return returnVal;
        }
    
    }
    
    
    public class Test {
        public static void main(String[] args) {        
            Excute cglibProxy=(Excute) new CglibProxy(new Target()).getCglibProxyInstance();
            cglibProxy.excute();
        }
    
    }
  • 相关阅读:
    单文档程序结构
    如何从一个对话框弹出单文档视图
    MFC 窗口居中显示 VS2010
    在单文档中显示我的第一个对话框
    在MFC中添加用户自定义消息
    MFC动态创建控件及添加消息响应
    MFC的DLL
    MFC多线程
    MFC的UDP编程实现
    MFC下CSocket编程详解
  • 原文地址:https://www.cnblogs.com/Pzhenzhen/p/11115058.html
Copyright © 2011-2022 走看看