Skip to content

Commit d6d3c18

Browse files
committed
update Functions-DecoratorPattern
1 parent 9f54bd8 commit d6d3c18

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

notebooks/Functions-DecoratorPattern.ipynb

+111-1
Original file line numberDiff line numberDiff line change
@@ -1922,14 +1922,124 @@
19221922
"test2(1, 9, 2)"
19231923
]
19241924
},
1925+
{
1926+
"cell_type": "markdown",
1927+
"id": "21db9ece",
1928+
"metadata": {},
1929+
"source": [
1930+
"### Defining class decorator\n",
1931+
"\n",
1932+
"- in the following example; we have an original class Coffee\n",
1933+
"- MilkDecorator decorates Coffee by adding cost of milk\n",
1934+
"- similarly, Coffee can be wrapped with more decorators (e.g., SugarDecorator, WhippedCreamDecorator, etc.)\n",
1935+
"- this decorator pattern utilizes the **Open-Closed Principle (OCP) of SOLID** principles\n",
1936+
" - making it easy to extend functionality without modifying the original class"
1937+
]
1938+
},
1939+
{
1940+
"cell_type": "code",
1941+
"execution_count": 1,
1942+
"id": "8f9d7fed",
1943+
"metadata": {},
1944+
"outputs": [],
1945+
"source": [
1946+
"class Coffee:\n",
1947+
" def cost(self):\n",
1948+
" return 5\n",
1949+
" \n",
1950+
"\n",
1951+
"#Decorator class\n",
1952+
"class MilkDecorator:\n",
1953+
" def __init__(self, coffee):\n",
1954+
" self._coffee = coffee\n",
1955+
" \n",
1956+
" def cost(self):\n",
1957+
" return self._coffee.cost() + 2 # Add cost of mile"
1958+
]
1959+
},
1960+
{
1961+
"cell_type": "code",
1962+
"execution_count": 4,
1963+
"id": "04756de0",
1964+
"metadata": {},
1965+
"outputs": [
1966+
{
1967+
"name": "stdout",
1968+
"output_type": "stream",
1969+
"text": [
1970+
"Black Coffee Cost: 5\n"
1971+
]
1972+
}
1973+
],
1974+
"source": [
1975+
"# without using the decorator\n",
1976+
"black_coffee = Coffee()\n",
1977+
"print(\"Black Coffee Cost:\", black_coffee.cost())"
1978+
]
1979+
},
1980+
{
1981+
"cell_type": "code",
1982+
"execution_count": 7,
1983+
"id": "f39a53d3",
1984+
"metadata": {},
1985+
"outputs": [
1986+
{
1987+
"name": "stdout",
1988+
"output_type": "stream",
1989+
"text": [
1990+
"Milk Coffee Cost: 7\n"
1991+
]
1992+
}
1993+
],
1994+
"source": [
1995+
"# using the decorator\n",
1996+
"milk_coffee = MilkDecorator(black_coffee)\n",
1997+
"print(\"Milk Coffee Cost:\", milk_coffee.cost())"
1998+
]
1999+
},
2000+
{
2001+
"cell_type": "code",
2002+
"execution_count": 8,
2003+
"id": "b960c48d",
2004+
"metadata": {},
2005+
"outputs": [],
2006+
"source": [
2007+
"class SugarDecorator:\n",
2008+
" def __init__(self, coffee):\n",
2009+
" self._coffee = coffee\n",
2010+
"\n",
2011+
" def cost(self):\n",
2012+
" return self._coffee.cost() + 1 # Add cost of sugar"
2013+
]
2014+
},
2015+
{
2016+
"cell_type": "code",
2017+
"execution_count": 9,
2018+
"id": "07ec20c5",
2019+
"metadata": {},
2020+
"outputs": [
2021+
{
2022+
"name": "stdout",
2023+
"output_type": "stream",
2024+
"text": [
2025+
"Coffee with Milk and Sugar Cost: 8\n"
2026+
]
2027+
}
2028+
],
2029+
"source": [
2030+
"# Applying multiple decorators\n",
2031+
"coffee_with_milk_and_sugar = SugarDecorator(MilkDecorator(Coffee()))\n",
2032+
"print(\"Coffee with Milk and Sugar Cost:\", coffee_with_milk_and_sugar.cost())"
2033+
]
2034+
},
19252035
{
19262036
"cell_type": "code",
19272037
"execution_count": 29,
19282038
"id": "063ef55e",
19292039
"metadata": {},
19302040
"outputs": [],
19312041
"source": [
1932-
"# class decorators\n",
2042+
"# Useful class decorators example\n",
19332043
"# Generates demo.log when applied to a function\n",
19342044
"# __call__ overrides when the class is called like a function\n",
19352045
"\n",

0 commit comments

Comments
 (0)