다형성을 위한 프로토콜과 다중 메서드
Clojure는 매크로와 모듈식 설계를 보완하는 유연한 다형성 도구 두 가지를 제공합니다. 이 강의에서는 확장 가능하고 결합도가 낮은 코드를 위한 프로토콜과 다중 메서드를 배웁니다.
다형성을 위한 프로토콜과 다중 메서드은(는) CoddyKit의 무료 Clojure Functional Programming & JVM Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Clojure Functional Programming & JVM Backend Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Clojure Functional Programming & JVM Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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 typesChoosing 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.
자주 묻는 질문
“다형성을 위한 프로토콜과 다중 메서드” 강의는 무료인가요?
네 — “다형성을 위한 프로토콜과 다중 메서드” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clojure Functional Programming & JVM Backend Development 강의 전체를 잠금 해제할 수 있습니다. Clojure Functional Programming & JVM Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“다형성을 위한 프로토콜과 다중 메서드”에서 뭘 배우나요?
Clojure는 매크로와 모듈식 설계를 보완하는 유연한 다형성 도구 두 가지를 제공합니다. 이 강의에서는 확장 가능하고 결합도가 낮은 코드를 위한 프로토콜과 다중 메서드를 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Clojure Functional Programming & JVM Backend Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Clojure Functional Programming & JVM Backend Development을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Clojure Functional Programming & JVM Backend Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“다형성을 위한 프로토콜과 다중 메서드” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Clojure Functional Programming & JVM Backend Development 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Clojure Functional Programming & JVM Backend Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Clojure 매크로 이해하기
- 네임스페이스 정의 및 사용
- 모듈로 코드 구성하기
- 다형성을 위한 프로토콜과 다중 메서드