java设计模式---原型模式

浅拷贝

[root@hadoop-01 prototype]# cat src/Book.java
/*************************************************************************
        > File Name: Book.java
        > Author:
        > Mail:
        > Created Time: Fri 01 Jun 2018 02:01:04 AM PDT
 ************************************************************************/
package indi.tlq.prototype;
import java.util.ArrayList;
import java.io.*;

public class Book implements Cloneable ,  Serializable{

    private String title;
    private ArrayList<String> image = new ArrayList<String>();



    public Book clone(){
        try{
            Book book = (Book) super.clone();
            return book;
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return  null;
    }

    public Book deepClone(){
        try{
            Book book = (Book) super.clone();
            book.image =   (ArrayList<String>)image.clone();
            return book;
        }catch (CloneNotSupportedException e ) {
            e.printStackTrace();
        }
        return null;
    }

    public Book deepClone2() {
        try
        {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);

            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream  oi = new ObjectInputStream(bi);
            return((Book)oi.readObject());

        }catch(Exception  e){
            e.printStackTrace();
        }
        return null;
    }

    public String getTitle(){
        return  title;
    }
    public void setTitle(String title){
        this.title = title;
    }

    public ArrayList<String> getImage(){
        return image;
    }

    public void addImage(String img){
        image.add(img);
    }

    public void show(){
        System.out.println("-----------------start print------------");
        System.out.println("title: "+ title);
        for(String s:image){
            System.out.println("Image name:"+s);
        }

        System.out.println("------------------end print -------------");
    }
}

深拷贝1—clone

public Book deepClone(){
        try{
            Book book = (Book) super.clone();
            book.image =   (ArrayList<String>)image.clone();
            return book;
        }catch (CloneNotSupportedException e ) {
            e.printStackTrace();
        }
        return null;
    }

深拷贝2—序列化

public Book deepClone2() {
        try
        {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);

            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream  oi = new ObjectInputStream(bi);
            return((Book)oi.readObject());

        }catch(Exception  e){
            e.printStackTrace();
        }
        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值