Skip to content

Commit c4d349d

Browse files
Update strategy to mark the behaviour as private
1 parent dd351d5 commit c4d349d

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,23 @@ From this repository :
7171

7272
```python
7373
class Duck():
74-
fly_behavior = None
75-
quack_behavior = None
74+
_fly_behavior = None
75+
_quack_behavior = None
7676

7777
def set_fly_behavior(self, fly_behavior):
78-
self.fly_behavior = fly_behavior
78+
self._fly_behavior = fly_behavior
7979

8080
def set_quack_behavior(self, quack_behavior):
81-
self.quack_behavior = quack_behavior
81+
self._quack_behavior = quack_behavior
8282

83-
def display():
83+
def display(self):
8484
raise NotImplementedError
8585

8686
def perform_fly(self):
87-
self.fly_behavior.fly()
87+
self._fly_behavior.fly()
8888

8989
def perform_quack(self):
90-
self.quack_behavior.quack()
90+
self._quack_behavior.quack()
9191

9292
def swim(self):
9393
print("All ducks float, even decoys! 〰🦆〰")

chapter01_strategy/duck.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,39 @@ def quack(self):
3939

4040

4141
class Duck:
42-
fly_behavior = None
43-
quack_behavior = None
42+
_fly_behavior = None
43+
_quack_behavior = None
4444

4545
def set_fly_behavior(self, fly_behavior):
46-
self.fly_behavior = fly_behavior
46+
self._fly_behavior = fly_behavior
4747

4848
def set_quack_behavior(self, quack_behavior):
49-
self.quack_behavior = quack_behavior
49+
self._quack_behavior = quack_behavior
5050

51-
def display():
51+
def display(self):
5252
raise NotImplementedError
5353

5454
def perform_fly(self):
55-
self.fly_behavior.fly()
55+
self._fly_behavior.fly()
5656

5757
def perform_quack(self):
58-
self.quack_behavior.quack()
58+
self._quack_behavior.quack()
5959

60-
def swim():
60+
def swim(self):
6161
print("All ducks float, even decoys!")
6262

6363

6464
class MallardDuck(Duck):
65-
fly_behavior = FlyWithWings()
66-
quack_behavior = Quack()
65+
_fly_behavior = FlyWithWings()
66+
_quack_behavior = Quack()
6767

6868
def display(self):
6969
print("I'm a real Mallard duck")
7070

7171

7272
class ModelDuck(Duck):
73-
fly_behavior = FlyNoWay()
74-
quack_behavior = Squeak()
73+
_fly_behavior = FlyNoWay()
74+
_quack_behavior = Squeak()
7575

7676
def display(self):
7777
print("I'm a real Mallard duck")

chapter01_strategy/readme.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Chapter 1: Strategy design pattern
22

3-
> **Strategy**: defines a family of algorithms, encapsulates each one and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
3+
> **Strategy**: defines a family of algorithms, encapsulates each one and makes them interchangeable.
4+
> Strategy lets the algorithm vary independently of clients that use it.
45
56
### Class Diagram
67

@@ -28,6 +29,13 @@ Strategy "1" <|.. "*" ConcreteStrategyB
2829

2930
## Use in Python
3031

31-
The more pythonic approach in my mind is to define methods rather than classes; this does not violate the above definition. The class-based approach is needed in Java and has the advantage of explicitly setting out the required method in an abstract base class.
32+
The more pythonic approach in my mind is to define methods rather than classes;
33+
this does not violate the above definition. The class-based approach is
34+
needed in Java and has the advantage of explicitly setting out the required
35+
method in an abstract base class.
3236

33-
An example from the standard lib is the list `sort()` and `sorted()` methods with the use of an optional `key` parameter. They invite the use of a sorting function or sorting "strategy", as do `min()` and `max()`.
37+
An example from the standard lib is the list `sort()` and `sorted()` methods
38+
with the use of an optional `key` parameter. They invite the use of a sorting
39+
function or sorting "strategy", as do `min()` and `max()`, although this might
40+
be better seen as a template method as the `key` function is just one part of
41+
the sorting or min/max algorithm.

0 commit comments

Comments
 (0)