栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Design Pattern

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Design Pattern

序言

适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 

适配器模式很好理解,实现起来也很简单.主要是我们要怎么实现,实现方式的不同,决定了该模式的运用的好坏cuiyaonan2000@163.com

场景1

需要调用A类的方法,但是a方法的入参是字符串,不能直接调用.所以使用适配器,做个简单的处理,来替代A

package nan.yao.cui.test;



public class Test01 {

    public class A{
        public void doA(String temp){
            System.out.println("the temp is  :" + temp);
        }
    }
    public interface Target {
        public void doTarget(Integer i);
    }

    public class Adapter implements Target{

        private A a;

        public Adapter(A a){
            this.a = a;
        }
        @Override
        public void doTarget(Integer i) {
            a.doA(i.toString());
        }
    }

}
场景2

官方的例子看着代码多,稍微绕一点.但是其实是适配器模式和工厂模式的整合.根据参数的来选择实例化哪个一个实现对象,来做操作cuiyaonan2000@163.com

https://www.runoob.com/design-pattern/adapter-pattern.html

原型模式

原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能.

Object默认提供了clone()方法只能实现浅克隆,即只能将基本类型的值进行复制和引用类型的地址进行复制,没办法将引用类型指向的真正对象进行复制

浅克隆

浅克隆很简单.只需要做如下两步就可以了

  1. 需要被克隆的类实现接口Cloneable
  2. 重写clone方法 
  3. 在重写clone方法中调用super.clone()  --------既然Object已经提供了clone方法,为什么我们不能直自己写个方法调用super.clone()呢cuiyaonan2000@163.com

查看Cloneable可以看到该接口没有方法(就是一个标记),

查看Object中的clone方法声明

综上所述了解到.Object的clone()方法 首先被protected修饰表明该方法只能在子类方法中调用,不能直接通过对象直接调用.

其次被native修饰表明该方法的实现不是使用的Java库,而是通过JNI的方式实现的.即不是java库实现的clone.

所以Cloneable接口只是一个标记,告诉JVM在实例化该对象的时候提供对应的JNI的实现.cuiyaonan2000@163.com

深克隆

深克隆主要有三种方式

  1. 通过new的方式来解决
  2. 通过签到clone的方式来解决,---此方法就是说如果要被克隆的对象层级深和复杂的情况下代码比较多cuiyaonan2000@163.com
  3. 通过序列化和反序列化的方式来解决

这里主要对比下他们之间的效率问题

package nan.yao.cui.test;


import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class Test01{



    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {


        series();

        newTest();

        doclone();

    }

    public static void series() throws IOException, ClassNotFoundException {

        long time = System.currentTimeMillis();

        List studentList = new ArrayList<>(100000);

        Student student_1 = new Student("小明",12, (byte) 1,new Teacher("小明老师",22));

        for(int i =1 ; i <= 100000 ; i++){

            //序列化
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(student_1);
            //反序列化
            ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            Student roomClone = (Student)objectInputStream.readObject();
            studentList.add(roomClone);
        }

        System.out.print(studentList.size());

        System.out.println("序列化 方法耗时"+(System.currentTimeMillis() - time));
    }


    public static void newTest(){
        long time = System.currentTimeMillis();

        List studentList = new ArrayList<>(100000);

        for(int i =1 ; i <= 100000 ; i++){
            Student student_1 = new Student("小明",12, (byte) 1,new Teacher("小明老师",22));
            studentList.add(student_1);
        }

        System.out.print(studentList.size());

        System.out.println("new 方法耗时"+(System.currentTimeMillis() - time));
    }

    public static void doclone() throws CloneNotSupportedException {
        long time = System.currentTimeMillis();

        List studentList = new ArrayList<>(100000);
        Student student_1 = new Student("小明",12, (byte) 1,new Teacher("小明老师",22));


        for(int i =1 ; i <= 100000 ; i++){
            Student studentClone = student_1.clone();
            Teacher teacherClone = student_1.getTeacher().clone();
            studentClone.setTeacher(teacherClone);
            studentList.add(studentClone);
        }

        System.out.print(studentList.size());

        System.out.println("clone方法耗时"+(System.currentTimeMillis() - time));
    }
}

结果显示如下所示:

综上所述,克隆模式是为了解决重复对象创建效率的问题,所以可以根据你的对象选择满足自己要求的方式cuiyaonan2000@163.com

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号