1. 表示层或者用户界面层(ui层)
2. 应用层
3. 领域层
4. 基础设施层一般只有领域层负责领域模型,其他层为其服务,这设计的好处是将领域相关的代码分离开来 具体每层的含义不再赘述。
public abstract class Entity<T> {
    T id;
    String name;
}基于此抽象可以实现一个抽象的基础实体类
public abstract  class BaseEntity<T> extends  Entity<T>{
    private T id;
    public BaseEntity(T id, String name){
        super.id = id;
        super.name = name;
    }
    //.... get set 方法 以及其他相关代码
}基于前面的抽象类创建restaurant实体,我们开发的是餐馆订座系统,所以Table是领域模型中 另一个重要的实体,这里由于业务,我们可以采用聚合模式,restaurant会作为一个根来工作, 而Table将是restaurant实体中的内部实体。因此Table实体始终都可以使用restaurant来访问。 下面作为示范,添加了基本属性
/**
 * @author xzg
 */
public class Table  extends BaseEntity<BigInteger> {
private  int capacity;
    public Table(BigInteger id, String name, int capacity) {
        super(id, name);
        this.capacity = capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public int getCapacity() {
        return capacity;
    }
}现在我们实现restaurant聚合实体
**
 * @author xzg
 */
public class Restaurant extends  BaseEntity<String> {
    private List<Table> tables = new ArrayList<>();
    public Restaurant(String id, String name,List<Table> tables) {
        super(id, name);
        this.tables = tables;
    }
    public List<Table> getTables() {
        return tables;
    }
    public void setTables(List<Table> tables) {
        this.tables = tables;
    }
}/**
 * @author xzg
 */
public interface ReadOnlyRepository<TE,T> {
    boolean contain(T id);
    Entity  get(T id);
    Collection<TE> getAll();
}基于只读接口我再添加额外的操作
/**
 * @author xzg
 */
public interface Repository<TE,T> extends  ReadOnlyRepository {
    void add(TE entity);
    void  remove(T id);
    void  update(TE entity);
}前面定义的抽象层可以使用合适的方式使用,上面属于存储服务的公共抽象层,具体不同存储业务,还需要我们根据具体去添加定义 下面提供餐馆服务的存储服务接口以及相应的实现类
/**
 * @author xzg
 */
public interface RestaurantRepository<Restaurant,String> extends Repository<Restaurant,String> {
    boolean cotainsName(String name);
}下面实现:
**
 * @author xzg
 * @这里为方便
 * 使用内存作为存储,实际生产请使用数据库库或者缓存
 */
public class InMemRestaurantRepository implements  RestaurantRepository<Restaurant,String> {
    private Map<String,Restaurant> entities;
    public InMemRestaurantRepository(){
        entities = new HashMap<>();
    }
    @Override
    public boolean cotainsName(String name) {
        return entities.containsKey(name);
    }
    @Override
    public void add(Restaurant entity) {
        entities.put(entity.getName(),entity);
    }
    @Override
    public void remove(String id) {
        if(entities.containsKey(id))
            entities.remove(id);
    }
    @Override
    public void update(Restaurant entity) {
            if(entities.containsKey(entity.getName()))
                entities.put(entity.getName(),entity);
    }
    @Override
    public boolean contains(Object id) {
        throw  new UnsupportedOperationException("not support yet");
    }
    @Override
    public Entity get(Object id) {
        throw  new UnsupportedOperationException("not support yet");
    }
    @Override
    public Collection getAll() {
        return entities.values();
    }
}和前面的存储库服务一样我们把领域服务也抽象出,主抽象服务和只读抽象服务
/**
 * @author xzg
 */
public abstract class ReadOnlyBaseService<TE,T> {
    private  Repository<TE,T> repository;
    ReadOnlyBaseService(Repository<TE,T> repository){
        this.repository = repository;
    }
    //....
}同理我们使用一个base服务去实现这个只读的服务,这里使用依赖注入的方式将存储服务注入到 领域服务中去。
   /**
 * @author xzg
 */
public  abstract  class BaseService<TE,T> extends  ReadOnlyBaseService<TE,T> {
    private Repository<TE,T> _repository;
    BaseService(Repository<TE,T> repository){
        super(repository);
        _repository = repository;
    }
    public  void add(TE entity) throws  Exception{
        _repository.add(entity);
    }
    public Collection<TE> getAll(){
        return _repository.getAll();
    }
}有了基础的服务层,我们的具体领域服务也就可以根据自身业务需求设计
/**
 * @author xzg
 */
public class RestaurantService extends  BaseService<Restaurant, BigInteger> {
    private RestaurantRepository<Restaurant,String> restaurantRepository;
    public RestaurantService(RestaurantRepository restaurantRepository){
        super(restaurantRepository);
        this.restaurantRepository = restaurantRepository;
    }
    @Override
    public  void add(Restaurant restaurant) throws Exception{
        if(restaurantRepository.cotainsName(restaurant.getName()))
            throw  new Exception("name is repeat");
        if(StringUtils.isEmpty(restaurant.getName()))
            throw  new NullPointerException("name is null");
        super.add(restaurant);
    }
//...others
}