设计模式
作者:互联网
新设计模式(4个)
1.对象池模式(资源频繁分配、释放所造成成本高昂的对象)
2.规约模式 父类依赖子类、实现了对象筛选性能
//使用 ISpecification spec1 = new BizSpecification("a"); ISpecification spec2 = new BizSpecification("b"); //规格调用 for (Object o : list) { if(spec1.and(spec2).isSatisfiedBy(o)){ //如果o满足spec1 && spec2 System.out.println(o); } } //定义规格书 interface ISpecification { //候选者是否满足条件 boolean isSatisfiedBy (Object candidate) ; //and操作 ISpecification and (ISpecification spec); //or操作 ISpecification or (ISpecification spec); //not操作 ISpecification not (); } //组合规格书 static abstract class CompositeSpecification implements ISpecification { //是否满足条件由子类实现 public abstract boolean isSatisfiedBy (Object candidate) ; //and操作 public ISpecification and (ISpecification spec) { return new AndSpecification(this, spec); } //or操作 public ISpecification or(ISpecification spec) { return new OrSpecification(this, spec); } //not操作 public ISpecification not() { return new NotSpecification(this); } } //与规格书 static class AndSpecification extends CompositeSpecification { //传递两个规格书进行and操作 private ISpecification left; private ISpecification right; public AndSpecification(ISpecification left, ISpecification right) { this.left = left; this.right = right; } //进行and运算 public boolean isSatisfiedBy(Object candidate) { return left.isSatisfiedBy(candidate) && right.isSatisfiedBy(candidate); } } static class OrSpecification extends CompositeSpecification { //传递两个规格书进行or操作 private ISpecification left; private ISpecification right; public OrSpecification(ISpecification left, ISpecification right) { this.left= left; this.right = right; } //进行or运算 public boolean isSatisfiedBy(Object candidate) { return left.isSatisfiedBy(candidate) || right.isSatisfiedBy(candidate); } } static class NotSpecification extends CompositeSpecification { //传递两个规格书进行非操作 private ISpecification spec; public NotSpecification(ISpecification spec) { this.spec = spec; } //进行not运算 public boolean isSatisfiedBy(Object candidate) { return !spec.isSatisfiedBy(candidate); } } //业务规格书 static class BizSpecification extends CompositeSpecification { //基准对象,如姓名等,也能够是int等类型 private String obj; public BizSpecification(String obj) { this.obj = obj; } //判断是否满足要求 public boolean isSatisfiedBy(Object candidate){ //依据基准对象判断是否合乎 return true; } }
3.空对象模式 定义一个空对象,当存在就返回真实对象,不存在就返回空对象
4.雇工模式 形象服务提供者(接口)、实际服务提供者(实现类)、雇工(调用接口服务)
标签:ISpecification,right,candidate,spec,isSatisfiedBy,设计模式,public 来源: https://www.cnblogs.com/sxsheng/p/16479125.html