一、UML图
二、实例
1、Prototype
package com.designPattern.prototype;
/**
* Created by ZhangJintao on 2020/3/10.
*/
public abstract class HealthStatus implements Cloneable {
// 体温是否大于37.3度
private boolean bodyTemperatureBigThanNormal;
// 咳嗽
private boolean cough;
// 体感虚弱
private boolean weak;
public boolean isBodyTemperatureBigThanNormal() {
return bodyTemperatureBigThanNormal;
}
public void setBodyTemperatureBigThanNormal(boolean bodyTemperatureBigThanNormal) {
this.bodyTemperatureBigThanNormal = bodyTemperatureBigThanNormal;
}
public boolean isCough() {
return cough;
}
public void setCough(boolean cough) {
this.cough = cough;
}
public boolean isWeak() {
return weak;
}
public void setWeak(boolean weak) {
this.weak = weak;
}
@Override
protected HealthStatus clone() throws CloneNotSupportedException {
return (HealthStatus) super.clone();
}
protected void showHealth(){
System.out.println("bodyTemperatureBigThanNormal=" + bodyTemperatureBigThanNormal + " cough=" + cough + " weak=" + weak);
}
}
2、ConcretePrototype
package com.designPattern.prototype;
/**
* Created by ZhangJintao on 2020/3/10.
*/
public class HealthStatusOk extends HealthStatus{
public HealthStatusOk() {
this.setBodyTemperatureBigThanNormal(false);
this.setCough(false);
this.setWeak(false);
}
}
3、main(Client)
package com.designPattern.prototype;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ZhangJintao on 2020/3/10.
*/
public class main {
public static void main(String[] args) {
List<HealthStatus> healthStatusList = new ArrayList<HealthStatus>();
try {
HealthStatusOk healthStatusOk = new HealthStatusOk();
for (int i = 0 ; i < 10 ; i++){
HealthStatus healthStatus = healthStatusOk.clone();
if (i % 3 == 0){
healthStatus.setCough(true);
}
healthStatusList.add(healthStatus);
}
}catch (CloneNotSupportedException exception) {
exception.printStackTrace();
}
for (HealthStatus healthStatus : healthStatusList ) {
healthStatus.showHealth();
}
}
}