Logo

dev-resources.site

for different kinds of informations.

Clojure is Awesome!!!

Published at
12/14/2024
Categories
clojure
factory
designpatterns
clj
Author
borba
Author
5 person written this
borba
open
Clojure is Awesome!!!

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
)
Enter fullscreen mode Exit fullscreen mode
clojure Article's
30 articles in total
Favicon
25 Must-Check Clojure Resources for Developers: Tutorials, Tools, and Tips
Favicon
Is it easy to manage a team of highly qualified engineers?
Favicon
[PT-BR] Functional vs OOP: Uma análise profunda dos paradigmas de programação
Favicon
From Chaos to Control
Favicon
The capacity to learn new languages is very important
Favicon
Transducer: A powerful function composition pattern
Favicon
Clojure Is Awesome!!! [PART 4]
Favicon
Episode 3: Once you try Clojure, there is no way back
Favicon
Clojure Is Awesome!!! [PART 3]
Favicon
Clojure Is Awesome!!! [PART 2]
Favicon
Clojure in Product podcast
Favicon
Early termination of transducers and reducing functions
Favicon
Clojure is Awesome!!!
Favicon
To transduce or not to transduce?
Favicon
Clojure in Product podcast, 2nd episode
Favicon
Clojure REPL-Driven Development with VS Code
Favicon
10 Soft Skills que Aprendi Durante 3 Anos Criando Soluções para 100 Milhões de Usuários
Favicon
Scheming About Clojure
Favicon
Calling Clojure from Java using a real example (Clojure + Quarkus)
Favicon
Querido Yo del Futuro: Hoy intentaremos configurar una aplicación fullstack en Clojure
Favicon
Never call the same function twice (with memoization)
Favicon
The Clojure Paradox
Favicon
Why I chose Clojure/Script for building Vade Studio
Favicon
How I’m learning Clojure in 2024
Favicon
Krestianstvo Electric Lazy Reflector for Croquet VM
Favicon
Implementing a 2d-tree in Clojure
Favicon
Need microservice? Take Clojure!
Favicon
Transducers and Eduction in Clojure simply explained
Favicon
Meet Datomic: the immutable and functional database.
Favicon
Java Allergies and Revisiting java.time

Featured ones: