领域驱动设计代码片段
1、在领域驱动设计中,这个Cell(电池单元)设计为一个包级别的类(没有在类上添加public),而且方法也是包级别的(没有在方法添加public,protected,private)
2、Cell类在领域设计中属于值对象,而且它只能被同一个包下的实体或者聚合使用,不是对外公开的
3、CellBlock(电池块)也是设计为包级别的类,方法也是包级别,和Cell类设计一致
4、CellBlock类在领域设计中属于值对象,而且它只能被同一个包下的实体或者聚合使用,不是对外公开的
5、CellCharger(充电器)也是设计为包级别的类,方法也是包级别,和Cell类设计一致
6、CellCharger类在领域设计中属于值对象,而且它只能被同一个包下的实体或者聚合使用,不是对外公开的
7、BatteryRootEntity有三部分组成,代表电池唯一标识的序列号,一个电池块和一个充电器。
8、BatteryRootEntity在领域设计中属于实体,它有两个关键的方法,充电和停止充电,同时提供电池和电池状态信息
9、EVBatteryInformation(EV电池信息类)这个类存储的是电池的序列号信息和最大容量信息,因为这些信息是外部关心的,所以这个类被设计为public,属于值对象
10、EVBatteryStatus(EV电池状态信息类)这个类存储的是电池的状态信息(其实还包括充电器的状态),最大充电等级和当前电池等级,因为这些信息是外部关心的,所以这个类被设计为public,属于值对象
11、EVBatteryAggregate(EV电池聚合)这个类才是真正的供外部使用的类,它提供的功能和BatteryRootEntity类一样,可以认为是对BatteryRootEntity的包装,为什么要这么设计呢?
试想一些,如果在这个案例中还有其他的实体,而我们刚才特别指出,实体是包级别的,而外部需要访问实体只能通过聚合,因此需要添加实体的时候,只能把新的实体添加到这个
聚合,然后向外部提供开放的功能和信息。
12、如果大伙儿感兴趣,可以留个问题,这里的EVBatteryInformation和EVBatteryStatus为什么要设计成public方法,他们的构造函数能不能设计为包级别的设置private
package com.ddd.battery
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* 定义一个电池单元
*/
class Cell {
//电池单元的充电功率
private int chargeKW;
//是否正在充电
private boolean charging;
//电池单元最大容量
private int maxCapacity;
//电池单元当前容量
private int currentCapacity;
Cell(int capacity) {
this.maxCapacity = capacity;
this.currentCapacity = capacity;
this.charging = false;
this.chargeKW = 0;
}
// 充电
void charge(int kw) {
this.chargeKW = kw;
this.charging = true;
currentCapacity++;
}
// 返回充电功率
int getChargeKW() {
return chargeKW;
}
boolean isCharging() {
return charging;
}
int getMaxCapacity() {
return maxCapacity;
}
int getCurrentCapacity() {
return currentCapacity;
}
//停止充电
void stopCharge() {
chargeKW = 0;
this.charging = false;
}
}
/**
* 定义一个电池块,有若干个电池块组成
*/
class CellBlock {
private List<Cell> batteryCells;
/**
*
* @param amountOfCells 电池有多少个单元
* @param capacityForCell 每个电池单元的容量
* 电池块的组成逻辑实际上是电池单元的累加
*/
CellBlock(int amountOfCells, int capacityForCell) {
this.batteryCells = new ArrayList<Cell>();
for (int i = 0; i < amountOfCells; i++) {
this.batteryCells.add(new Cell(capacityForCell));
}
}
//计算电池的总容量
int getCapacity() {
int capacity = 0;
for(Cell cell : batteryCells) {
capacity += cell.getMaxCapacity();
}
return capacity;
}
//计算电池的等级,用当前电池的容量/总容量得到一个比例来反映电池的等级
double getChargeLevel() {
int overallChargeLevel = 0;
for(Cell c : batteryCells) {
overallChargeLevel += c.getCurrentCapacity();
}
return overallChargeLevel / batteryCells.size();
}
List<Cell> getBatteryCells() {
return batteryCells;
}
}
/**
* 定义一个电池充电器,用于给电池充电
*/
class CellCharger {
//充电的最大功率
int chargeLimit;
//重点状态
boolean charging;
CellCharger() {
this.charging = false;
this.chargeLimit = 100;
}
int getChargeLimit() {
return chargeLimit;
}
boolean isCharging() {
return charging;
}
//充电功能,实际上就是对每个电池单元充电
CellBlock charge(CellBlock cellBlock, int kw) {
this.charging = true;
for (Cell cell : cellBlock.getBatteryCells()) {
//do charging with some intelligent logic
cell.charge(kw);
}
return cellBlock;
}
//停止充电,实际上就是对每个电池单元停止充电
CellBlock stopCharge(CellBlock cellBlock) {
this.charging = false;
for (Cell cell : cellBlock.getBatteryCells()) {
cell.stopCharge();
}
return cellBlock;
}
}
/**
* 电池根实体
*/
class BatteryRootEntity {
/**
* 序列号
*/
private UUID serialNumber;
/**
* 电池块
*/
private CellBlock cellBlock;
/**
* 充电器
*/
private CellCharger cellCharger;
BatteryRootEntity(int amountOfCells, int capacityForCell) {
this.serialNumber = UUID.randomUUID();
this.cellBlock = new CellBlock(amountOfCells, capacityForCell);
this.cellCharger = new CellCharger();
}
void startCharging(int kw) {
this.cellBlock = cellCharger.charge(cellBlock, kw);
}
void stopCharging() {
this.cellBlock = cellCharger.stopCharge(cellBlock);
}
EVBatteryInformation getInformation() {
return new EVBatteryInformation(serialNumber, cellBlock.getCapacity());
}
EVBatteryStatus getCurrentBatteryStatus() {
return new EVBatteryStatus(cellCharger.isCharging(), cellCharger.getChargeLimit(), cellBlock.getChargeLevel());
}
}
public class EVBatteryInformation {
private UUID id;
private int capacity;
public EVBatteryInformation(UUID id, int capacity) {
this.id = id;
this.capacity = capacity;
}
public UUID getId() {
return id;
}
public int getCapacity() {
return capacity;
}
}
/**
* EV电池信息类
*/
public class EVBatteryStatus {
private boolean charging;
private int chargeLevelSet;
private double currentChargeLevel;
public EVBatteryStatus(boolean charging, int chargeLevelSet, double currentChargeLevel) {
this.charging = charging;
this.chargeLevelSet = chargeLevelSet;
this.currentChargeLevel = currentChargeLevel;
}
public boolean isCharging() {
return charging;
}
public int getChargeLevelSet() {
return chargeLevelSet;
}
public double getCurrentChargeLevel() {
return currentChargeLevel;
}
}
/**
* EV电池状态
*/
public class EVBatteryStatus {
private boolean charging;
private int chargeLevelSet;
private double currentChargeLevel;
public EVBatteryStatus(boolean charging, int chargeLevelSet, double currentChargeLevel) {
this.charging = charging;
this.chargeLevelSet = chargeLevelSet;
this.currentChargeLevel = currentChargeLevel;
}
public boolean isCharging() {
return charging;
}
public int getChargeLevelSet() {
return chargeLevelSet;
}
public double getCurrentChargeLevel() {
return currentChargeLevel;
}
}
/**
* 动力电池聚合根
*/
public class EVBatteryAggregate {
private BatteryRootEntity batteryRootEntity;
public EVBatteryAggregate() {
this.batteryRootEntity = new BatteryRootEntity(70, 1);
}
public void startCharging(int kw) {
this.batteryRootEntity.startCharging(kw);
}
public void stopCharging() {
this.batteryRootEntity.stopCharging();
}
public EVBatteryInformation getInformation() {
return this.batteryRootEntity.getInformation();
}
public EVBatteryStatus getCurrentBatteryStatus() {
return this.batteryRootEntity.getCurrentBatteryStatus();
}
}