好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java设计模式:原始模型模式

什么是原始模型模式

通过给出一个原型对象指明所要创建的对象的类型,然后通过复制这个原型对象来获取的更多的同类型的对象。

这让我不由自主的想起克隆技术,还记得克隆羊吗?我们接下来讲的内容和克隆羊不能说关系密切,只能说毫无关系。

设计模式和编程语言无关,但是二当家的依然用Java语言去实战举例。而且Java有标准的实现原始模型模式的方法。

原始模型模式中的角色

Prototype:抽象类或者一个接口,给出具体模型需要的接口。ConcretePrototype:继承抽象原型模型角色,被复制的对象。Client:提出复制请求。

抽象原型角色(Prototype)

我们用家庭作业为抽象原型角色(Prototype)。我们这里的作业是可以抄的。大家不要学哈。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

package com.secondgod.prototype;

/**

  * 作业

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public interface IHomework {

     /**

      * 抄一份

      * @return

      */

     IHomework copy();

     /**

      * 修改所有者

      * @param owner

      */

     void setOwner(String owner);

}

具体原型角色(ConcretePrototype)  

我们用语文作业作为具体原型角色(ConcretePrototype)。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

package com.secondgod.prototype;

import java.text.MessageFormat;

/**

  * 语文作业

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class ChineseHomework implements IHomework {

     /**

      * 作业的所有者

      */

     private String owner;

     /**

      * 作业标题/作业要求

      */

     private String title;

     /**

      * 作业内容

      */

     private String content;

     public ChineseHomework(String owner, String title, String content) {

         this .owner = owner;

         this .title = title;

         this .content = content;

     }

     public String getOwner() {

         return owner;

     }

     public void setOwner(String owner) {

         this .owner = owner;

     }

     public String getTitle() {

         return title;

     }

     public void setTitle(String title) {

         this .title = title;

     }

     public String getContent() {

         return content;

     }

     public void setContent(String content) {

         this .content = content;

     }

     public String toString() {

         return MessageFormat.format( "owner:{0},title:{1},content:{2}" , owner, title, content);

     }

     @Override

     public IHomework copy() {

         ChineseHomework homework = new ChineseHomework( this .getOwner(), this .getTitle(), this .getContent());

         return homework;

     }

}

客户端角色(Client)

我们测试一下。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.secondgod.prototype;

/**

  * 测试原始模型

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class Test {

     public static void main(String[] args) {

         // 老师让写作业,大当家按时完成

         IHomework homework = new ChineseHomework( "大当家的" , "作文-最崇拜的人" , "不瞒你们说,我最崇拜的是二当家的" );

         // 二当家的没按时完成,决定去抄大当家的作业~

         IHomework newHomework = homework.copy();

         newHomework.setOwner( "二当家的" );

         System.out.println(homework);

         System.out.println(newHomework);

     }

}

和我们的预期一致

使用Java内置机制实现原始模型模式

在Object类中有这样一个方法,Java中所有的类都继承自Object类,也就是说所有的类内部都可以复制自己。但是却不能直接调用别的类的克隆方法。也就是说有统一的方式,但是默认不可用。

我们改一下语文作业类,使用clone方法去尝试,克隆自己。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

package com.secondgod.prototype;

import java.text.MessageFormat;

/**

  * 语文作业

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class ChineseHomework implements IHomework {

     /**

      * 作业的所有者

      */

     private String owner;

     /**

      * 作业标题/作业要求

      */

     private String title;

     /**

      * 作业内容

      */

     private String content;

     public ChineseHomework(String owner, String title, String content) {

         this .owner = owner;

         this .title = title;

         this .content = content;

     }

     public String getOwner() {

         return owner;

     }

     public void setOwner(String owner) {

         this .owner = owner;

     }

     public String getTitle() {

         return title;

     }

     public void setTitle(String title) {

         this .title = title;

     }

     public String getContent() {

         return content;

     }

     public void setContent(String content) {

         this .content = content;

     }

     public String toString() {

         return MessageFormat.format( "owner:{0},title:{1},content:{2}" , owner, title, content);

     }

     @Override

     public IHomework copy() {

         try {

             return (ChineseHomework) super .clone();

         } catch (CloneNotSupportedException e) {

             throw new RuntimeException(e);

         }

     }

}

这时候会报错,因为我们还少做一件事。我们需要把语文作业类实现Cloneable接口,然而这个接口里没有任何抽象方法,仅仅是一个标记接口。这就像注解一样,就是为了明确声明可以克隆。

实现接口后,则可以正确运行。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

package com.secondgod.prototype;

import java.text.MessageFormat;

/**

  * 语文作业

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class ChineseHomework implements IHomework, Cloneable {

     /**

      * 作业的所有者

      */

     private String owner;

     /**

      * 作业标题/作业要求

      */

     private String title;

     /**

      * 作业内容

      */

     private String content;

     public ChineseHomework(String owner, String title, String content) {

         this .owner = owner;

         this .title = title;

         this .content = content;

     }

     public String getOwner() {

         return owner;

     }

     public void setOwner(String owner) {

         this .owner = owner;

     }

     public String getTitle() {

         return title;

     }

     public void setTitle(String title) {

         this .title = title;

     }

     public String getContent() {

         return content;

     }

     public void setContent(String content) {

         this .content = content;

     }

     public String toString() {

         return MessageFormat.format( "owner:{0},title:{1},content:{2}" , owner, title, content);

     }

     @Override

     public IHomework copy() {

         try {

             return (ChineseHomework) super .clone();

         } catch (CloneNotSupportedException e) {

             throw new RuntimeException(e);

         }

     }

}

和我们自己实现copy效果一样。clone是一个native方法,是交给本地实现的,通常是直接内存拷贝。

浅拷贝和深拷贝

浅拷贝:创建一个新对象,然后将当前对象的非静态字段复制到该新对象,如果字段是值类型的,那么对该字段执行复制;如果该字段是引用类型的话,则复制引用但不复制引用的对象。因此,原始对象及其副本引用同一个对象。

深拷贝:创建一个新对象,然后将当前对象的非静态字段复制到该新对象,无论该字段是值类型的还是引用类型,都复制独立的一份。当你修改其中一个对象的任何内容时,都不会影响另一个对象的内容。

二当家的理解方式是,浅拷贝就是仅拷贝当前对象的内容,深拷贝就是递归拷贝当前对象和当前对象的引用类型属性的内容,直到全部都是基本数据类型的属性为止。另外,仅仅使用clone方法是浅拷贝。

package com.secondgod.prototype;/** * 测试原始模型 * * @author 二当家的白帽子 https://le-yi.blog.csdn.net/ */public class Test implements Cloneable {    private Field field;    public Field getField() {        return field;    }    public void setField(Field field) {        this.field = field;    }    public Test clone() throws CloneNotSupportedException {        return (Test) super.clone();    }    public static void main(String[] args) throws CloneNotSupportedException {        Test t = new Test();        t.setField(new Field());        Test cloneT = t.clone();        System.out.println(t == cloneT);        System.out.println(t.getField() == cloneT.getField());    }}class Field {}

源对象和克隆出的新对象field属性值是同一个对象。所以是浅拷贝。

怎么实现深拷贝

让每个引用类型属性内部都重写clone() 方法,然后需要对所有引用类型属性都调用clone方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

package com.secondgod.prototype;

/**

  * 测试原始模型

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class Test implements Cloneable {

     private Field field;

     public Field getField() {

         return field;

     }

     public void setField(Field field) {

         this .field = field;

     }

     public Test clone() throws CloneNotSupportedException {

         return (Test) super .clone();

     }

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

         Test t = new Test();

         t.setField( new Field());

         Test cloneT = t.clone();

         System.out.println(t == cloneT);

         System.out.println(t.getField() == cloneT.getField());

     }

}

class Field {

}

这种方式需要递归每个引用类型的属性,要把他们的类都实现深拷贝,只到仅有基本数据类型为止。

利用序列化,这是个偷懒的办法。但是二当家的觉得更安全,如果你确定是要深拷贝,使用该方式可以防止某处漏实现clone方法,而变成不完全的深拷贝。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

package com.secondgod.prototype;

/**

  * 测试原始模型

  *

  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/

  */

public class Test implements Cloneable {

     private Field field;

     public Field getField() {

         return field;

     }

     public void setField(Field field) {

         this .field = field;

     }

     public Test clone() throws CloneNotSupportedException {

         return (Test) super .clone();

     }

     public Test deepClone() throws CloneNotSupportedException {

         Test t = new Test();

         t.setField( this .getField().deepClone());

         return t;

     }

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

         Test t = new Test();

         t.setField( new Field());

         Test cloneT = t.clone();

         System.out.println(t == cloneT);

         System.out.println(t.getField() == cloneT.getField());

         Test deepCloneT = t.deepClone();

         System.out.println(t == deepCloneT);

         System.out.println(t.getField() == deepCloneT.getField());

     }

}

class Field implements Cloneable {

     public Field clone() throws CloneNotSupportedException {

         return (Field) super .clone();

     }

     public Field deepClone() throws CloneNotSupportedException {

         // 没有引用类型属性

         return this .clone();

     }

}

到底使用浅拷贝还是深拷贝,要根据实际情况。但是有一点是确定的,深拷贝需要创建更多对象,占用更多内存。

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/leyi520/article/details/119536473

查看更多关于java设计模式:原始模型模式的详细内容...

  阅读:14次