论坛首页 Java企业应用论坛

规格模式(Specification)

浏览 2045 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-19   最后修改:2010-04-19

我们公司大量的设计都体现了规格模式,设计出来的只是规格,然后再去实例化。业务规则不适于放在任何已个实体或值对象中,而且规则的变化和组合会掩盖那些领域对象的基本含义。有时经常造成组合爆炸。规格是模型的一部分,将它们从实体或值对象中独立出来有助于使模型更加清晰,它表达的是业务的规则。规格是值对象,它用来判断对象 是否满足标准的谓词。

 

商品依赖关系就是一种规格,在它下面可以存在各种各样的关系,例如现在展现的主副机,例如基本包与增值等等。

/**
 * 商品的依赖关系
 * 例如基本收视副机依赖基本收视商品
 */
public abstract class ProductOfferingRelationship extends DomainObject{
	/**
	 * 例如:基本收视副机
	 */
	private ProductOffering productOffering;

	/**
	 * 例如:基本收视
	 */
	public ProductOffering relationshipProductOffering;
	
	protected ProductOfferingRelationship() {
		super();
	}

	public ProductOfferingRelationship(ProductOffering productOffering,
			ProductOffering relationshipProductOffering) {
		super(productOffering.getName());
		this.productOffering = productOffering;
		this.relationshipProductOffering = relationshipProductOffering;
	}

	public ProductOffering getProductOffering() {
		return productOffering;
	}

	public void setProductOffering(ProductOffering productOffering) {
		this.productOffering = productOffering;
	}

	public ProductOffering getRelationshipProductOffering() {
		return relationshipProductOffering;
	}

	public void setRelationshipProductOffering(
			ProductOffering relationshipProductOffering) {
		this.relationshipProductOffering = relationshipProductOffering;
	}

	private static IProductOfferingRelationshipFinder productOfferingRelationshipFinder;

    public static IProductOfferingRelationshipFinder getProductOfferingRelationshipFinder() {
        return productOfferingRelationshipFinder;
    }

    public static void setProductOfferingRelationshipFinder(
            IProductOfferingRelationshipFinder productOfferingRelationshipFinder) {
        ProductOfferingRelationship.productOfferingRelationshipFinder = productOfferingRelationshipFinder;
    }
}

 

这就是定义在规格下的一种具体关系。

/**
 * 主副机关系
 */
public class MasterSlaveProductOfferingRelationship extends
        ProductOfferingRelationship {

    private int maxSlaveCount;
    
    protected MasterSlaveProductOfferingRelationship() {
        
    }

    public MasterSlaveProductOfferingRelationship(ProductOffering productOffering,
            ProductOffering relationshipProductOffering,
            int maxSlaveCount) {
        super(productOffering, relationshipProductOffering);
        setMaxSlaveCount(maxSlaveCount);
    }
    
    public ProductOffering getMaster(){
    	return this.getRelationshipProductOffering();
    }
    
    public ProductOffering getSlave(){
    	return this.getProductOffering();
    }
    
    public int getMaxSlaveCount() {
        return this.maxSlaveCount;
    }
    
    public void setMaxSlaveCount(int maxRelationCount) {
        this.maxSlaveCount = maxRelationCount;
    }

    public static boolean isSlaveToMaster(Product product, Product masterProduct) {
        return getPurchaseAgreementProductFinder().findSlaves(masterProduct).contains(product); 
    }

    public static boolean isSlave(Product product) {
        IProductOfferingRelationshipFinder finder = getProductOfferingRelationshipFinder();
    
        ProductOffering productOffering = product.getProductOffering();
    
        List<MasterSlaveProductOfferingRelationship> relationships = finder.findBy(productOffering, MasterSlaveProductOfferingRelationship.class);
        
		return relationships.size() > 0;
    }

    public static boolean isMaster(Product product) {
        IProductOfferingRelationshipFinder finder = getProductOfferingRelationshipFinder();
    
        ProductOffering productOffering = product.getProductOffering();
    
        return finder.findByRelationshipProductOffering(productOffering, MasterSlaveProductOfferingRelationship.class).size() > 0;
    }
}

 

这才是实例化,只是放入规格。

/**
 * 产品在商品层面上的依赖关系的实例化
 */
public class ProductRelationshipFromProductOffering extends ProductRelationship {

	private ProductOfferingRelationship productOfferingRelationship;
	private static IProductRelationshipFromProductOfferingFinder productRelationshipFromProductOfferingFinder;
	
	protected ProductRelationshipFromProductOffering() {
		super();
	}

	public ProductRelationshipFromProductOffering(Product product,
            Product relationshipProduct) {
       this(product, relationshipProduct, 
           getProductOfferingRelationshipFinder().findBy(product.getProductOffering(), relationshipProduct.getProductOffering()));
    }

	public ProductRelationshipFromProductOffering(Product product,
			Product relationshipProduct,
			ProductOfferingRelationship productOfferingRelationship) {
	    super(product,relationshipProduct);
	    setProductOfferingRelationship(productOfferingRelationship);
	}

	public ProductOfferingRelationship getProductOfferingRelationship() {
        return this.productOfferingRelationship;
    }

    private void setProductOfferingRelationship(
            ProductOfferingRelationship productOfferingRelationship) {
        assert productOfferingRelationship != null : "productOfferingRelationship is null";
        this.productOfferingRelationship = productOfferingRelationship;
    }

	public static IProductRelationshipFromProductOfferingFinder getProductRelationshipFromProductOfferingFinder() {
		return productRelationshipFromProductOfferingFinder;
	}

	public static void setProductRelationshipFromProductOfferingFinder(
			IProductRelationshipFromProductOfferingFinder productRelationshipFromProductOfferingFinder) {
		ProductRelationshipFromProductOffering.productRelationshipFromProductOfferingFinder = productRelationshipFromProductOfferingFinder;
	}
}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics