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

    继承设计模式

    //公司类
    package edu.chinasoft.spring01;
    
    public class Company extends XiaoMing {
        private XiaoWu  xiaoWu=new XiaoWu();
        @Override
        public void yaoZhang() {
            xiaoWu.before();
            super.yaoZhang();
            xiaoWu.after();
        }
    }
    
    
    //小明类
    package edu.chinasoft.spring01;
    
    public class XiaoMing {
        public void yaoZhang(){
            System.out.println("还我100元!");
        }
    }
    
    //小武类
    package edu.chinasoft.spring01;
    
    public class XiaoWu {
        public void before(){
            System.out.println("恐吓一下!");
        }
        public void after(){
            System.out.println("警告不要报警!");
        }
    }

    装饰设计模式

    //公司类
    package edu.chinasoft.spring02;
    
    
    public class Company {
        private XiaoMing xiaoMing;
        private XiaoWu xiaoWu=new XiaoWu();
    
    
        public Company() {
        }
        //以构造函数参数 的形式将小明传进来
        public Company(XiaoMing xiaoMing) {
            this.xiaoMing = xiaoMing;
        }
    
        public void yaoZhang() {
            xiaoWu.before();
            this.xiaoMing.yaoZhang();
            xiaoWu.after();
        }
    }
    
    
    
    //小明类
    package edu.chinasoft.spring02;
    
    public class XiaoMing {
        public void yaoZhang() {
            System.out.println("还我100元!");
        }
    }
    
    
    //小武类
    package edu.chinasoft.spring02;
    
    public class XiaoWu {
        public void before(){
            System.out.println("恐吓一下!");
        }
        public void after(){
            System.out.println("警告不要报警!");
        }
    }

    静态代理设计模式

    //接口要账
    package edu.chinasoft.spring03;
    
    public interface YaoZhang {
        void yaoZhang();
    }
    
    
    
    //公司类
    package edu.chinasoft.spring03;
    
    
    public class Company implements YaoZhang{
        private XiaoMing xiaoMing;
        private XiaoWu xiaoWu=new XiaoWu();
    
    
        public Company() {
        }
    
        public Company(XiaoMing xiaoMing) {
            this.xiaoMing = xiaoMing;
        }
    
        @Override
        public void yaoZhang() {
            xiaoWu.before();
            this.xiaoMing.yaoZhang();
            xiaoWu.after();
        }
    }
    
    
    //小明类
    package edu.chinasoft.spring03;
    
    public class XiaoMing implements YaoZhang {
        @Override
        public void yaoZhang() {
            System.out.println("还我100元!");
        }
    }
    
    
    
    //小武类
    package edu.chinasoft.spring03;
    
    public class XiaoWu {
        public void before(){
            System.out.println("恐吓一下!");
        }
        public void after(){
            System.out.println("警告不要报警!");
        }
    }

    动态代理设计模式

    //要账接口
    package edu.chinasoft.spring04;
    
    public interface YaoZhang {
        void yaoZhang();
    }
    
    
    
    //公司工厂类  用来创建要账公司
    package edu.chinasoft.spring04;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    //创建要账公司  创建关于 target代理类
    public class CompanyFactory implements InvocationHandler {
        private  YaoZhang  yaoZhang;           //target
        private  XiaoWu xiaoWu=new XiaoWu();
    
        public CompanyFactory(YaoZhang yaoZhang) {
            this.yaoZhang = yaoZhang;
        }
        //通过j2se里的常用类 Proxy创建target的代理类
        public YaoZhang  createCompany(){
            //创建关于该对象yaoZhang的代理类
            YaoZhang o = (YaoZhang)Proxy.newProxyInstance(yaoZhang.getClass().getClassLoader(), yaoZhang.getClass().getInterfaces(), this);
            return  o;
        }
    
        //设计代理类里的功能
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            this.xiaoWu.before();
            Object result = method.invoke(this.yaoZhang, args);    //执行target类里任何方法
            this.xiaoWu.after();
            return result;
        }
    }
    
    
    
    
    //小明类
    package edu.chinasoft.spring04;
    
    public class XiaoMing implements YaoZhang {
        @Override
        public void yaoZhang() {
            System.out.println("还我100元!");
        }
    }
    
    
    
    //小武类
    package edu.chinasoft.spring04;
    
    public class XiaoWu {
        public void before(){
            System.out.println("恐吓一下!");
        }
        public void after(){
            System.out.println("警告不要报警!");
        }
    }

    单例设计模式

    //People01类
    package edu.chinasoft.spring02;
    
    import org.apache.log4j.Logger;
    
    public class People01 {
        private static People01 install = new People01();
        private  Logger logger = Logger.getLogger(People01.class);
        private People01() {
            logger.debug("people01里的私有构造函数执行 ……");
        }
    
        public static People01 getInstatll() {
            System.out.println("people01里的getInstall方法执行……");
            return install;
        }
    
    }
    
    
    //People02类
    package edu.chinasoft.spring02;
    
    public class People02 {
        private static People02 install = null;
    
        private People02() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public static  synchronized People02 getInstatll() {
            if (install == null) {
                install = new People02();
            }
    
            return install;
        }
    }

    工厂设计模式

    //水果工厂类
    package edu.chinasoft.spring03;
    
    import org.apache.log4j.Logger;
    
    public class FruitsFactory {
        private Logger logger = Logger.getLogger(FruitsFactory.class);
        public Pear  createPear() {
            logger.debug("类FruitsFactory里的方法createPear执行……");
            return new Pear();
        }
        
        public Apple  createApple() {
            return new Apple();
        }
    
    }
    
    
    
    //苹果
    package edu.chinasoft.spring03;
    
    public class Apple {
    
        @Override
        public String toString() {
            return "我是苹果!";
        }
        
        
    
    }
    
    
    //
    package edu.chinasoft.spring03;
    
    public class Pear {
    
        @Override
        public String toString() {
            return "我是梨!";
        }
    
    }
  • 相关阅读:
    PG-日常管理
    PG-高可用(pgpool-Ⅱ)
    PG-基准测试
    PG-备份恢复
    PG-并发问题
    Go-常量
    Go-变量
    Oracle-11g升级PSU补丁
    Oracle-`sqlplus`工具使用技巧
    [CF1051F] The Shortest Statement
  • 原文地址:https://www.cnblogs.com/bailanglang/p/13593764.html
Copyright © 2011-2022 走看看