Git Product home page Git Product logo

specification-pattern-samples's Introduction

Specification-Pattern-Samples

Specification Pattern(Hard coded, Parameterized and Composite) samples, included Eric Evans and Martin Fowler samples

Wikipedia defines Specification as follows :

"In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.

A specification pattern outlines a business rule that is combinable with other business rules. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate Composite Specification class. The Composite Specification class has one function called IsSatisfiedBy that returns a boolean value. After instantiation, the specification is "chained" with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore, upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository."

There are various implementations of the specification pattern

  1. Hard Coded Specification
  2. Parameterized Specification
  3. Composite Specification

Here I tried to implemented all of them with a simple example of mathematical operators based on C# .

This pattern was introduced first by Eric Evans and Martin Fowler When they worked at Silicon Valley.

In their original article, they use several examples, including cargoes and containers.Some peace of their's example has been implemented here.

I also use a very simple example to show hard coded and parameterized specification, and also how we can combine each specifications using the logical opertors and composite pattern.

For example we have two svery simple MathSpecification

  1. GreaterThanMathSpecification
  2. LowerThanMathSpecification

Without for Between operator, we have to implemented a new specification, for example :

public class BetweenMathSpecification : MathSpecification
    {
        private readonly int _left, _right;
        public BetweenMathSpecification(int left, int rigth)
        {
            _left = left;
            _right = rigth;
        }

        public override bool IsSatisfiedBy(int candidate)
        {
            return candidate > _left && candidate < _right;
        }
    }

But by using CompositeSpecification we can combine this simple operator and create Between without we have to implement a new specification

var specification = new
                MathOperations.ParameterizedSpec.GreaterThanMathSpecification(-5)
                .And(new MathOperations.ParameterizedSpec.LowerThanMathSpecification(5));

specification-pattern-samples's People

Contributors

masoud-bahrami avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lesliekuo

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.