Skip to content

Commit 3a15621

Browse files
committed
Adds facade & factory patterns
1 parent 4a101fe commit 3a15621

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ I just want to build software like a pro.
1313
* [Null Object Pattern](#null-object-pattern)
1414
* [Factory](#factory)
1515
* [Facade](#facade)
16-
* [Fat Models, Skinny Controllers](#fat-models-skinny-controllers)
1716
* [CodeRetreat](#coderetreat)
1817

1918
--
@@ -223,7 +222,87 @@ In object-oriented computer programming, a Null Object is an object with defined
223222
- [https://www.youtube.com/watch?v=9lv2lBq6x4A](https://www.youtube.com/watch?v=9lv2lBq6x4A)
224223

225224
### Factory
226-
Coming soon...
225+
226+
In class-based programming, the factory method pattern is a creational pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created. This is done by creating objects via calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
227+
228+
#### Example
229+
230+
```ruby
231+
class VehicleFactory
232+
233+
def self.factory(klass)
234+
{ :car => Car,
235+
:boat => Boat
236+
}[klass].new
237+
end
238+
end
239+
240+
class Car
241+
end
242+
243+
class Boat
244+
end
245+
246+
VehicleFactory.factory :car
247+
```
248+
249+
#### Facade
250+
251+
The facade pattern (or façade pattern) is a software design pattern commonly used with object-oriented programming. The name is by analogy to an architectural facade.
252+
253+
A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:
254+
255+
- make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
256+
- make the library more readable, for the same reason;
257+
- reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
258+
- wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).
259+
260+
#### Problem
261+
262+
```ruby
263+
class UserDashboardController < ApplicationController
264+
265+
def index
266+
@user = User.find(params[:user_id])
267+
@notifications = @user.notifications
268+
@messages = @user.messages
269+
@posts = @user.posts
270+
end
271+
end
272+
```
273+
274+
#### Solution
275+
276+
```ruby
277+
class UserDashboardController < ApplicationController
278+
279+
def index
280+
user = User.find(params[:user_id])
281+
@user_dashboard = UserDashboard.new(user)
282+
end
283+
end
284+
285+
class UserDashboard
286+
287+
attr_reader :user
288+
289+
def initialize(user = nil)
290+
@user = user
291+
end
292+
293+
def notifications
294+
@user.notifications
295+
end
296+
297+
def messages
298+
@user.messages
299+
end
300+
301+
def posts
302+
@user.posts
303+
end
304+
end
305+
```
227306

228307
### CodeRetreat
229308
Coming soon...

0 commit comments

Comments
 (0)