Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1752dcb

Browse files
author
Badacadabra
committed
Add all creational patterns in CoffeeScript
1 parent f1a89f3 commit 1752dcb

File tree

5 files changed

+416
-0
lines changed

5 files changed

+416
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT GNU/LINUX DISTRO
5+
# ==============================
6+
7+
class LinuxDistro
8+
constructor: (name) ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is LinuxDistro
10+
@name = name
11+
12+
bootLinux: ->
13+
"#{@name} is booting..."
14+
15+
# ==============================
16+
# ABSTRACT MAC OS RELEASE
17+
# ==============================
18+
19+
class MacRelease
20+
constructor: (name) ->
21+
throw new Error "You cannot instantiate an abstract class!" if @constructor is MacRelease
22+
@name = name
23+
24+
bootMac: ->
25+
"#{@name} is booting..."
26+
27+
# ==============================
28+
# ABSTRACT WINDOWS VERSION
29+
# ==============================
30+
31+
class WindowsVersion
32+
constructor: (name) ->
33+
throw new Error "You cannot instantiate an abstract class!" if @constructor is WindowsVersion
34+
@name = name
35+
36+
bootWindows: ->
37+
"#{@name} is booting..."
38+
39+
# ==============================
40+
# CONCRETE GNU/LINUX DISTROS
41+
# ==============================
42+
43+
class Debian extends LinuxDistro
44+
constructor: ->
45+
super "Debian"
46+
47+
class RedHat extends LinuxDistro
48+
constructor: ->
49+
super "RedHat"
50+
51+
# ==============================
52+
# CONCRETE MAC OS RELEASES
53+
# ==============================
54+
55+
class OS9 extends MacRelease
56+
constructor: ->
57+
super "Mac OS 9"
58+
59+
class OSX extends MacRelease
60+
constructor: ->
61+
super "Mac OS X"
62+
63+
# ==============================
64+
# CONCRETE WINDOWS VERSIONS
65+
# ==============================
66+
67+
class XP extends WindowsVersion
68+
constructor: ->
69+
super "Windows XP"
70+
71+
class Vista extends WindowsVersion
72+
constructor: ->
73+
super "Windows Vista"
74+
75+
# ==============================
76+
# ABSTRACT FACTORY OF OPERATING SYSTEMS
77+
# ==============================
78+
79+
class OSFactory
80+
@LINUX: 0
81+
@MAC: 1
82+
@WINDOWS: 2
83+
84+
constructor: ->
85+
throw new Error "You cannot instantiate an abstract class!" if @constructor is OSFactory
86+
87+
@getOSFactory: (id) ->
88+
switch id
89+
when @LINUX then new LinuxFactory
90+
when @MAC then new MacFactory
91+
when @WINDOWS then new WindowsFactory
92+
else throw new Error "The factory you are looking for has not been found."
93+
94+
getLinuxDistro: (id) ->
95+
throw new Error "You cannot call an abstract method!"
96+
97+
getMacRelease: (id) ->
98+
throw new Error "You cannot call an abstract method!"
99+
100+
getWindowsVersion: (id) ->
101+
throw new Error "You cannot call an abstract method!"
102+
103+
# ==============================
104+
# CONCRETE GNU/LINUX FACTORY
105+
# ==============================
106+
107+
class LinuxFactory extends OSFactory
108+
@DEBIAN: 0
109+
@REDHAT: 1
110+
111+
getLinuxDistro: (id) ->
112+
switch id
113+
when LinuxFactory.DEBIAN then new Debian
114+
when LinuxFactory.REDHAT then new RedHat
115+
else throw new Error "The Linux distribution you are looking for has not been found."
116+
117+
# ==============================
118+
# CONCRETE MAC OS FACTORY
119+
# ==============================
120+
121+
class MacFactory extends OSFactory
122+
@OS9: 0
123+
@OSX: 1
124+
125+
getMacRelease: (id) ->
126+
switch id
127+
when MacFactory.OS9 then new OS9
128+
when MacFactory.OSX then new OSX
129+
else throw new Error "The Mac release you are looking for has not been found."
130+
131+
# ==============================
132+
# CONCRETE WINDOWS FACTORY
133+
# ==============================
134+
135+
class WindowsFactory extends OSFactory
136+
@XP: 0
137+
@VISTA: 1
138+
139+
getWindowsVersion: (id) ->
140+
switch id
141+
when WindowsFactory.XP then new XP
142+
when WindowsFactory.VISTA then new Vista
143+
else throw new Error "The Windows version you are looking for has not been found."
144+
145+
# ==============================
146+
# CLIENT CODE
147+
# ==============================
148+
149+
# We can get concrete factories from the abstract factory
150+
linuxFactory = OSFactory.getOSFactory OSFactory.LINUX
151+
macFactory = OSFactory.getOSFactory OSFactory.MAC
152+
windowsFactory = OSFactory.getOSFactory OSFactory.WINDOWS
153+
154+
# Then we can get real objects from these concrete factories
155+
debian = linuxFactory.getLinuxDistro LinuxFactory.DEBIAN
156+
osx = macFactory.getMacRelease MacFactory.OSX
157+
xp = windowsFactory.getWindowsVersion WindowsFactory.XP
158+
159+
console.log debian.bootLinux() # Debian is booting...
160+
console.log osx.bootMac() # Mac OS X is booting...
161+
console.log xp.bootWindows() # Windows XP is booting...
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"use strict"
2+
3+
# ==============================
4+
# PC (PRODUCT)
5+
# ==============================
6+
7+
class PC
8+
constructor: ->
9+
@._motherboard = ""
10+
@._cpu = ""
11+
@._ram = ""
12+
@._ssd = ""
13+
@._nic = ""
14+
@._powerSupply = ""
15+
@._caseDesign = ""
16+
17+
setMotherboard: (part) ->
18+
@._motherboard = part
19+
20+
setCpu: (part) ->
21+
@._cpu = part
22+
23+
setRam: (part) ->
24+
@._ram = part
25+
26+
setSsd: (part) ->
27+
@._ssd = part
28+
29+
setNic: (part) ->
30+
@._nic = part
31+
32+
setPowerSupply: (part) ->
33+
@._powerSupply = part
34+
35+
setCaseDesign: (part) ->
36+
@._caseDesign = part
37+
38+
toString: (part) ->
39+
"Motherboard: #{@._motherboard}\n
40+
CPU: #{@._cpu}\n
41+
RAM: #{@._ram}\n
42+
SSD: #{@._ssd}\n
43+
NIC: #{@._nic}\n
44+
Power supply: #{@._powerSupply}\n
45+
Case design: #{@._caseDesign}"
46+
47+
# ==============================
48+
# ABSTRACT PC BUILDER
49+
# ==============================
50+
51+
class Workforce
52+
construct: ->
53+
throw new Error "You cannot instantiate an abstract class!"
54+
55+
assemblePC: ->
56+
throw new Error "You cannot call an abstract method!"
57+
58+
setMotherboard: (motherboard) ->
59+
throw new Error "You cannot call an abstract method!"
60+
61+
setCpu: (cpu) ->
62+
throw new Error "You cannot call an abstract method!"
63+
64+
setRam: (ram) ->
65+
throw new Error "You cannot call an abstract method!"
66+
67+
setSsd: (ssd) ->
68+
throw new Error "You cannot call an abstract method!"
69+
70+
setNic: (nic) ->
71+
throw new Error "You cannot call an abstract method!"
72+
73+
setPowerSupply: (powerSupply) ->
74+
throw new Error "You cannot call an abstract method!"
75+
76+
setCaseDesign: (caseDesign) ->
77+
throw new Error "You cannot call an abstract method!"
78+
79+
# ==============================
80+
# CONCRETE PC BUILDER
81+
# ==============================
82+
83+
class Geek extends Workforce
84+
constructor: ->
85+
@._pc = new PC
86+
87+
assemblePC: ->
88+
@._pc.toString()
89+
90+
setMotherboard: (motherboard) ->
91+
@._pc.setMotherboard motherboard
92+
93+
setCpu: (cpu) ->
94+
@._pc.setCpu cpu
95+
96+
setRam: (ram) ->
97+
@._pc.setRam ram
98+
99+
setSsd: (ssd) ->
100+
@._pc.setSsd ssd
101+
102+
setNic: (nic) ->
103+
@._pc.setNic nic
104+
105+
setPowerSupply: (powerSupply) ->
106+
@._pc.setPowerSupply powerSupply
107+
108+
setCaseDesign: (caseDesign) ->
109+
@._pc.setCaseDesign caseDesign
110+
111+
# ==============================
112+
# MANUFACTURER (DIRECTOR)
113+
# ==============================
114+
115+
class Manufacturer
116+
@manufacture = (builder) ->
117+
builder.setMotherboard "Asus Z170-A ATX LGA1151"
118+
builder.setCpu "Intel Core i7 6950X"
119+
builder.setRam "HyperX Fury 8 GB"
120+
builder.setSsd "SanDisk SSD PLUS 240 GB"
121+
builder.setNic "D-Link DGE-528T"
122+
builder.setPowerSupply "Corsair RM750x"
123+
builder.setCaseDesign "Cooler Master HAF X"
124+
builder.assemblePC()
125+
126+
# ==============================
127+
# CLIENT CODE
128+
# ==============================
129+
130+
geek = new Geek
131+
pc = Manufacturer.manufacture geek
132+
133+
console.log pc
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT GNU/LINUX DISTRO
5+
# ==============================
6+
7+
class LinuxDistro
8+
constructor: (name) ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is LinuxDistro
10+
@name = name
11+
12+
boot: ->
13+
"#{@name} is booting..."
14+
15+
# ==============================
16+
# CONCRETE GNU/LINUX DISTROS
17+
# ==============================
18+
19+
# Let's say that each instance of a Linux distro will have the name of its constructor...
20+
21+
class Debian extends LinuxDistro
22+
constructor: ->
23+
super "Debian"
24+
25+
class RedHat extends LinuxDistro
26+
constructor: ->
27+
super "RedHat"
28+
29+
class Slackware extends LinuxDistro
30+
constructor: ->
31+
super "Slackware"
32+
33+
# ==============================
34+
# FACTORY OF GNU/LINUX DISTROS
35+
# ==============================
36+
37+
class LinuxDistrosFactory
38+
@DEBIAN: 0
39+
@REDHAT: 1
40+
@SLACKWARE: 2
41+
@getLinuxDistro: (id) ->
42+
switch id
43+
when @DEBIAN then new Debian
44+
when @REDHAT then new RedHat
45+
when @SLACKWARE then new Slackware
46+
else throw new Error "The Linux distribution you are looking for has not been found"
47+
48+
# ==============================
49+
# CLIENT CODE
50+
# ==============================
51+
52+
# Creation of our objects through the factory
53+
debian = LinuxDistrosFactory.getLinuxDistro LinuxDistrosFactory.DEBIAN
54+
redhat = LinuxDistrosFactory.getLinuxDistro LinuxDistrosFactory.REDHAT
55+
slackware = LinuxDistrosFactory.getLinuxDistro LinuxDistrosFactory.SLACKWARE
56+
57+
console.log debian.boot(); # Debian is booting...
58+
console.log redhat.boot(); # RedHat is booting...
59+
console.log slackware.boot(); # Slackware is booting...
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict"
2+
3+
# ==============================
4+
# ABSTRACT PROTOTYPE
5+
# ==============================
6+
7+
class PaperSheet
8+
constructor: ->
9+
throw new Error "You cannot instantiate an abstract class!" if @constructor is PaperSheet
10+
11+
photocopy: ->
12+
throw new Error "You cannot call an abstract method!"
13+
14+
# ==============================
15+
# CONCRETE PROTOTYPE
16+
# ==============================
17+
18+
class Invoice extends PaperSheet
19+
constructor: (@price, @currency) ->
20+
21+
photocopy: ->
22+
clone = Object.create Object.getPrototypeOf @
23+
for own key, val of @
24+
clone[key] = val
25+
clone
26+
27+
# ==============================
28+
# CLIENT CODE
29+
# ==============================
30+
31+
invoice = new Invoice 42, ""
32+
copy = invoice.photocopy()
33+
34+
console.log copy.price # 42
35+
console.log copy.currency #
36+
console.log copy is invoice # false (the copy is not the original invoice: it is a new object!)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /