On 12.12.2012 02:16, Mark Engelberg wrote:

Hi Mark,

Here's my take on the problem. I hope you can adapt it to your situation.

(ns example.canvas
  "A canvas is an abstract interface to a graphics API. The canvas
namespace does not build new functionality on top of the APIs, that's a
job for another namespace.")

(defprotocol Canvas
  (draw-triangle [canvas point-a point-b point-c])
  (draw-circle [canvas position size]))

(defmulti make-canvas-of-type (fn [type] type))

(defn make-canvas
  ([] (make-canvas-of-type :svg))
  ([type] (make-canvas-of-type type)))

;---

(ns example.svg-canvas
  "A canvas that produces SVG images."
  (:import example.canvas.Canvas)
  (:require [example.canvas :refer [make-canvas-of-type]]))

(defmethod make-canvas-of-type :svg [_]
  (reify Canvas
    (comment ...)))

;---

(ns example.png-canvas
  "A canvas that produces PNG images."
  (:import example.canvas.Canvas)
  (:require [example.canvas :refer [make-canvas-of-type]]))

(defmethod make-canvas-of-type :png [_]
  (reify Canvas
    (comment ...)))

;---

(ns example.artist
  "The artist uses a canvas to provide advanced rendering operations. It
can work with any canvas you give it, so it doesn't care what kind of
different canvases there are."
  (:require [example.canvas :refer [draw-triangle draw-square]]))

(defn create-masterpiece [canvas triangle-budget circle-budget]
  "Bigger budgets = better art!"
  (comment
    (draw-triangle ...)
    (draw-circle ...)))

;---

(ns example.patron
  "The patron doesn't have an artistic vision (that's the artist's job),
nor does it know how to talk to a graphics API (that's what the canvases
are for). What it *does* know is how many triangles and squares we can
afford, and where we want them drawn."
  (:require [example.artist :refer [create-masterpiece]
             example.canvas :refer [make-canvas]]))

(defn commision-a-masterpiece []
  (create-masterpiece (make-canvas) 500 1000))


-- 
Timo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to