dev-resources.site
for different kinds of informations.
Clojure is Awesome!!!
Published at
12/14/2024
Categories
clojure
factory
designpatterns
clj
Author
borba
Main Article
Author
5 person written this
borba
open
Nothing to say, just... Clojure Is Awesome!
(ns factory
(:require [clojure.string :as str]))
(defprotocol DeliveryService
"Interface for delivery services."
(calculate-cost [this distance] "Calculates the delivery cost based on the distance.")
(process-delivery [this package destination] "Executes the delivery of the package to the destination."))
(defrecord AirDelivery []
DeliveryService
(calculate-cost [_ distance]
(* 5 distance))
(process-delivery [_ package destination]
(str "Package '" package "' will be delivered via Air to " destination)))
(defrecord LandDelivery []
DeliveryService
(calculate-cost [_ distance]
(* 2 distance))
(process-delivery [_ package destination]
(str "Package '" package "' will be delivered via Land to " destination)))
(defrecord SeaDelivery []
DeliveryService
(calculate-cost [_ distance]
(* 1 distance))
(process-delivery [_ package destination]
(str "Package '" package "' will be delivered via Sea to " destination)))
(defn delivery-factory
"Factory that creates a delivery service based on the given type."
[type]
(case (str/lower-case type)
"air" (->AirDelivery)
"land" (->LandDelivery)
"sea" (->SeaDelivery)
(throw (IllegalArgumentException.
(str "Invalid delivery type: " type)))))
(defn calculate-and-deliver
"Service that uses the factory to calculate the cost and deliver a package."
[type package destination distance]
(let [service (delivery-factory type)]
{:cost (calculate-cost service distance)
:delivery (process-delivery service package destination)}))
(comment
(calculate-and-deliver "air" "Box of Parts" "Pernambuco" 100)
;; => {:cost 500, :delivery "Package 'Box of Parts' will be delivered via Air to Pernambuco"}
(calculate-and-deliver "land" "Toolbox" "Rio Grande Do Sul" 50)
;; => {:cost 100, :delivery "Package 'Toolbox' will be delivered via Land to Rio Grande Do Sul"}
(calculate-and-deliver "space" "Heavy Cargo" "Port of Santos" 200)
;; => IllegalArgumentException: Invalid delivery type: space
)
clojure Article's
30 articles in total
25 Must-Check Clojure Resources for Developers: Tutorials, Tools, and Tips
read article
Is it easy to manage a team of highly qualified engineers?
read article
[PT-BR] Functional vs OOP: Uma análise profunda dos paradigmas de programação
read article
From Chaos to Control
read article
The capacity to learn new languages is very important
read article
Transducer: A powerful function composition pattern
read article
Clojure Is Awesome!!! [PART 4]
read article
Episode 3: Once you try Clojure, there is no way back
read article
Clojure Is Awesome!!! [PART 3]
read article
Clojure Is Awesome!!! [PART 2]
read article
Clojure in Product podcast
read article
Early termination of transducers and reducing functions
read article
Clojure is Awesome!!!
currently reading
To transduce or not to transduce?
read article
Clojure in Product podcast, 2nd episode
read article
Clojure REPL-Driven Development with VS Code
read article
10 Soft Skills que Aprendi Durante 3 Anos Criando Soluções para 100 Milhões de Usuários
read article
Scheming About Clojure
read article
Calling Clojure from Java using a real example (Clojure + Quarkus)
read article
Querido Yo del Futuro: Hoy intentaremos configurar una aplicación fullstack en Clojure
read article
Never call the same function twice (with memoization)
read article
The Clojure Paradox
read article
Why I chose Clojure/Script for building Vade Studio
read article
How I’m learning Clojure in 2024
read article
Krestianstvo Electric Lazy Reflector for Croquet VM
read article
Implementing a 2d-tree in Clojure
read article
Need microservice? Take Clojure!
read article
Transducers and Eduction in Clojure simply explained
read article
Meet Datomic: the immutable and functional database.
read article
Java Allergies and Revisiting java.time
read article
Featured ones: