Hi, Am 31.01.2009 um 02:19 schrieb CuppoJava:
(defn new_sprite_engine [] ... (add_sprite (new_cursor_sprite)) ...) So, my code (I think) is pretty well separated in terms of responsibility. But as it's structured right now, I can't load the file. Is there an elegant way of going about this?
This form is not very extensible! Whenever you want to add a new sprite you have to modify new_sprite_engine. And I understand, that adding sprites will be done several times. (Disclaimer: I'm not a professional software engineer. Question the following and be sceptical!) I would suggest to add another function register-sprite, which is called from the sprite library. sprite_engine.clj--------------- (def *sprites* (atom [])) (defn register-sprite [ctor] (swap! *sprites* conj ctor)) (defn new-sprite-engine [] ... (doseq [sprite *sprites*] (add-sprite (sprite))) ...) cursor_sprite.clj-------------- (defn new-cursor-sprite [] ...) ... (register-sprite new-cursor-sprite) user.clj------------------------ (use 'sprite-engine 'cursor-sprite 'other-sprite 'additional-sprite) So the user just requires whatever sprites he needs and then creates his engine. The use of atom is hopefully safe here. One disadvantage, that this works only with one combination of sprites in the user code. In case the user does want two sprite engines with different sprites, this approach won't work. In this case it's maybe better to provide the list of sprites to the engine constructor or a public add-sprite function is called by the user. user.clj------------------------(def sprite-engine (new-sprite-engine [(new-cursor-sprite) (new-other- sprite)]))
... or ... (def sprite-engine (-> (new-sprite-engine) (add-sprite (new-cursor-sprite)) (add-sprite (new-other-sprite)))) Hope this helps. Sincerely Meikel
smime.p7s
Description: S/MIME cryptographic signature