In the last OO design in Haskell thread (and probably in every one preceeding it), it was suggested that having some examples might be a good idea.<br><br>Since most people with existing designs will have some familiarity with Design Patterns, and those are typical building blocks for OO designs, it occured to me that implementing some of them might be a useful excersize. If for nothing other than learning some more Haskell.
<br><br>Now, some of them are probably bad ideas for implementing in Haskell. There's a better, or more natural, way than what is suggested by the design pattern. Visitor is probably not a good pattern to follow, for example. On the other hand, others may still be useful, even in a functional language.
<br><br>So, I've been working on a Composite example. I've used existential types to have a generic proxy to the base type, rather than a simple algebraic type, since adding new leaves to the algebraic type means modifying the whole type, a violation of the Open-Closed principle (open for extension, closed for modification)
<br><br>The interface of the composite. Two methods, add and draw.<br><br>> class IComponent e where<br>> draw ::e -> String<br>> add :: (IComponent e') => e -> e' -> Component<br><br>
<br>A proxy type which can hold any kind of component, and provides the<br>'virtual' dispatch implementation. That is, it forwards to the add<br>or draw implementation of the instance it is proxying for.<br><br>> data Component =
<br>> forall e.(IComponent e) => Component e<br><br>> componentDraw :: Component -> String<br>> componentDraw (Component c) = draw c<br><br>> componentAdd :: (IComponent e) => Component -> e -> Component
<br>> componentAdd (Component e) a = Component (add e a)<br><br>> instance IComponent Component where<br>> draw = componentDraw<br>> add = componentAdd <br><br><br>The Single type, which is the leaf node in this composite, add is a
<br>no-op, except for wrapping the value in a Component. Since there<br>isn't an implicit down cast from the 'derived' Single to the 'base'<br>Component.<br><br>> data Leaf = <br>> Text String
<br>> deriving(Show, Eq)<br><br>> leafDraw :: Leaf -> String<br>> leafDraw (Text s) = show s<br><br>> leafAdd :: (IComponent e) => Leaf -> e -> Component <br>> leafAdd s _ = Component s<br>
<br>> instance IComponent Leaf where<br>> draw = leafDraw<br>> add = leafAdd<br><br><br><br>The Composite type, which holds a list of Components through the<br>composite proxy. I was tempted to make the list a state variable,
<br>so that add could modify rather than produce a new Many, but I<br>wanted to get the basics working.<br><br>> data Composite =<br>> Many [Component]<br><br>> compositeDraw :: Composite -> String<br>> compositeDraw (Many ) = "()"
<br>> compositeDraw (Many leaves) = "(" ++ (foldr1 (++) $ map draw leaves) ++ ")"<br><br>> compositeAdd :: (IComponent e) => Composite -> e -> Component <br>> compositeAdd (Many leaves) c =
<br>> Component $ Many ((Component c) : leaves)<br><br>> instance IComponent Composite where<br>> draw = compositeDraw<br>> add = compositeAdd<br>> <br><br><br>