本文根据尚硅谷韩顺平老师的设计模式课程编写设计模式
Sheep.java
public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public Sheep() {
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Client.java
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom", 1, "白色");
Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
System.out.println(sheep.toString());
System.out.println(sheep1.toString());
System.out.println(sheep2.toString());
System.out.println(sheep3.toString());
System.out.println(sheep4.toString());
}
}
结果:
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
全都一模一样
优缺点:
新的思路
java中Object类是所有类的根类,Object类提供了一个clone()方法,该方法可以将一个Java对象复制一份,但是需要实现clone的java类必须实现一个接口Cloneable,该接口表示该类能够复制且具有复制的能力=>原型模式。
基本介绍
Sheep.java
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public Sheep() {
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone(){
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
}catch (CloneNotSupportedException ex){
System.out.println(ex.getMessage());
}
return sheep;
}
}
Client.java
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom", 1, "白色");
Sheep sheep1 = (Sheep) sheep.clone();
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
Sheep sheep5 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep1);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
}
}
结果:
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
Sheep{
name='tom', age=1, color='白色'}
beans.xml
<bean id="id01" class="com.liu.bean.Monster" scope="protptype"/>
这里是使用原型模式来创建的对象
doGetBean();
DeepCloneableTarget.java
public class DeepCloneableTarget implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
//构造器
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
//因为该类属性都是String,因此我们这里使用默认的clone完成即可
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
DeepProtoType.java
public class DeepProtoType implements Serializable, Cloneable {
//String类型
public String name;
//引用类型
public DeepCloneableTarget deepCloneableTarget;
//深拷贝 - 方式一 clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
//这里是对基本数据类型和字符串的克隆
deep = super.clone();
//引用类型变多后会很麻烦
//需要对引用类型的属性进行单独处理
DeepProtoType deepProtoType = (DeepProtoType) deep;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
return deepProtoType;
}
//深拷贝 - 方式2 通过对象的序列化实现 (推荐)
public Object deepClone(){
//创建流对象
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
//序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
//当前这个流对象以对象流的方式输出
oos.writeObject(this);
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
return (DeepProtoType) ois.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
//关闭流
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}