|
6 | 6 | - 단일체 Singleton
|
7 | 7 | - 프로토타입 Prototype
|
8 | 8 |
|
9 |
| -## 추상 팩토리 |
| 9 | +## 추상 팩토리Abstract Factory |
10 | 10 | 객체의 구체적인 유형을 모른 채 객체의 Kit를 생성하는 방법<br>
|
11 | 11 | ```javascript
|
12 | 12 |
|
@@ -62,3 +62,207 @@ courtSession.complaintPresented({ severity: 8 });
|
62 | 62 | courtSession.complaintPresented({ severity: 12 });
|
63 | 63 |
|
64 | 64 | ```
|
| 65 | + |
| 66 | +## 빌더Builder |
| 67 | + |
| 68 | +```javascript |
| 69 | +var Wasteros; |
| 70 | +(function(Wasteros) { |
| 71 | + var Event = (function() { |
| 72 | + function Event(name) { |
| 73 | + this.name = name; |
| 74 | + } |
| 75 | + return Event; |
| 76 | + })(); |
| 77 | + Wasteros.Event = Event; |
| 78 | + var Prize = (function() { |
| 79 | + function Prize(name) { |
| 80 | + this.name = name; |
| 81 | + }; |
| 82 | + return Prize; |
| 83 | + })(); |
| 84 | + Wasteros.Prize = Prize; |
| 85 | + var Attendee = (function() { |
| 86 | + function Attendee(name) { |
| 87 | + this.name = name; |
| 88 | + }; |
| 89 | + return Attendee; |
| 90 | + })(); |
| 91 | + Wasteros.Attendee = Attendee; |
| 92 | + var Tournament = (function() { |
| 93 | + function Tournament() { |
| 94 | + this.events = []; |
| 95 | + this.attendees = []; |
| 96 | + this.prizes = []; |
| 97 | + }; |
| 98 | + return Tournament; |
| 99 | + })(); |
| 100 | + Wasteros.Tournament = Tournament; |
| 101 | + // 빌더를 구현 |
| 102 | + var LannisterTournamentBuilder = (function() { |
| 103 | + function LannisterTournamentBuilder() { |
| 104 | + |
| 105 | + }; |
| 106 | + LannisterTournamentBuilder.prototype.build = function() { |
| 107 | + var tourunament = new Tournament(); |
| 108 | + tourunament.events.push(new Event('Joust')); |
| 109 | + tourunament.events.push(new Event('Melee')); |
| 110 | + |
| 111 | + tourunament.attendees.push(new Attendee('Jamie')); |
| 112 | + |
| 113 | + tourunament.prizes.push(new Prize('Gold')); |
| 114 | + |
| 115 | + return tourunament; |
| 116 | + } |
| 117 | + return LannisterTournamentBuilder; |
| 118 | + })(); |
| 119 | + Wasteros.LannisterTournamentBuilder = LannisterTournamentBuilder; |
| 120 | + |
| 121 | + // Director 구현 |
| 122 | + var TournamentBuilder = (function() { |
| 123 | + function TournamentBuilder() {} |
| 124 | + TournamentBuilder.prototype.build = function(builder) { |
| 125 | + return builder.build(); |
| 126 | + } |
| 127 | + return TournamentBuilder; |
| 128 | + })(); |
| 129 | + Wasteros.TournamentBuilder = TournamentBuilder; |
| 130 | +})(Wasteros || (Wasteros = {})); |
| 131 | +var builder = new Wasteros.TournamentBuilder(); |
| 132 | +var lannister = builder.build(new Wasteros.LannisterTournamentBuilder()); |
| 133 | +console.log(lannister); |
| 134 | +``` |
| 135 | + |
| 136 | +## 팩토리 메소드Factory Method |
| 137 | +전략패턴 같이 다수의 유사한 구현이 필요한 경우 유용<br> |
| 138 | +일반적으로 추상 팩토리 패턴과 함께 사용<br> |
| 139 | +```javascript |
| 140 | +var Wasteros; |
| 141 | +(function(Wasteros) { |
| 142 | + (function(Religion) { |
| 143 | + var WateryGod = (function() { |
| 144 | + function WateryGod() {}; |
| 145 | + WateryGod.prototype.prayTo = function() {}; |
| 146 | + return WateryGod; |
| 147 | + })(); |
| 148 | + Religion.WateryGod = WateryGod; |
| 149 | + |
| 150 | + var AncientGod = (function() { |
| 151 | + function AncientGod() {}; |
| 152 | + AncientGod.prototype.prayTo = function() {}; |
| 153 | + return AncientGod; |
| 154 | + })(); |
| 155 | + Religion.AncientGod = AncientGod; |
| 156 | + |
| 157 | + var DefaultGod = (function() { |
| 158 | + function DefaultGod() {}; |
| 159 | + DefaultGod.prototype.prayTo = function() {}; |
| 160 | + return DefaultGod; |
| 161 | + })(); |
| 162 | + Religion.DefaultGod = DefaultGod; |
| 163 | + |
| 164 | + var GodFactory = (function() { |
| 165 | + function GodFactory() {}; |
| 166 | + GodFactory.Build = function(godName) { |
| 167 | + if (godName === 'watery') |
| 168 | + return new WateryGod(); |
| 169 | + if (godName === 'ancient') |
| 170 | + return new AncientGod(); |
| 171 | + return new DefaultGod(); |
| 172 | + }; |
| 173 | + return GodFactory; |
| 174 | + })(); |
| 175 | + var GodDeterminant = (function() { |
| 176 | + function GodDeterminant(religionName, prayerPurpose) { |
| 177 | + this.religionName = religionName; |
| 178 | + this.prayerPurpose = prayerPurpose; |
| 179 | + }; |
| 180 | + return GodDeterminant; |
| 181 | + })(); |
| 182 | + var Prayer= (function() { |
| 183 | + function Prayer() {} |
| 184 | + Prayer.prototype.pray = function(godName) { |
| 185 | + GodFactory.Build(godName).prayTo(); |
| 186 | + } |
| 187 | + return Prayer; |
| 188 | + })(); |
| 189 | + Religion.Prayer = Prayer; |
| 190 | + })(Wasteros.Religion || (Wasteros.Religion = {})); |
| 191 | + // var Religion = Wasteros.Religion; ?? |
| 192 | +})(Wasteros || (Wasteros = {})); |
| 193 | + |
| 194 | +``` |
| 195 | + |
| 196 | +## 단일체Singleton |
| 197 | + |
| 198 | + |
| 199 | +```javascript |
| 200 | +var Wasteros; |
| 201 | + |
| 202 | +(function(Wasteros) { |
| 203 | + var Wall = (function() { |
| 204 | + function Wall() { |
| 205 | + this.height = 0; |
| 206 | + if (Wall._instance) |
| 207 | + return Wall._instance; |
| 208 | + Wall._instance = this; |
| 209 | + } |
| 210 | + |
| 211 | + Wall.prototype.setHeight = function(height) { |
| 212 | + this.height = height; |
| 213 | + }; |
| 214 | + Wall.prototype.getStatus = function(status) { |
| 215 | + console.log('Wall is ' + this.height + ' meters wall'); |
| 216 | + }; |
| 217 | + Wall.getInstance = function() { |
| 218 | + if (!Wall._instance) { |
| 219 | + Wall._instance = new Wall(); |
| 220 | + } |
| 221 | + return Wall._instance; |
| 222 | + }; |
| 223 | + Wall._instance = null; |
| 224 | + return Wall; |
| 225 | + })(); |
| 226 | + Wasteros.Wall = Wall; |
| 227 | +})(Wasteros || (Wasteros = {})); |
| 228 | + |
| 229 | +``` |
| 230 | + |
| 231 | +## 프로토타입Prototype |
| 232 | + |
| 233 | +```javascript |
| 234 | +var Wasteros; |
| 235 | + |
| 236 | +(function(Wasteros) { |
| 237 | + (function(Failies) { |
| 238 | + var Lannister = (function() { |
| 239 | + function Lannister() {} |
| 240 | + Lannister.prototype.clone = function() { |
| 241 | + var clone = new Lannister(); |
| 242 | + for (var attr in this) { |
| 243 | + clone[attr] = this[attr]; |
| 244 | + } |
| 245 | + return clone; |
| 246 | + } |
| 247 | + return Lannister; |
| 248 | + })(); |
| 249 | + Failies.Lannister = Lannister; |
| 250 | + })(Wasteros.Failies || (Wasteros.Failies = {})); |
| 251 | + var Failies = Westeros.Families; |
| 252 | +})(Wasteros || (Wasteros = {})); |
| 253 | + |
| 254 | +var jamie = new Wasteros.Failies.Lannister(); |
| 255 | +jamie.swordSkills = 9; |
| 256 | +jamie.charm = 6; |
| 257 | +jamie.wealth = 10; |
| 258 | +var tryion = jamie.clone(); |
| 259 | +tryion.charm = 10; |
| 260 | + |
| 261 | +``` |
| 262 | + |
| 263 | +## 요약 |
| 264 | +- 추상화 팩토리: 교체 가능한 Kit, 관련 객체들의 집합을 구축 |
| 265 | +- 빌더: 매개변수 문제에 대한 해결책 |
| 266 | +- 팩토리 메서드: 추상 팩토리를 보완하는 정적 팩토리 제공 |
| 267 | +- 단일체: 단 하나의 인스턴스 제공 |
| 268 | +- 프로토타입: 다른 객체를 기반으로 객체를 구축하는 패턴 |
0 commit comments