1-
1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "id" : " 7ed35767" ,
6+ "metadata" : {},
7+ "source" : [
8+ " # Design Patterns in Python\n " ,
9+ " This notebook provides an overview of key design patterns used in software development with practical Python examples.\n " ,
10+ " \n " ,
11+ " **Categories of Design Patterns:**\n " ,
12+ " - Creational Patterns: Singleton, Factory, Prototype\n " ,
13+ " - Structural Patterns: Adapter, Composite\n " ,
14+ " - Behavioral Patterns: Observer, Strategy, Command, Mediator\n "
15+ ]
16+ },
17+ {
18+ "cell_type" : " markdown" ,
19+ "id" : " 2c122035" ,
20+ "metadata" : {},
21+ "source" : [
22+ " ## Creational Patterns: Singleton\n " ,
23+ " **Purpose:** Ensures that a class has only one instance and provides a global point of access.\n " ,
24+ " \n " ,
25+ " **Example:** Logger instance"
26+ ]
27+ },
28+ {
29+ "cell_type" : " code" ,
30+ "execution_count" : null ,
31+ "id" : " b67af2f5" ,
32+ "metadata" : {},
33+ "outputs" : [],
34+ "source" : [
35+ " class Singleton:\n " ,
36+ " _instance = None\n " ,
37+ " \n " ,
38+ " def __new__(cls):\n " ,
39+ " if cls._instance is None:\n " ,
40+ " cls._instance = super().__new__(cls)\n " ,
41+ " return cls._instance\n " ,
42+ " \n " ,
43+ " # Test Singleton\n " ,
44+ " s1 = Singleton()\n " ,
45+ " s2 = Singleton()\n " ,
46+ " print(s1 is s2) # True"
47+ ]
48+ },
49+ {
50+ "cell_type" : " markdown" ,
51+ "id" : " 9af971f4" ,
52+ "metadata" : {},
53+ "source" : [
54+ " ## Creational Patterns: Factory\n " ,
55+ " **Purpose:** Creates objects without specifying the exact class to instantiate.\n " ,
56+ " \n " ,
57+ " **Example:** Shape Factory"
58+ ]
59+ },
60+ {
61+ "cell_type" : " code" ,
62+ "execution_count" : null ,
63+ "id" : " 70ee94f3" ,
64+ "metadata" : {},
65+ "outputs" : [],
66+ "source" : [
67+ " class Shape:\n " ,
68+ " def draw(self):\n " ,
69+ " pass\n " ,
70+ " \n " ,
71+ " class Circle(Shape):\n " ,
72+ " def draw(self):\n " ,
73+ " return 'Drawing Circle'\n " ,
74+ " \n " ,
75+ " class Square(Shape):\n " ,
76+ " def draw(self):\n " ,
77+ " return 'Drawing Square'\n " ,
78+ " \n " ,
79+ " class ShapeFactory:\n " ,
80+ " @staticmethod\n " ,
81+ " def get_shape(shape_type):\n " ,
82+ " if shape_type == 'circle':\n " ,
83+ " return Circle()\n " ,
84+ " elif shape_type == 'square':\n " ,
85+ " return Square()\n " ,
86+ " \n " ,
87+ " # Test Factory\n " ,
88+ " shape = ShapeFactory.get_shape('circle')\n " ,
89+ " print(shape.draw()) # Drawing Circle"
90+ ]
91+ },
92+ {
93+ "cell_type" : " markdown" ,
94+ "id" : " 35f88e3f" ,
95+ "metadata" : {},
96+ "source" : [
97+ " ## Structural Patterns: Adapter\n " ,
98+ " **Purpose:** Converts an interface of a class into another interface that clients expect.\n " ,
99+ " \n " ,
100+ " **Example:** Power Adapter"
101+ ]
102+ },
103+ {
104+ "cell_type" : " code" ,
105+ "execution_count" : null ,
106+ "id" : " 619542e0" ,
107+ "metadata" : {},
108+ "outputs" : [],
109+ "source" : [
110+ " class EuropeanSocket:\n " ,
111+ " def voltage(self):\n " ,
112+ " return '220V'\n " ,
113+ " \n " ,
114+ " class USASocket:\n " ,
115+ " def voltage(self):\n " ,
116+ " return '110V'\n " ,
117+ " \n " ,
118+ " class SocketAdapter:\n " ,
119+ " def __init__(self, socket):\n " ,
120+ " self.socket = socket\n " ,
121+ " \n " ,
122+ " def voltage(self):\n " ,
123+ " return self.socket.voltage()\n " ,
124+ " \n " ,
125+ " # Test Adapter\n " ,
126+ " european_socket = EuropeanSocket()\n " ,
127+ " adapter = SocketAdapter(european_socket)\n " ,
128+ " print(adapter.voltage()) # 220V"
129+ ]
130+ },
131+ {
132+ "cell_type" : " markdown" ,
133+ "id" : " 2bab5892" ,
134+ "metadata" : {},
135+ "source" : [
136+ " ## Behavioral Patterns: Observer\n " ,
137+ " **Purpose:** Defines a dependency between objects so that when one object changes state, all its dependents are notified.\n " ,
138+ " \n " ,
139+ " **Example:** News Subscriber System"
140+ ]
141+ },
142+ {
143+ "cell_type" : " code" ,
144+ "execution_count" : null ,
145+ "id" : " 5b42333f" ,
146+ "metadata" : {},
147+ "outputs" : [],
148+ "source" : [
149+ " class NewsPublisher:\n " ,
150+ " def __init__(self):\n " ,
151+ " self.subscribers = []\n " ,
152+ " \n " ,
153+ " def subscribe(self, subscriber):\n " ,
154+ " self.subscribers.append(subscriber)\n " ,
155+ " \n " ,
156+ " def notify(self, news):\n " ,
157+ " for subscriber in self.subscribers:\n " ,
158+ " subscriber.update(news)\n " ,
159+ " \n " ,
160+ " class Subscriber:\n " ,
161+ " def __init__(self, name):\n " ,
162+ " self.name = name\n " ,
163+ " \n " ,
164+ " def update(self, news):\n " ,
165+ " print(f'{self.name} received news: {news}')\n " ,
166+ " \n " ,
167+ " # Test Observer\n " ,
168+ " news_publisher = NewsPublisher()\n " ,
169+ " alice = Subscriber('Alice')\n " ,
170+ " bob = Subscriber('Bob')\n " ,
171+ " news_publisher.subscribe(alice)\n " ,
172+ " news_publisher.subscribe(bob)\n " ,
173+ " news_publisher.notify('New Python version released!')"
174+ ]
175+ },
176+ {
177+ "cell_type" : " markdown" ,
178+ "id" : " 3230eb27" ,
179+ "metadata" : {},
180+ "source" : [
181+ " ## Conclusion\n " ,
182+ " Design patterns help structure code in a reusable, scalable way. This notebook demonstrated examples of different patterns in Python."
183+ ]
184+ }
185+ ],
186+ "metadata" : {},
187+ "nbformat" : 4 ,
188+ "nbformat_minor" : 5
189+ }
0 commit comments