Book Image

Clojure Polymorphism

By : Paul Stadig
Book Image

Clojure Polymorphism

By: Paul Stadig

Overview of this book

Clojure is a modern, dynamic language that you can use to develop robust, multithreaded programs. Clojure Polymorphism is a comprehensive guide that shows you how to use Clojure’s features to your advantage. The book begins by describing examples that show how to define and implement abstractions with plain functions and multimethods. Then you'll analyze these examples and separate the good and bad aspects of their design principles. You'll also learn how to perform data transformation abstraction with a plain function and discover how to write new cross-platform predicates while keeping the core of your abstraction free from reader conditionals. The later chapters explain the considerations to keep in mind when implementing Clojure protocols on the Java Virtual Machine (JVM). By the end of this book, you’ll know how to use the various polymorphic tools of Clojure to your advantage while designing your applications.
Table of Contents (7 chapters)

Service Abstraction

Polymorphism is primarily used to create an abstraction that can be implemented in several ways. An abstraction that I have created many times is a service abstraction. For instance, you may want to define a storage service abstraction modeled as a key-value store for binary objects. You could then implement it for S3, CloudFiles, and Azure. For use in tests, you could even implement a local filesystem backend.

My abstraction will consist of five functions: connect, get, put, delete, and close. You call connect to construct a service object. The service object will be used with get to fetch an object from the store, put to store an object in the store, delete to delete an object from the store, and close to clean up the service object.

There are a couple of different ways of defining and implementing this abstraction. Each will have its own advantages and disadvantages. Let's first take a look at them, and then I will pick them apart for some design principles...