concept protocol in category clojure

This is an excerpt from Manning's book Clojure: The Essential Reference.
Here’s some code that implements the protocol:
instance?, in production use, should appear sparingly. When a fewinstance?calls accumulate in a cascading "if-then-else" or a cond expression it’s probably time to think about something more flexible such as protocols. Multimethods are also an option, but protocols are specifically designed to handle type dispatch efficiently.
(defprotocol IFace ; #1 (m1 [this]) (m2 [this]) (m3 [this])) (def AFace ; #2 {:m1 (fn [this] (str "AFace::m1")) :m2 (fn [this] (str "AFace::m2"))}) (defrecord MyFace []) ; #3 (extend MyFace IFace (assoc AFace :m1 (fn [this] (str "MyFace::m1")))) ; #4 (m1 (->MyFace)) ; #5 ;; "MyFace::m1" (m2 (->MyFace)) ; #6 ;; "AFace::m2" (m3 (->MyFace)) ; #7 ;; No implementation of method: :m3 of protocol: #'user/IFaceImplementing the protocol at declaration time adds the methods to the interface of the generated class, making them visible from Java but also impossible to extend. Once the implementation is attached at declaration time it can’t change unless the entire record is redefined.
Implementing the protocol by extension allows instances of the record to be extended to the protocol at some later time, including the option to change the implementation without redefining the record.

This is an excerpt from Manning's book Clojure in Action, Second Edition.
This book covers Clojure version 1.6. In the first few chapters you’ll learn the fundamentals of Clojure: its syntax, building blocks, data structures, Java interoperability, and concurrency features. As we progress beyond the basics, you’ll learn how Clojure can simplify larger programs using macros, protocols and records, and higher-order functions. By the end of this book you’ll understand why Clojure is traveling a rare path to popularity and how it can transform your approach to developing software.
Clojure’s multimethods are more expressive than Java’s object methods but much slower because Java is optimized for single dispatch on type, not arbitrary multiple dispatch. In most cases the performance difference is negligible and the increased expressiveness of code more than makes up for it. But as Clojure matures and moves more of its implementation into Clojure itself, there needs to be a way to support its abstraction and data-definition facilities without this performance hit. Protocols and data types are that solution, and they also offer a high-performance solution to a commonly encountered subset of the expression problem by using Java’s extremely fast single dispatch on type.
In this section, we’ll examine what protocols and data types are and how they can be used. Keep in mind your design of modus operandi as you work through this.
The term protocol means the way something is done, often predefined and followed by all participating parties. Clojure protocols are analogous to your modus operandi, and defprotocol is to protocols what def-modus-operandi is to modi operandi. Similarly, extend-protocol is to protocols what detail-modus-operandi is to modi operandi.
The protocol as well as the methods that form it can accept doc strings. A call to defprotocol results in a bunch of vars being created: one for the protocol itself and one for each polymorphic function (or method) that’s a part of the protocol. These functions dispatch on the type of the first argument (and therefore must have at least one argument), and by convention, the first argument is called this. So from listing 9.5, the following snippet defines a protocol named ExpenseCalculations:
(defprotocol ExpenseCalculations (total-cents [e]) (is-category? [e category]))You’re defining a set of related methods (total-cents and is-category?) that can be implemented any number of times by any data type. A call to defprotocol also generates an underlying Java interface. So, because the previous code exists in the namespace clj-in-act.ch9.expense-protocol, it will result in a Java interface called chapter_protocols.expense_protocol.ExpenseCalculations. The methods in this interface will be the ones specified in the definition of the protocol, total _cents and is_category_QMARK. The reference to QMARK is thanks to the translated name of a Clojure function (one that ends with a question mark) into Java.[2] The fact that defprotocol generates a Java interface also means that if some other Java code wants to participate in a protocol, it can implement the generated interface and proceed as usual.
This might look similar to the code you generated in the implementation of your modus operandi. For each protocol and data type pair, extend accepts a map that describes participation of that data type in the protocol. The keys of the map are keyword versions of the names of the methods, and the values are the function bodies that contain the implementation for each. The extend function is the most flexible in terms of building an implementation of a protocol.
Figure 9.2 shows the conceptual flow of defining and using protocols.
Figure 9.2. Calling defprotocol performs an analogous operation where a var is created to hold information about the protocol and its implementors. The underlying implementation will also result in a Java interface that pertains to the protocol being defined. Calls to extend, extend-type, and extend-protocol will update the var with implementor details and generate Java classes that implement the protocol.
![]()
We’ve covered protocols and how they’re defined and used. We’ll say another couple of things about them before moving on to the remaining topics of this chapter.

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.
Protocols are somewhat related to Java interfaces and a distillation of what are commonly known as mix-ins. The Concatenatable protocol defines a single function cat that takes two arguments, the target object and another object, to concatenate to it. But the Concatenatable protocol describes only a sketch of the functions that form a protocol for concatenation—there is as of yet no implementation of this protocol. In the next section, we’ll discuss protocols in more detail, including their extension to new and existing types.
A final benefit of records is how well they integrate with Clojure protocols. But to fully understand how they relate, we must first explore what protocols are.
(extend-type String StringOps (upp [s] (clojure.string/upper-case s))) (upp "Works") ;=> "WORKS" (rev "Works?") ; IllegalArgumentException No implementation of method: :rev ; of protocol: #'user/StringOps found for ; class: java.lang.String
We showed in the previous section that Clojure multimethods provide a way to achieve runtime polymorphism based on arbitrary dispatch functions. Although extremely powerful, multimethods are sometimes less than ideal. Interposing a dispatch function into the polymorphism machinery isn’t always conducive to raw speed. Likewise, dispatching on an arbitrary function is often overkill. Therefore, Clojure provides facilities for creating logically grouped polymorphic functions that are both simple and performant—types, records, and protocols. We’ll delve into these topics in this section and introduce the concept of abstraction-oriented programming, predicated on the creation of logical groupings. But first, we’ll discuss the simplest of the three topics, records, which you might recognize.