Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d5de26

Browse files
add ABC to abtract class
1 parent f8b46cb commit 4d5de26

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

‎factory.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Define a Pizza interface
2-
from abc import abstractmethod
2+
from abc import ABC, abstractmethod
33

44

5-
class Pizza:
5+
class Pizza(ABC):
66
@abstractmethod
77
def prepare(self):
88
return "Concrete implementation required"
99

10-
1110
@abstractmethod
1211
def bake(self):
1312
pass
@@ -20,6 +19,7 @@ def cut(self):
2019
def box(self):
2120
pass
2221

22+
2323
# Concrete Pizza classes
2424
class MargheritaPizza(Pizza):
2525

@@ -35,6 +35,7 @@ def cut(self):
3535
def box(self):
3636
print("Boxing Margherita Pizza")
3737

38+
3839
class PepperoniPizza(Pizza):
3940
def prepare(self):
4041
print("Preparing Pepperoni Pizza")
@@ -48,18 +49,22 @@ def cut(self):
4849
def box(self):
4950
print("Boxing Pepperoni Pizza")
5051

52+
5153
# Pizza Factory
5254
class PizzaFactory:
5355
"""It creates and returns an instance of the appropriate subclass based
5456
on the pizza_type parameter."""
55-
def create_pizza(self, pizza_type):
57+
58+
@staticmethod
59+
def create_pizza(pizza_type):
5660
if pizza_type == 'Margherita':
5761
return MargheritaPizza()
5862
elif pizza_type == 'Pepperoni':
5963
return PepperoniPizza()
6064
else:
6165
raise ValueError("Invalid pizza type")
6266

67+
6368
# Client code
6469
if __name__ == "__main__":
6570
# Creating a Margherita Pizza using the factory

‎tests/test_factory.py‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ def test_create_margherita_pizza(self):
1111
pizza_factory = PizzaFactory()
1212
pizza = pizza_factory.create_pizza("Margherita")
1313
self.assertIsInstance(pizza, MargheritaPizza)
14-
# Add more specific assertions related to MargheritaPizza creation if needed
14+
# Add more specific assertions related to MargheritaPizza creation
15+
# if needed
1516

1617
def test_create_pepperoni_pizza(self):
1718
pizza_factory = PizzaFactory()
1819
pizza = pizza_factory.create_pizza("Pepperoni")
1920
self.assertIsInstance(pizza, PepperoniPizza)
20-
# Add more specific assertions related to PepperoniPizza creation if needed
21-
22-
23-
def test_abstract_class_instantiation(self):
24-
with self.assertRaises(Exception) ascontext:
25-
abstract_instance=Pizza()
26-
self.assertEqual(str(context.exception), "Concrete "
27-
"implementation required")
21+
# Add more specific assertions related to PepperoniPizza creation if
22+
# needed
23+
24+
def test_instantiation_abtract_class_error(self):
25+
with self.assertRaises(TypeError):
26+
# Attempt to instantiate the abstract class, which should raise
27+
# a TypeError
28+
instance=Pizza()
2829

2930

3031
if __name__ == '__main__':

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /