0Pricing
Clojure Functional Programming & JVM Backend Development · Lektion

Protokolle und Multimethoden für Polymorphie

Clojure bietet zwei flexible Werkzeuge für Polymorphie, die Macros und modulares Design ergänzen. Diese Lektion vermittelt Protokolle und Multimethoden für erweiterbaren, entkoppelten Code.

Protokolle und Multimethoden für Polymorphie ist eine kostenlose Clojure Functional Programming & JVM Backend Development-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Clojure Functional Programming & JVM Backend Development-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Clojure Functional Programming & JVM Backend Development-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Polymorphism in Clojure

Clojure separates data from behavior. Protocols and multimethods let you dispatch behavior without classes or inheritance.

Defining a Protocol

A defprotocol names a set of method signatures, like an interface.

(defprotocol Shape
  (area [s])
  (perimeter [s]))

Implementing With a Record

Use defrecord to create a type that implements the protocol.

(defrecord Circle [r]
  Shape
  (area [_] (* Math/PI r r))
  (perimeter [_] (* 2 Math/PI r)))
(println (area (->Circle 2)))

Dispatch on Type

Protocols dispatch on the type of the first argument. This is fast and the most common polymorphism in Clojure.

Extending Existing Types

Use extend-protocol to add behavior to types you do not own, even built-in ones.

(extend-protocol Shape
  java.lang.Long
  (area [n] (* n n)))
(println (area 5))

When You Need More

Protocols only dispatch on type. When you must dispatch on a value, multiple arguments, or a computed key, reach for multimethods.

Defining a Multimethod

defmulti takes a dispatch function; defmethod supplies an implementation per dispatch value.

(defmulti describe :kind)
(defmethod describe :dog [_] "woof")
(defmethod describe :cat [_] "meow")
(println (describe {:kind :dog}))

The Default Method

Provide a :default method to handle unmatched dispatch values gracefully.

(defmethod describe :default [_] "unknown")
(println (describe {:kind :fish}))

Dispatch on Anything

The dispatch function can compute any value, even from multiple arguments, giving multimethods far more flexibility than protocols.

(defmulti combine (fn [a b] [(:type a) (:type b)]))
; dispatch on a pair of types

Choosing Between Them

Prefer protocols for type-based, performance-sensitive dispatch. Use multimethods when dispatch logic is richer than a single type.

Open for Extension

Both tools are open: new types or methods can be added later in separate namespaces without editing the original code, solving the expression problem cleanly.

Quick Check

Test your polymorphism knowledge.

Recap

You learned protocols with defrecord and extend-protocol for type dispatch, multimethods with defmulti/defmethod for value dispatch, default methods, and how both keep code open for extension.

Häufig gestellte Fragen

Ist die Lektion „Protokolle und Multimethoden für Polymorphie“ kostenlos?

Ja — der vollständige Text von „Protokolle und Multimethoden für Polymorphie“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Clojure Functional Programming & JVM Backend Development-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Clojure Functional Programming & JVM Backend Development-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Protokolle und Multimethoden für Polymorphie“?

Clojure bietet zwei flexible Werkzeuge für Polymorphie, die Macros und modulares Design ergänzen. Diese Lektion vermittelt Protokolle und Multimethoden für erweiterbaren, entkoppelten Code. Du übst Clojure Functional Programming & JVM Backend Development mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Clojure Functional Programming & JVM Backend Development zu starten?

Keine Vorkenntnisse erforderlich. Clojure Functional Programming & JVM Backend Development auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Protokolle und Multimethoden für Polymorphie“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Clojure Functional Programming & JVM Backend Development-Lektion Code schreiben und ausführen?

Ja. Jede Clojure Functional Programming & JVM Backend Development-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Clojure-Makros verstehen
  2. Namespaces definieren und verwenden
  3. Code mit Modulen organisieren
  4. Protokolle und Multimethoden für Polymorphie
← Zurück zu Clojure Functional Programming & JVM Backend Development