| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,7 @@ def main(): | |||
| 62 | 62 | # eggs | |
| 63 | 63 | # milk | |
| 64 | 64 | # cheese | |
| 65 | - # | ||
| 65 | + # | ||
| 66 | 66 | # PRODUCT INFORMATION: | |
| 67 | 67 | # Name: Cheese, Price: 2.00, Quantity: 10 | |
| 68 | 68 | # PRODUCT INFORMATION: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | 10 | ||
| 11 | 11 | class PetShop: | |
| 12 | + | ||
| 12 | 13 | """A pet shop""" | |
| 13 | 14 | ||
| 14 | 15 | def __init__(self, animal_factory=None): | |
@@ -30,6 +31,7 @@ def show_pet(self): | |||
| 30 | 31 | # Stuff that our factory makes | |
| 31 | 32 | ||
| 32 | 33 | class Dog: | |
| 34 | + | ||
| 33 | 35 | def speak(self): | |
| 34 | 36 | return "woof" | |
| 35 | 37 | ||
@@ -38,6 +40,7 @@ def __str__(self): | |||
| 38 | 40 | ||
| 39 | 41 | ||
| 40 | 42 | class Cat: | |
| 43 | + | ||
| 41 | 44 | def speak(self): | |
| 42 | 45 | return "meow" | |
| 43 | 46 | ||
@@ -48,6 +51,7 @@ def __str__(self): | |||
| 48 | 51 | # Factory classes | |
| 49 | 52 | ||
| 50 | 53 | class DogFactory: | |
| 54 | + | ||
| 51 | 55 | def get_pet(self): | |
| 52 | 56 | return Dog() | |
| 53 | 57 | ||
@@ -56,6 +60,7 @@ def get_food(self): | |||
| 56 | 60 | ||
| 57 | 61 | ||
| 58 | 62 | class CatFactory: | |
| 63 | + | ||
| 59 | 64 | def get_pet(self): | |
| 60 | 65 | return Cat() | |
| 61 | 66 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | 9 | class Dog(object): | |
| 10 | + | ||
| 10 | 11 | def __init__(self): | |
| 11 | 12 | self.name = "Dog" | |
| 12 | 13 | ||
@@ -15,6 +16,7 @@ def bark(self): | |||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | 18 | class Cat(object): | |
| 19 | + | ||
| 18 | 20 | def __init__(self): | |
| 19 | 21 | self.name = "Cat" | |
| 20 | 22 | ||
@@ -23,6 +25,7 @@ def meow(self): | |||
| 23 | 25 | ||
| 24 | 26 | ||
| 25 | 27 | class Human(object): | |
| 28 | + | ||
| 26 | 29 | def __init__(self): | |
| 27 | 30 | self.name = "Human" | |
| 28 | 31 | ||
@@ -31,6 +34,7 @@ def speak(self): | |||
| 31 | 34 | ||
| 32 | 35 | ||
| 33 | 36 | class Car(object): | |
| 37 | + | ||
| 34 | 38 | def __init__(self): | |
| 35 | 39 | self.name = "Car" | |
| 36 | 40 | ||
@@ -39,6 +43,7 @@ def make_noise(self, octane_level): | |||
| 39 | 43 | ||
| 40 | 44 | ||
| 41 | 45 | class Adapter(object): | |
| 46 | + | ||
| 42 | 47 | """ | |
| 43 | 48 | Adapts an object by replacing methods. | |
| 44 | 49 | Usage: | |
@@ -63,6 +68,7 @@ class Adapter(object): | |||
| 63 | 68 | A Human goes 'hello' | |
| 64 | 69 | A Car goes vroom!!! | |
| 65 | 70 | """ | |
| 71 | + | ||
| 66 | 72 | def __init__(self, obj, adapted_methods): | |
| 67 | 73 | """We set the adapted methods in the object's dict""" | |
| 68 | 74 | self.obj = obj | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,18 +6,21 @@ | |||
| 6 | 6 | ||
| 7 | 7 | # ConcreteImplementor 1/2 | |
| 8 | 8 | class DrawingAPI1(object): | |
| 9 | + | ||
| 9 | 10 | def draw_circle(self, x, y, radius): | |
| 10 | 11 | print('API1.circle at {}:{} radius {}'.format(x, y, radius)) | |
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | 14 | # ConcreteImplementor 2/2 | |
| 14 | 15 | class DrawingAPI2(object): | |
| 16 | + | ||
| 15 | 17 | def draw_circle(self, x, y, radius): | |
| 16 | 18 | print('API2.circle at {}:{} radius {}'.format(x, y, radius)) | |
| 17 | 19 | ||
| 18 | 20 | ||
| 19 | 21 | # Refined Abstraction | |
| 20 | 22 | class CircleShape(object): | |
| 23 | + | ||
| 21 | 24 | def __init__(self, x, y, radius, drawing_api): | |
| 22 | 25 | self._x = x | |
| 23 | 26 | self._y = y | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | 10 | # Director | |
| 11 | 11 | class Director(object): | |
| 12 | + | ||
| 12 | 13 | def __init__(self): | |
| 13 | 14 | self.builder = None | |
| 14 | 15 | ||
@@ -23,6 +24,7 @@ def get_building(self): | |||
| 23 | 24 | ||
| 24 | 25 | # Abstract Builder | |
| 25 | 26 | class Builder(object): | |
| 27 | + | ||
| 26 | 28 | def __init__(self): | |
| 27 | 29 | self.building = None | |
| 28 | 30 | ||
@@ -32,6 +34,7 @@ def new_building(self): | |||
| 32 | 34 | ||
| 33 | 35 | # Concrete Builder | |
| 34 | 36 | class BuilderHouse(Builder): | |
| 37 | + | ||
| 35 | 38 | def build_floor(self): | |
| 36 | 39 | self.building.floor = 'One' | |
| 37 | 40 | ||
@@ -40,15 +43,17 @@ def build_size(self): | |||
| 40 | 43 | ||
| 41 | 44 | ||
| 42 | 45 | class BuilderFlat(Builder): | |
| 46 | + | ||
| 43 | 47 | def build_floor(self): | |
| 44 | 48 | self.building.floor = 'More than One' | |
| 45 | - | ||
| 49 | + | ||
| 46 | 50 | def build_size(self): | |
| 47 | 51 | self.building.size = 'Small' | |
| 48 | 52 | ||
| 49 | 53 | ||
| 50 | 54 | # Product | |
| 51 | 55 | class Building(object): | |
| 56 | + | ||
| 52 | 57 | def __init__(self): | |
| 53 | 58 | self.floor = None | |
| 54 | 59 | self.size = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | 12 | class Catalog(): | |
| 13 | + | ||
| 13 | 14 | """ | |
| 14 | 15 | catalog of multiple static methods that are executed depending on an init | |
| 15 | 16 | parameter | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,11 +5,13 @@ | |||
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | 7 | class Handler: | |
| 8 | + | ||
| 8 | 9 | def successor(self, successor): | |
| 9 | 10 | self.successor = successor | |
| 10 | 11 | ||
| 11 | 12 | ||
| 12 | 13 | class ConcreteHandler1(Handler): | |
| 14 | + | ||
| 13 | 15 | def handle(self, request): | |
| 14 | 16 | if 0 < request <= 10: | |
| 15 | 17 | print('request {} handled in handler 1'.format(request)) | |
@@ -18,6 +20,7 @@ def handle(self, request): | |||
| 18 | 20 | ||
| 19 | 21 | ||
| 20 | 22 | class ConcreteHandler2(Handler): | |
| 23 | + | ||
| 21 | 24 | def handle(self, request): | |
| 22 | 25 | if 10 < request <= 20: | |
| 23 | 26 | print('request {} handled in handler 2'.format(request)) | |
@@ -26,6 +29,7 @@ def handle(self, request): | |||
| 26 | 29 | ||
| 27 | 30 | ||
| 28 | 31 | class ConcreteHandler3(Handler): | |
| 32 | + | ||
| 29 | 33 | def handle(self, request): | |
| 30 | 34 | if 20 < request <= 30: | |
| 31 | 35 | print('request {} handled in handler 3'.format(request)) | |
@@ -34,6 +38,7 @@ def handle(self, request): | |||
| 34 | 38 | ||
| 35 | 39 | ||
| 36 | 40 | class Client: | |
| 41 | + | ||
| 37 | 42 | def __init__(self): | |
| 38 | 43 | h1 = ConcreteHandler1() | |
| 39 | 44 | h2 = ConcreteHandler2() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | 7 | class MoveFileCommand(object): | |
| 8 | + | ||
| 8 | 9 | def __init__(self, src, dest): | |
| 9 | 10 | self.src = src | |
| 10 | 11 | self.dest = dest | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ def denormalize(val): | |||
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | 39 | class SpecialDict(dict): | |
| 40 | + | ||
| 40 | 41 | """ A dictionary type which allows direct attribute | |
| 41 | 42 | access to its keys """ | |
| 42 | 43 | ||
@@ -71,6 +72,7 @@ def __setattr__(self, name, value): | |||
| 71 | 72 | ||
| 72 | 73 | ||
| 73 | 74 | class CompositeDict(SpecialDict): | |
| 75 | + | ||
| 74 | 76 | """ A class which works like a hierarchical dictionary. | |
| 75 | 77 | This class is based on the Composite design-pattern """ | |
| 76 | 78 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | 7 | class foo_decorator(object): | |
| 8 | + | ||
| 8 | 9 | def __init__(self, decoratee): | |
| 9 | 10 | self._decoratee = decoratee | |
| 10 | 11 | ||
@@ -17,6 +18,7 @@ def __getattr__(self, name): | |||
| 17 | 18 | ||
| 18 | 19 | ||
| 19 | 20 | class undecorated_foo(object): | |
| 21 | + | ||
| 20 | 22 | def f1(self): | |
| 21 | 23 | print("original f1") | |
| 22 | 24 | ||
@@ -26,6 +28,7 @@ def f2(self): | |||
| 26 | 28 | ||
| 27 | 29 | @foo_decorator | |
| 28 | 30 | class decorated_foo(object): | |
| 31 | + | ||
| 29 | 32 | def f1(self): | |
| 30 | 33 | print("original f1") | |
| 31 | 34 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments