zoukankan      html  css  js  c++  java
  • 总结设计模式—(大话设计模式上篇)

      1、简单工厂模式、工厂模式与抽象工厂模式

    public interface Human {  
        public void say();
    }
    class Man implements Human {
        public void say() {
            System.out.println("男人");
        }
    }
    class Woman implements Human {
        public void say() {
            System.out.println("女人");
        }
    }

         简单工厂模式:简单来说 ,在工厂类中添加判断方法返回对应的类; 

                 缺点 违背开放封闭原则,需要经常修改工厂类

     class SampleFactory {
      public static Human makeHuman(String type){
         if(type.equals("man")){
          return new Man();
         }else if(type.equals("womman")){
          return new Woman();
         } 
      } 
    }
    

               

           工厂模式: 将逻辑判断拿到客户端执行

    interface Factory {
        public Human crateMan();    
    }
    class ManFactory implements Factory{
        public Human crateMan() {
            return new Man();
        }
    }
    class WomanFactory implements Factory{
    
        public Human crateMan() {
            return new Woman();
        }
    }
    class NormalFatory{
      
    public static void main(String[] args) {
    Factory factory=new ManFactory();
         Human man2=factory.crateMan();
            man2.say();  
        }

    }

           抽象工厂模式:因为实际业务中,通过客户端添加逻辑代码非常长,而且不容易修改。现在可以用子类抽象工厂来组装分离不同的业务。    

    interface Car {
        void gotowork();
    }
    class Bus implements Car {
        public void gotowork() {
            System.out.println("坐公交车去上班!");
        }
    }

    class Train implements Car {
        public void gotowork() {
            System.out.println("坐火车去上班!");
        }
    }
    
    
    interface AbstractFactory {
      Human getHuman();
      Car getCar();
    }
    class ManFactory implements IAbstractFactory {
        public Car getCar() {
            return new Bus();
        }
        public Human getHuman() {
            return new Man();
        }
    }
    class WomanFactory implements IAbstractFactory {
        public Car getCar() {
            return new Train();
        }
        public Human getHuman() {
            return new Woman();
        }
    }

    class AbstractFactoryTest{
      public static void main(String[] args) {    
        IAbstractFactory factory = new ManFactory(); 
        Car car = factory.getCar();
        
    Human human = factory.getHuman();
        
    human.say();
        car.gotowork();
        IAbstractFactory factory2 = new ManFactory(); 
        car = factory2.getCar();
        
    human = factory.getHuman();
        
    human.say();
        car.gotowork();
        }
    }

     2 、策略模式: 上面 1所述的模式我们发现 工厂中主要嵌套方法,如果嵌套的是属性,就是策略模式。

    class Strategy{
      Human human;
      Strategy(Human human){
        this.human = human;
      }
      public void doExec1(){
      human.say();

      /*一般这里都是和工厂模式一样添加逻辑判断*/
      }
    }

    3、代理模式 :我的理解代理就是对象的引用访问控制;相当于是策略模式的延伸。通过Proxy访问对象

    class Proxy{
      Woman woman;
      Proxy(Woman woman){
        this.woman = woman;
      }
      public void doExec1(){
        woman.say();
      }
    }

    4、原型模式 :这里涉及一个深拷贝和浅拷贝的概念。

      ⑴浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。 Object类提供的方法clone只是拷贝本对象,其对象内部的数组、引用对象等都不拷贝,还是指向原生对象的内部元素地址

      ⑵深复制把要复制的对象所引用的对象都复制了一遍,在jvm中新建个对象。

      浅克隆,部分对象未实现Cloneable接口
      深克隆,所有对象都实现Cloneable接口重写clone()或者通过流序列化如下所示

      在所有涉及的类都实现Serializable接口
        Object deepClone() throws IOException, ClassNotFoundException{
                //将对象写到流里
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(this);
                //从流里读回来
                ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bis);
                return ois.readObject();
        }

      简单来说,Man man =new Man()  就是创建一个原型, Man man1 = man.clone();这里就是浅客隆

     

    5、模版模式 定义一套模版,子类在模版的结构中添加不同的逻辑。 我们对工厂模式的案例重写下

    abstract class Human{
           public void say();
           public void action();
           template(){
            this.say();
            this.action();
           }
      }
    
      class Man implements Human {
        public void say() {
            System.out.println("男人");
        }
        public void action(){
            System.out.println("玩游戏");
        }
    }
    class Woman implements Human {
        public void say() {
            System.out.println("女人");
        }
        public void action(){
            System.out.println("贴面膜");
        }
    }
    

     客户端

     public static void main(String[] args) {
          Human human;
          
          human = new Man();
          human.template();
    
          human = new Woman();
          human.template();
      }

    6、外观模式 我的理解就是对 类方法的不同组合

    class Facade{
         Man man;
         Woman woman;
    // A组合
    public methodA(){ man.say(); woman.action(); } //B组合 public methodB(){ man.say(); woman.say(); } }

    7、建造者模式  这个模式 与外观模式好像,这不过外观模式 多用于不同对象的方法组合。建造者模式是一种 类由多个方法组合

    class Director{
         //这个方法内部是固定的,就像做好菜的秘方一样
         public void group(Human human){
             human.say();
             huamn.action();
             ...//say方法建造过程中可以放前,也可以放action后。
         }
     }
    

      

    时间有限,篇幅有点长了,下篇再写。以上只是一个识别设计模式的简单案例,实际代码中可能会有三四种模式的复合或者更多复合。 可能因为个人的理解和角度不同,如果觉得有问题,请留言。    

  • 相关阅读:
    用mobiscroll.js如何简单使用日期控件
    Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)
    运行网站项目时,有时出现Bad Request,该怎么解决?
    Easyui的datagrid的editor(行编辑器)如何扩展datetimebox类型
    JQuery对象和DOM对象的区别与转换
    【转发】Cookie存储的值大小限制和个数问题
    【转发】centos 7开启FTP以及添加用户配置权限,只允许访问自身目录,不能跳转根目录
    Vim 保存和退出命令
    CentOS下防御或减轻DDoS攻击方法(转)
    CentOS 7 firewalld vsftpd开放端口
  • 原文地址:https://www.cnblogs.com/nicknailo/p/9816754.html
Copyright © 2011-2022 走看看