- Create a
Product
type class. - Create an existential type
SomeProduct
for theProduct
type class. - Create a
Factory
type class with acreateProduct
method.
class Product p where
data SomeProduct where
SomeProduct :: Product p => p -> SomeProduct
class Factory f where
createProduct :: f -> SomeProduct
- Create multiple
Product
type classes, e.g.ProductA
andProductB
. - Create an existential type for each type class, e.g.
SomeProductA
andSomeProductB
. - Create a
Factory
type class with one factory method for each product.
class ProductA p where
class ProductB p where
data SomeProductA where
SomeProductA :: ProductA p => p -> SomeProductA
data SomeProductB where
SomeProductB :: ProductB p => p -> SomeProductB
class Factory f where
createProductA :: f -> SomeProductA
createProductB :: f -> SomeProductB
- Create a
Builder
type class with methods for build steps, which return new builders. - Create a
Director
type class with recipes created by composingBuilder
methods.
class Builder b where
buildStart :: b
buildStepA :: b -> b
buildStepB :: b -> b
buildStepZ :: b -> b
class Director d where
makeFoo :: Builder b => d -> b -> b
makeBar :: Builder b => d -> b -> b
- Create a
Prototype
type class with aclone
method. - Create an existential type
SomePrototype
for thePrototype
type class. - Create a
Registry
type class with methods to get and set prototypes.
class Prototype p where
clone :: p -> IO p
data SomePrototype where
SomePrototype :: Prototype p => p -> SomePrototype
class Registry r where
setPrototype :: Prototype p => r -> String -> p -> IO ()
getPrototype :: r -> String -> IO (Maybe SomePrototype)
Create a Singleton
type class with a getInstance
method.
class Singleton s where
getInstance :: IO s