It sounds like a use for the decorator pattern, or am I missing
something? In case I am not I have given it a shot using records and
protocols:
(defrecord TextField [text])
(defrecord DatetimePicker [datetimes])
(defrecord Label [text widget])
(defprotocol Renderable
(render [this]))
(extend-protocol Renderable
TextField
(render [this]
(str "[" (:text this) "]"))
DatetimePicker
(render [this]
(apply str (:datetimes this)))
Label
(render [this]
(str (:text this) ": " (render (:widget this)))))
(comment
(def textfield (TextField. "Foo"))
(def datetimepicker (DatetimePicker. [:yesterday :today :tomorrow]))
(def labeledtextfield (Label. "Name" textfield))
(def labeleddatetimepicker (Label. "Date" datetimepicker))
(render textfield) ;=> [Foo]
(render datetimepicker) ;=> :yesterday:today:tomorrow
(render labeledtextfield) ;=> Name: [Foo]
(render labeleddatetimepicker) ;=> Date: :yesterday:today:tomorrow
)
1/ is handled by the Renderable implementation of Label calling the
Renderable implementation of whatever widget is decorated by the Label
instance.
2/ is handled by having the label decorator as a record.
Regards,
Matthias
On 5 Feb., 22:15, Razvan Rotaru <[email protected]> wrote:
> Hi,
>
> I found some posts about this topic, but they did not clarify things
> in my head well enough, so I have to start my own... :)
>
> I'm basically craving for multiple inheritance or mixins, at least
> with my current way of thinking. I haven't really gone deep enough
> with multimethods or protocols, so I might be a little off track by
> looking for multiple inheritance.
>
> Let's take an example:
>
> I want to implement some gui widgets, so there's a main method
> "render" which draws the widget on the screen. I want to have two
> types:
>
> - Textfield
> - Datetimepicker
>
> Each can be "mixed" with the Label widget, so that we have:
>
> - LabelledTextfield
> - LabelledDatetimepicker
>
> So, two challenges occur:
> 1/ the render method for Label needs to call the render method for
> it's "mixed" widget (either Textfield or Datetimepicker) - let's
> assume that labels are rendered "around" the actual widget, so that we
> have a simple way to mix the implementations of render method
> 2/ the Label widget comes with its own data (the label text)
>
> For the second thing, I think it will work with defrecords, since the
> label can be a map entry which can be stored in the actual widget,
> even if it's not mixed with Label. I still want to point this out in
> case there are other ways of achieving this.
>
> How can I achieve 1? Are there alternatives for 2?
>
> Thanks for reading,
> Razvan
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en