forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request faif#216 from spookylukey/pythonic_builder
Made builder pattern much simpler by removing unnecessary class.
- Loading branch information
Showing
2 changed files
with
12 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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 | ||
|
@@ -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 ### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters