Skip to content

Commit 8401066

Browse files
committed
add Strategy pattern
1 parent 40a4a4c commit 8401066

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Strategy - Defina uma família de algoritmos, encapsule cada um e torne-os intercambiáveis. A estratégia permite que o algoritmo varie independentemente dos clientes que o utilizam.\n",
8+
"\n",
9+
"### Mais Informações:\n",
10+
"- https://sourcemaking.com/design_patterns/strategy\n",
11+
"- https://brizeno.wordpress.com/2011/08/31/strategy/"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import abc"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"# Context to use the strategy\n",
30+
"class Classifier:\n",
31+
" def __init__(self, strategy=None):\n",
32+
" self._strategy = strategy\n",
33+
"\n",
34+
" def set_strategy(self, strategy):\n",
35+
" self._strategy = strategy\n",
36+
" \n",
37+
" def apply(self):\n",
38+
" if self._strategy==None:\n",
39+
" print('There is no strategy!')\n",
40+
" else:\n",
41+
" self._strategy.algorithm()"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 3,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"# Strategy Interface - Common to all supported algorithms\n",
51+
"class Strategy(metaclass=abc.ABCMeta):\n",
52+
" @abc.abstractmethod\n",
53+
" def algorithm(self):\n",
54+
" pass"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"# Strategy 01 - MLP\n",
64+
"class MLP_Strategy(Strategy):\n",
65+
" def algorithm(self):\n",
66+
" print('> Running MLP Algorithm..')\n",
67+
"\n",
68+
"# Strategy 02 - SVM\n",
69+
"class SVM_Strategy(Strategy):\n",
70+
" def algorithm(self):\n",
71+
" print('> Running SVM Algorithm..')"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 5,
77+
"metadata": {},
78+
"outputs": [
79+
{
80+
"name": "stdout",
81+
"output_type": "stream",
82+
"text": [
83+
"> Running MLP Algorithm..\n",
84+
"> Running SVM Algorithm..\n",
85+
"There is no strategy!\n"
86+
]
87+
}
88+
],
89+
"source": [
90+
"# Instantiate Strategies\n",
91+
"strategy_mlp = MLP_Strategy()\n",
92+
"strategy_svm = SVM_Strategy()\n",
93+
"\n",
94+
"# Instantiate classifier context\n",
95+
"classifier = Classifier(strategy_mlp)\n",
96+
"classifier.apply()\n",
97+
"\n",
98+
"# Set other strategy\n",
99+
"classifier.set_strategy(strategy_svm)\n",
100+
"classifier.apply()\n",
101+
"\n",
102+
"# Remove current strategy\n",
103+
"classifier.set_strategy(None)\n",
104+
"classifier.apply()"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.5.2"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 2
129+
}

Strategy/Strategy.ipynb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Strategy - Defina uma família de algoritmos, encapsule cada um e torne-os intercambiáveis. A estratégia permite que o algoritmo varie independentemente dos clientes que o utilizam.\n",
8+
"\n",
9+
"### Mais Informações:\n",
10+
"- https://sourcemaking.com/design_patterns/strategy\n",
11+
"- https://brizeno.wordpress.com/2011/08/31/strategy/"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import abc"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"# Context to use the strategy\n",
30+
"class Classifier:\n",
31+
" def __init__(self, strategy=None):\n",
32+
" self._strategy = strategy\n",
33+
"\n",
34+
" def set_strategy(self, strategy):\n",
35+
" self._strategy = strategy\n",
36+
" \n",
37+
" def apply(self):\n",
38+
" if self._strategy==None:\n",
39+
" print('There is no strategy!')\n",
40+
" else:\n",
41+
" self._strategy.algorithm()"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 3,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"# Strategy Interface - Common to all supported algorithms\n",
51+
"class Strategy(metaclass=abc.ABCMeta):\n",
52+
" @abc.abstractmethod\n",
53+
" def algorithm(self):\n",
54+
" pass"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"# Strategy 01 - MLP\n",
64+
"class MLP_Strategy(Strategy):\n",
65+
" def algorithm(self):\n",
66+
" print('> Running MLP Algorithm..')\n",
67+
"\n",
68+
"# Strategy 02 - SVM\n",
69+
"class SVM_Strategy(Strategy):\n",
70+
" def algorithm(self):\n",
71+
" print('> Running SVM Algorithm..')"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 5,
77+
"metadata": {},
78+
"outputs": [
79+
{
80+
"name": "stdout",
81+
"output_type": "stream",
82+
"text": [
83+
"> Running MLP Algorithm..\n",
84+
"> Running SVM Algorithm..\n",
85+
"There is no strategy!\n"
86+
]
87+
}
88+
],
89+
"source": [
90+
"# Instantiate Strategies\n",
91+
"strategy_mlp = MLP_Strategy()\n",
92+
"strategy_svm = SVM_Strategy()\n",
93+
"\n",
94+
"# Instantiate classifier context\n",
95+
"classifier = Classifier(strategy_mlp)\n",
96+
"classifier.apply()\n",
97+
"\n",
98+
"# Set other strategy\n",
99+
"classifier.set_strategy(strategy_svm)\n",
100+
"classifier.apply()\n",
101+
"\n",
102+
"# Remove current strategy\n",
103+
"classifier.set_strategy(None)\n",
104+
"classifier.apply()"
105+
]
106+
}
107+
],
108+
"metadata": {
109+
"kernelspec": {
110+
"display_name": "Python 3",
111+
"language": "python",
112+
"name": "python3"
113+
},
114+
"language_info": {
115+
"codemirror_mode": {
116+
"name": "ipython",
117+
"version": 3
118+
},
119+
"file_extension": ".py",
120+
"mimetype": "text/x-python",
121+
"name": "python",
122+
"nbconvert_exporter": "python",
123+
"pygments_lexer": "ipython3",
124+
"version": "3.5.2"
125+
}
126+
},
127+
"nbformat": 4,
128+
"nbformat_minor": 2
129+
}

0 commit comments

Comments
 (0)