Skip to content

Commit 1d201b3

Browse files
committed
Made builder pattern much simpler by removing unnecessary classes.
This follows the motto "a class with only one method that actually does anything should be a function", with the result that using the director function requires one line of code instead of 5.
1 parent 5e05ca1 commit 1d201b3

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

creational/builder.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from its actual representation (generally for abstraction).
1111
1212
*What does this example do?
13-
This particular example uses a Director to abtract the
13+
This particular example uses a director function to abtract the
1414
construction of a building. The user specifies a Builder (House or
1515
Flat) and the director specifies the methods in the order necessary
16-
creating a different building dependding on the specified
16+
creating a different building depending on the specified
1717
specification (through the Builder class).
1818
1919
@author: Diogenes Augusto Fernandes Herminio <[email protected]>
@@ -29,19 +29,11 @@
2929
"""
3030

3131

32-
# Director
33-
class Director(object):
34-
35-
def __init__(self):
36-
self.builder = None
37-
38-
def construct_building(self):
39-
self.builder.new_building()
40-
self.builder.build_floor()
41-
self.builder.build_size()
42-
43-
def get_building(self):
44-
return self.builder.building
32+
def construct_building(builder):
33+
builder.new_building()
34+
builder.build_floor()
35+
builder.build_size()
36+
return builder.building
4537

4638

4739
# Abstract Builder
@@ -93,14 +85,9 @@ def __repr__(self):
9385

9486
# Client
9587
if __name__ == "__main__":
96-
director = Director()
97-
director.builder = BuilderHouse()
98-
director.construct_building()
99-
building = director.get_building()
88+
building = construct_building(BuilderHouse())
10089
print(building)
101-
director.builder = BuilderFlat()
102-
director.construct_building()
103-
building = director.get_building()
90+
building = construct_building(BuilderFlat())
10491
print(building)
10592

10693
### OUTPUT ###

tests/test_builder.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
import unittest
4-
from creational.builder import Director, BuilderHouse, BuilderFlat
4+
from creational.builder import construct_building, BuilderHouse, BuilderFlat
55

66

77
class TestHouseBuilding(unittest.TestCase):
88

99
def setUp(self):
10-
self.director = Director()
11-
self.director.builder = BuilderHouse()
12-
self.director.construct_building()
13-
self.building = self.director.get_building()
10+
self.building = construct_building(BuilderHouse())
1411

1512
def test_house_size(self):
1613
self.assertEqual(self.building.size, 'Big')
@@ -22,10 +19,7 @@ def test_num_floor_in_house(self):
2219
class TestFlatBuilding(unittest.TestCase):
2320

2421
def setUp(self):
25-
self.director = Director()
26-
self.director.builder = BuilderFlat()
27-
self.director.construct_building()
28-
self.building = self.director.get_building()
22+
self.building = construct_building(BuilderFlat())
2923

3024
def test_house_size(self):
3125
self.assertEqual(self.building.size, 'Small')

0 commit comments

Comments
 (0)