本篇的议题如下:架构模式设计模式设计原则

{
bool IsSatisfiedBy(T entity);
}
public class HasReachedMaxSpecification : ISpecification<Customer>
{
public bool IsSatisfiedBy(Customer entity)
{
return entity.TotalRentNumber > 5;
}
}

{
public override bool IsSatisfiedBy(CustomerAccount candidate)
{
return candidate.NumberOfRentalsThisMonth >= 5;
}
}

1. Component是一个抽象类,这个类提供了一个Add方法,这个Add可以加入其他的Component.大家想想,这样是否就可以很容易的实现链式的效果。


{
public override bool IsSatisfiedBy(CustomerAccount candidate)
{
return candidate.AccountActive;
}
}

{
public override bool IsSatisfiedBy(CustomerAccount candidate)
{
return candidate.LateFees > 0;
}
}

{
public abstract bool IsSatisfiedBy(T candidate);
public ISpecification<T> And(ISpecification<T> other)
{
return new AndSpecification<T>(this, other);
}
public ISpecification<T> Not()
{
return new NotSpecification<T>(this);
}
}

{
private ISpecification<T> _leftSpecification;
private ISpecification<T> _rightSpecification;
public AndSpecification(ISpecification<T> leftSpecification, ISpecification<T> rightSpecification)
{
_leftSpecification = leftSpecification;
_rightSpecification = rightSpecification;
}
public override bool IsSatisfiedBy(T candidate)
{
return _leftSpecification.IsSatisfiedBy(candidate) && _rightSpecification.IsSatisfiedBy(candidate);
}
}

{
private ISpecification<T> _innerSpecification;
public NotSpecification(ISpecification<T> innerSpecification)
{
_innerSpecification = innerSpecification;
}
public override bool IsSatisfiedBy(T candidate)
{
return !_innerSpecification.IsSatisfiedBy(candidate);
}
}

{
…
}
public class CustomerAccountStillActiveSpecification :CompositeSpecification<CustomerAccount>
{
}
public class CustomerAccountHasLateFeesSpecification :CompositeSpecification<CustomerAccount>
{
…
}
{
private ISpecification<Customer> hasReachedRentalThreshold;
private ISpecification<Customer> customerIsActive;
private ISpecification<Customer> customerHasLateFees;
public Customer()
{
hasReachedRentalThreshold = new HasReachedMaxSpecification();
customerIsActive = new CustomerAvtiveSpecification();
customerHasLateFees = new CustomerHasLateFeesSpecification();
}
public decimal TotalRentNumber { get; set; }
public bool IsActive { get; set; }
public decimal LateFees { get; set; }
public bool CanRent()
{
ISpecification<Customer>canRent =customerIsActive.And(hasReachedRentalThreshold.Not()).And(customerHasLateFees.Not());
return canRent.IsSatisfiedBy(this);
}
}
{
return new AndSpecification<T>(this, other);
}

And(_customerAccountHasLateFees.Not());


-
《微服务架构设计模式》第五章 微服务架构中的业务逻辑设计
在领域驱动设计的上下文中,领域事件是聚合发生的事情。它由领域模型中的一个类表示。事件通常代表状态的变化。
架构 微服务 java 业务逻辑 领域模型
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M