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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Introduction to Retrieval Augmented Generation (RAG)
  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
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!