|  | 
|  | 1 | +package com.company.prototype; | 
|  | 2 | + | 
|  | 3 | +import java.util.ArrayList; | 
|  | 4 | + | 
|  | 5 | +/** | 
|  | 6 | + * 文档类型,代表ConcretePrototype   Cloneable代表prototype角色,生命具备clone的能力 | 
|  | 7 | + */ | 
|  | 8 | +public class WordDocument2 implements Cloneable { | 
|  | 9 | +    private String text;//文本 | 
|  | 10 | +    private ArrayList<String> images = new ArrayList<>();//图片 | 
|  | 11 | + | 
|  | 12 | +    public WordDocument2() { | 
|  | 13 | +        System.out.println("--------WordDocument的构造函数--------"); | 
|  | 14 | +    } | 
|  | 15 | + | 
|  | 16 | +    public String getText() { | 
|  | 17 | +        return text; | 
|  | 18 | +    } | 
|  | 19 | + | 
|  | 20 | +    public void setText(String text) { | 
|  | 21 | +        this.text = text; | 
|  | 22 | +    } | 
|  | 23 | + | 
|  | 24 | +    public ArrayList<String> getImages() { | 
|  | 25 | +        return images; | 
|  | 26 | +    } | 
|  | 27 | + | 
|  | 28 | +    public void addImage(String image) { | 
|  | 29 | +        this.images.add(image); | 
|  | 30 | +    } | 
|  | 31 | + | 
|  | 32 | +    @Override | 
|  | 33 | +    protected WordDocument2 clone() { | 
|  | 34 | +        try { | 
|  | 35 | +            WordDocument2 wordDocument = (WordDocument2) super.clone();//返回新的对象 | 
|  | 36 | +            wordDocument.text = this.text; | 
|  | 37 | +//            对对象的引用数据类型,采用clone(),进行深拷贝 | 
|  | 38 | +            wordDocument.images = (ArrayList<String>) this.images.clone(); | 
|  | 39 | +            return wordDocument; | 
|  | 40 | +        } catch (Exception e) { | 
|  | 41 | +            e.printStackTrace(); | 
|  | 42 | +        } | 
|  | 43 | +        return null; | 
|  | 44 | +    } | 
|  | 45 | + | 
|  | 46 | +    public void showDocument() { | 
|  | 47 | +        System.out.println("--------Word Content Start--------"); | 
|  | 48 | +        System.out.println("Text: " + text); | 
|  | 49 | +        System.out.println("Image lists"); | 
|  | 50 | +        for (String imageName : images) { | 
|  | 51 | +            System.out.println("Image name: " + imageName); | 
|  | 52 | +        } | 
|  | 53 | +        System.out.println("--------Word Content End--------"); | 
|  | 54 | +    } | 
|  | 55 | +} | 
0 commit comments