Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clojure Polymorphism

You're reading from   Clojure Polymorphism Leverage Clojure's polymorphic tools to develop your applications

Arrow left icon
Product type Paperback
Published in Nov 2019
Publisher
ISBN-13 9781838982362
Length 56 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paul Stadig Paul Stadig
Author Profile Icon Paul Stadig
Paul Stadig
Arrow right icon
View More author details
Toc

Protocol + Client Namespace + Multimethod

You could use a protocol to define the abstraction, except connect, which is a multimethod:

1 (ns stadig.storage.protocol

2   (:refer-clojure :exclude [get]))

3

4 (defprotocol IStorage

5   (get [this bucket key])

6   (put [this bucket key value])

7   (delete [this bucket key])

8   (close [this]))

9

10 (defmulti connect :backend)

Implement the protocol and multimethod for each backend:

1 (ns stadig.storage.s3

2   (:require

3    [aws.sdk.s3 :as s3]

4    [stadig.storage.protocol :as proto]))

5

6 (defrecord S3Storage

7     [access-key secret-key]

8   proto/IStorage

9   (get [this bucket key]

10     (s3/get-object this bucket key))

11   (put [this bucket key value]

12     (s3/put-object this bucket key value...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image