(nsdecorator(:require[clojure.spec.alpha:ass]))(s/def::idstring?)(s/def::items(s/coll-ofmap?:kindvector?))(s/def::total(s/andnumber?pos?))(s/def::customer-idstring?)(s/def::order(s/keys:req-un[::id::items::total::customer-id]))(defprotocolOrderProcessor"Protocol defining order processing operations"(process-order[thisorder]"Processes an order and returns the processed result")(get-order[thisorder-id]"Retrieves an order by its ID")(cancel-order[thisorder-id]"Cancels an order by its ID"))(defrecordBasicOrderProcessor[]OrderProcessor(process-order[_order]{:status:processed:orderorder:processed-at(java.time.Instant/now)})(get-order[_order-id]{:status:retrieved:order-idorder-id:retrieved-at(java.time.Instant/now)})(cancel-order[_order-id]{:status:cancelled:order-idorder-id:cancelled-at(java.time.Instant/now)}))(defrecordValidationDecorator[processor]OrderProcessor(process-order[_order](if(s/valid?::orderorder)(process-orderprocessororder)(throw(ex-info"Invalid order structure"{:error(s/explain-str::orderorder)}))))(get-order[_order-id](if(string?order-id)(get-orderprocessororder-id)(throw(ex-info"Invalid order ID"{:order-idorder-id}))))(cancel-order[_order-id](if(string?order-id)(cancel-orderprocessororder-id)(throw(ex-info"Invalid order ID"{:order-idorder-id})))))(defrecordLoggingDecorator[processor]OrderProcessor(process-order[_order](println"Processing order:"(:idorder))(let[result(process-orderprocessororder)](println"Order processed:"(:idorder))result))(get-order[_order-id](println"Retrieving order:"order-id)(let[result(get-orderprocessororder-id)](println"Order retrieved:"order-id)result))(cancel-order[_order-id](println"Cancelling order:"order-id)(let[result(cancel-orderprocessororder-id)](println"Order cancelled:"order-id)result)))(defrecordCachingDecorator[processor]OrderProcessor(process-order[_order](let[cache(atom{})](if-let[cached(get@cache(:idorder))]cached(let[result(process-orderprocessororder)](swap!cacheassoc(:idorder)result)result))))(get-order[_order-id](let[cache(atom{})](if-let[cached(get@cacheorder-id)]cached(let[result(get-orderprocessororder-id)](swap!cacheassocorder-idresult)result))))(cancel-order[_order-id](cancel-orderprocessororder-id)))(defrecordMetricsDecorator[processor]OrderProcessor(process-order[_order](let[start-time(System/nanoTime)result(process-orderprocessororder)end-time(System/nanoTime)duration(/(-end-timestart-time)1e6)](println"Order processing time:"duration"ms")result))(get-order[_order-id](let[start-time(System/nanoTime)result(get-orderprocessororder-id)end-time(System/nanoTime)duration(/(-end-timestart-time)1e6)](println"Order retrieval time:"duration"ms")result))(cancel-order[_order-id](let[start-time(System/nanoTime)result(cancel-orderprocessororder-id)end-time(System/nanoTime)duration(/(-end-timestart-time)1e6)](println"Order cancellation time:"duration"ms")result)))(defncreate-order-processor"Creates a new order processor with the specified decorators.
Available decorators: :validation, :logging, :caching, :metrics"[&decorators](let[base-processor(->BasicOrderProcessor)](reduce(fn[processordecorator](casedecorator:validation(->ValidationDecoratorprocessor):logging(->LoggingDecoratorprocessor):caching(->CachingDecoratorprocessor):metrics(->MetricsDecoratorprocessor)processor))base-processordecorators)))(comment(defprocessor(create-order-processor:validation:logging:caching:metrics))(defsample-order{:id"ORDER-123":items[{:product-id"PROD-1":quantity2:price10.0}{:product-id"PROD-2":quantity1:price15.0}]:total35.0:customer-id"CUST-456"})(process-orderprocessorsample-order)(get-orderprocessor"ORDER-123")(cancel-orderprocessor"ORDER-123")(process-orderprocessor(dissocsample-order:total)))
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)