-
-
Save fxg42/615e34fb68d901e50582 to your computer and use it in GitHub Desktop.
Petit exemple DCI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
@ToString class CompteEpargne { | |
def signataire, balance | |
} | |
@ToString class MargeCredit { | |
def signataire, limiteCredit, credit | |
} | |
@ToString class Facture { | |
def nom, numeroReference | |
} | |
// Rôles. | |
class CompteEpargneSource { | |
static fondsDisponibles (self) { | |
self.balance | |
} | |
static retirer (self, montant) { | |
self.balance -= montant | |
} | |
} | |
class MargeCreditSource { | |
static fondsDisponibles (self) { | |
self.limiteCredit - self.credit | |
} | |
static retirer (self, montant) { | |
self.credit += montant | |
} | |
} | |
class MargeCreditDestination { | |
static deposer (self, montant) { | |
self.credit -= montant | |
} | |
} | |
class CompteEpargneDestination { | |
static deposer (self, montant) { | |
self.balance += montant | |
} | |
} | |
class FactureDestination { | |
// Effectuer un paiement de facture | |
static deposer (self, montant) { | |
self.transmettrePaiement(montant) | |
} | |
static transmettrePaiement (self, montant) { | |
// Pourrait déclencher un autre scénario... | |
} | |
} | |
// Scénario | |
class TransfererFonds { | |
def source, destination, montant | |
// Contexte | |
private static final castings = [ | |
"Source": [ | |
(CompteEpargne.class): CompteEpargneSource, | |
(MargeCredit.class): MargeCreditSource, | |
], | |
"Destination": [ | |
(CompteEpargne.class): CompteEpargneDestination, | |
(MargeCredit.class): MargeCreditDestination, | |
(Facture.class): FactureDestination, | |
] | |
] | |
def TransfererFonds (source, destination, montant) { | |
this.montant = montant | |
this.source = source | |
this.destination = destination | |
cast(source, "Source") | |
cast(destination, "Destination") | |
} | |
private cast (acteur, role) { | |
def roles = castings[role][acteur.class] | |
if (!roles) | |
throw new IllegalArgumentException("${acteur.class.simpleName} ne peut jouer le role de ${role} dans un transfert de fonds") | |
acteur.metaClass.mixin(roles) | |
} | |
def go () { | |
// debuter transaction & logging | |
if (source.fondsDisponibles() < montant) | |
throw new IllegalArgumentException("fonds insuffisants") | |
// rollback & logging | |
source.retirer(montant) | |
destination.deposer(montant) | |
// commit & logging | |
// journaliser | |
} | |
} | |
println "" | |
println "Un étudiant veux transmettre 1000.00\$ à un enseignant à partir de sa marge de crédit." | |
println "======================================================================================" | |
etudiant = new MargeCredit(signataire: "un etudiant", credit: 0.00, limiteCredit: 1500.00) | |
enseignant = new CompteEpargne(signataire: "un enseignant", balance: 10000.00) | |
println "AVANT" | |
println etudiant | |
println enseignant | |
new TransfererFonds(etudiant, enseignant, 1000.00).go() | |
println "APRÈS" | |
println etudiant | |
println enseignant | |
println "" | |
println "Un étudiant veux payer une facture." | |
println "===================================" | |
epargne = new CompteEpargne(signataire: "un etudiant", balance: 500.00) | |
facture = new Facture(nom: "Vidéotron", numeroReference: "123") | |
println "AVANT" | |
println epargne | |
new TransfererFonds(epargne, facture, 100.00).go() | |
println "APRÈS" | |
println epargne | |
println "" | |
println "Un étudiant veux rembourser sa marge de crédit." | |
println "===============================================" | |
epargne = new CompteEpargne(signataire: "un etudiant", balance: 500.00) | |
marge = new MargeCredit(signataire: "un etudiant", credit: 100.00, limiteCredit: 1500.00) | |
println "AVANT" | |
println epargne | |
println marge | |
new TransfererFonds(epargne, marge, 100.00).go() | |
println "APRÈS" | |
println epargne | |
println marge |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment