DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  1. DZone
  2. Coding
  3. Languages
  4. Specification Pattern by Scala

Specification Pattern by Scala

By 
Slim Ouertani user avatar
Slim Ouertani
·
Nov. 03, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
8.6K Views

Join the DZone community and get the full member experience.

Join For Free

 Specification pattern is simple and clean solution to implement your business rules.  This pattern is mixture between composite and factory Gof patterns introduced by Eric Evans & Martin Fowler.

In this blog, we will not explain how this pattern works, but you can refer to wikipedia and Xebia  for more details. But, we try here to translate specification pattern using the Scala language.

Implementation :
UML diag

package me.jtunisie.specifications


trait TSpecification [T]{
def ->(candidate :T ):Boolean
def ||( specification : TSpecification[T]):TSpecification[T]
def &&(specification :TSpecification[T] ):TSpecification[T]
def unary_!():TSpecification[T]
//Addition
def | ( specification : TSpecification[T]):TSpecification[T]
def & (specification :TSpecification[T] ):TSpecification[T]
}

The last two methods has been added compared to the specification. Sometimes, we need to do full check as A || B is not always equal to B || A. {Take as example true || (1/0 == 0 ). "We have encountered this problem when we validate an old embedded compiler written in C and in some platform  like Solaris ( A || B ) starts by checking B and then A. }
 abstract class ASpecification[T] extends  TSpecification [T]{

def ->(candidate :T ):Boolean
def ||( s : TSpecification[T]):TSpecification[T] = OrSpecification(this,s)
def &&(s :TSpecification[T] ):TSpecification[T] = AndSpecification(this,s)
def unary_!():TSpecification[T] = NotSpecification(this)

// Recurssive Add-0ns
def | ( s : TSpecification[T]):TSpecification[T] = ROrSpecification(this,s)
def &(s :TSpecification[T] ):TSpecification[T] = RAndSpecification(this,s)

}

To implement recursive and not recursive behavior, we have used /: and \: .
According to documentation :
(z /: List(a, b, c)) (op)  equal to op(op(op(z, a), b), c)

and

(List(a, b, c) :\ z) (op)  equal to op(a, op(b, op(c, z)))

case class AndSpecification[T]( s: TSpecification[T]* ) extends ASpecification[T]{
override def -> (candidate :T ):Boolean= (true /: s) (_ -> candidate && _ -> candidate)

}
case class OrSpecification[T]( s: TSpecification[T]* ) extends ASpecification[T]{
override def ->(candidate :T ):Boolean= (false /: s) (_ -> candidate || _ -> candidate)
}
case class NotSpecification[T]( s: TSpecification[T] ) extends ASpecification[T]{
override def ->(candidate :T )= ! (s -> candidate )
}


case class RAndSpecification[T]( s: TSpecification[T]* ) extends ASpecification[T]{
override def ->(candidate :T ):Boolean= (s :\ true) (_ -> candidate && _ -> candidate)
}
case class ROrSpecification[T]( s: TSpecification[T]* ) extends ASpecification[T]{
override def ->(candidate :T ):Boolean= (s :\ false) (_ -> candidate || _ -> candidate)
}

This code will not compile. In fact, Boolean class doesn't have -> method (IsSatisfiedBy). By Scala, we can use open class tool ( adding this method to Boolean class).

Here is the implicit implementation :

package me.jtunisie

package object specifications {

class MyBool(b:Boolean){
def ->(candidate :Any ):Boolean = b
}
implicit def toMyBool(b:Boolean)= new MyBool(b)
}

source code is under  : http://github.com/ouertani/Rules

Mode of use ( with precedence checked ):

Object AlwaysOk extends ASpecification[Boolean] { override def -> (b: Boolean)= true } object AlwaysKo extends ASpecification[Boolean] { override def -> (b: Boolean)= false }

 

object AlwaysOk extends ASpecification[Boolean] {
override def -> (b: Boolean)= true
}
object AlwaysKo extends ASpecification[Boolean] {
override def -> (b: Boolean)= false
}


false mustBe ((AlwaysOk || AlwaysKo) && AlwaysKo)-> (true)
true mustBe (AlwaysOk || (AlwaysKo && AlwaysKo))-> (true)
true mustBe ( AlwaysOk || AlwaysKo && AlwaysKo)-> (true)

 

Specification pattern Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: