-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.hs
More file actions
64 lines (41 loc) · 1.53 KB
/
Command.hs
File metadata and controls
64 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- | The Invoker and command interface along with concrete command impelemntation
module Command where
import Product
import qualified Data.Map as M
import Shipping
import Syntax
--
-- * interface for Command
--
-- | interface
class Command cmd where
getProduct :: cmd -> Product -> String
placeOrder' :: Shipping' m => cmd -> m -> [(Product,Int)] -> Maybe ([(Product,Int,Int)],Int,Int)
--
-- ** concrete command as instances
--
data Cmd1 = Cmd1
-- | concrete command1 : give products' (name + price + description)
instance Command Cmd1 where
getProduct Cmd1 p = concat [ productName p, " | ", "$" ++ show (currentPrice p), " | ", productDescription p]
data Cmd2 = Cmd2
-- | concrete command2 : give products' ( id + name + price + description)
instance Command Cmd2 where
getProduct Cmd2 p = concat [show (productId p), " | " , productName p, " | ", "$" ++ show (currentPrice p), " | ", productDescription p]
data Cmd3 = Cmd3
-- | concrete command2 : give products' ( id + name + price)
instance Command Cmd3 where
getProduct Cmd3 p = concat [show (productId p), " | " , productName p, " | ", "$" ++ show (currentPrice p)]
--
-- ** Invoker Class
--
-- |interface
class Command cmd => Invoker cmd where
execute :: cmd -> Product -> String
executeOrder :: Shipping' m => cmd -> m -> [(Product,Int)]-> String
instance Invoker Cmd1 where
execute = getProduct
instance Invoker Cmd2 where
execute = getProduct
instance Invoker Cmd3 where
execute = getProduct