Giter VIP home page Giter VIP logo

ddd-with-specification-pattern's Introduction

Example DDD + Specification Design Pattern

This project is based on the DDD + Specification pattern.
Add as many comparison logics as necessary for various logics or domains at the abstract level.

The example only uses AND logic, but OR and NOT can also be used if necessary.

  • npm i or npm install
  • npm run example
const currentProduct = new Product("book", 1000, 0);
const currentOrder = new Order(currentProduct, 1, 1000);
const wrongProduct = new Product("book", 0, 0);
const wrongOrder = new Order(wrongProduct, 0, 0);

const productPriceMustBeGreaterZeroSpecification =
  new ProductPriceMustBeGreaterZeroSpecification();
const productNameIsNotBlankSpecification =
  new ProductNameIsNotBlankSpecification();
const productDiscountWithinLimitSpecification =
  new ProductDiscountWithinLimitSpecification();

const orderPriceConsistencySpecification =
  new OrderPriceConsistencySpecification();
const orderQuantityMustBeGreaterZeroSpecification =
  new OrderQuantityMustBeGreaterZeroSpecification();

const productAndSpecification = productPriceMustBeGreaterZeroSpecification
  .and(productNameIsNotBlankSpecification)
  .and(productDiscountWithinLimitSpecification);

const orderAndSpecification = orderPriceConsistencySpecification.and(
  orderQuantityMustBeGreaterZeroSpecification
);

// true
const currentProductOutput =
  productAndSpecification.isSatisfiedBy(currentProduct);
console.log(`currentProductOutput = ${currentProductOutput}`);

// true
const currentOrderOutput = orderAndSpecification.isSatisfiedBy(currentOrder);
console.log(`currentOrderOutput = ${currentOrderOutput}`);

// false
const wrongProductOutput = productAndSpecification.isSatisfiedBy(wrongProduct);
console.log(`wrongProductOutput = ${wrongProductOutput}`);

// false
const wrongOrderOutput = orderAndSpecification.isSatisfiedBy(wrongOrder);
console.log(`wrongOrderOutput = ${wrongOrderOutput}`);

This project is composed of and, or, and not.

interface Specification<T> {
  isSatisfiedBy(candidate: T): boolean;
}

abstract class CompositeSpecification<T> implements Specification<T> {
  abstract isSatisfiedBy(candidate: T): boolean;

  and(specification: CompositeSpecification<T>): CompositeSpecification<T> {
    return new AndSpecification<T>(this, specification);
  }

  or(specification: CompositeSpecification<T>): CompositeSpecification<T> {
    return new OrSpecification<T>(this, specification);
  }

  not(): NotSpecification<T> {
    return new NotSpecification<T>(this);
  }
}

Example Domain & Specification

  • Order
    • OrderPriceConsistencySpecification
    • OrderQuantityMustBeGreaterZeroSpecification
/**
 * @class OrderPriceConsistencySpecification
 * @description
 * The order price cannot exceed or be less than the total sum of the product price and discount price.
 */
class OrderPriceConsistencySpecification extends CompositeSpecification<Order> {
  isSatisfiedBy(candidate: Order): boolean {
    if (!candidate) return false;

    const totalPrice =
      candidate.quantity *
      (candidate.product.price - candidate.product.discountPrice);

    return candidate.totalPrice === totalPrice;
  }
}

/**
 * @class OrderQuantityMustBeGreaterZeroSpecification
 * @description
 * This specification ensures that the order quantity must be greater than zero.
 */
class OrderQuantityMustBeGreaterZeroSpecification extends CompositeSpecification<Order> {
  isSatisfiedBy(candidate: Order): boolean {
    if (!candidate) return false;

    return candidate.quantity > 0;
  }
}
  • Product
    • ProductPriceMustBeGreaterZeroSpecification
    • ProductNameIsNotBlankSpecification
    • ProductDiscountWithinLimitSpecification
/**
 * @class ProductPriceMustBeGreaterZeroSpecification
 * @description
 * The price of the product must always be greater than 0.
 */
class ProductPriceMustBeGreaterZeroSpecification extends CompositeSpecification<Product> {
  isSatisfiedBy(candidate: Product): boolean {
    if (!candidate) return false;

    return candidate.price > 0;
  }
}

/**
 * @class ProductNameIsNotBlankSpecification
 * @description
 * The product name cannot be blank.
 */
class ProductNameIsNotBlankSpecification extends CompositeSpecification<Product> {
  isSatisfiedBy(candidate: Product): boolean {
    if (!candidate) return false;

    return candidate.name.length > 0;
  }
}

/**
 * @class ProductDiscountWithinLimitSpecification
 * @description
 * The discounted price of a product cannot be greater than the normal price.
 */
class ProductDiscountWithinLimitSpecification extends CompositeSpecification<Product> {
  isSatisfiedBy(candidate: Product): boolean {
    if (!candidate) return false;

    return candidate.discountPrice < candidate.price;
  }
}

Author

Hyunwoo Park

ddd-with-specification-pattern's People

Contributors

awakelife93 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.