Skip to content

Commit

Permalink
Merge pull request faif#216 from spookylukey/pythonic_builder
Browse files Browse the repository at this point in the history
Made builder pattern much simpler by removing unnecessary class.
  • Loading branch information
faif authored Feb 6, 2018
2 parents 5e05ca1 + 1d201b3 commit e9a08d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
31 changes: 9 additions & 22 deletions creational/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from its actual representation (generally for abstraction).
*What does this example do?
This particular example uses a Director to abtract the
This particular example uses a director function to abtract the
construction of a building. The user specifies a Builder (House or
Flat) and the director specifies the methods in the order necessary
creating a different building dependding on the specified
creating a different building depending on the specified
specification (through the Builder class).
@author: Diogenes Augusto Fernandes Herminio <[email protected]>
Expand All @@ -29,19 +29,11 @@
"""


# Director
class Director(object):

def __init__(self):
self.builder = None

def construct_building(self):
self.builder.new_building()
self.builder.build_floor()
self.builder.build_size()

def get_building(self):
return self.builder.building
def construct_building(builder):
builder.new_building()
builder.build_floor()
builder.build_size()
return builder.building


# Abstract Builder
Expand Down Expand Up @@ -93,14 +85,9 @@ def __repr__(self):

# Client
if __name__ == "__main__":
director = Director()
director.builder = BuilderHouse()
director.construct_building()
building = director.get_building()
building = construct_building(BuilderHouse())
print(building)
director.builder = BuilderFlat()
director.construct_building()
building = director.get_building()
building = construct_building(BuilderFlat())
print(building)

### OUTPUT ###
Expand Down
12 changes: 3 additions & 9 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.builder import Director, BuilderHouse, BuilderFlat
from creational.builder import construct_building, BuilderHouse, BuilderFlat


class TestHouseBuilding(unittest.TestCase):

def setUp(self):
self.director = Director()
self.director.builder = BuilderHouse()
self.director.construct_building()
self.building = self.director.get_building()
self.building = construct_building(BuilderHouse())

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

def setUp(self):
self.director = Director()
self.director.builder = BuilderFlat()
self.director.construct_building()
self.building = self.director.get_building()
self.building = construct_building(BuilderFlat())

def test_house_size(self):
self.assertEqual(self.building.size, 'Small')
Expand Down

0 comments on commit e9a08d0

Please sign in to comment.