Lab Manual 07
Lab Manual 07
Lab 07
Student Information
Student Name
Student ID
Date
Assessment
Marks Obtained
Remarks
Signature
Usman Institute of Technology
Department of Computer Science
SE308 - Software Design and Architecture
Lab 07
Abstract Factory
Definition
Provide an interface for creating families of related or dependent objects without specifying their concrete
classes.
Participants
The classes and objects participating in this pattern are:
AbstractFactory
o declares an interface for operations that create abstract products
ConcreteFactory
o implements the operations to create concrete product objects
AbstractProduct
o declares an interface for a type of product object
Product
o defines a product object to be created by the corresponding concrete factory
o implements the AbstractProduct interface
Client
o uses interfaces declared by AbstractFactory and AbstractProduct classes
Example in Python
class Door:
def getDescription(self):
pass
class WoodenDoor(Door):
def getDescription(self):
print ('I am a wooden door')
class IronDoor(Door):
def getDescription(self):
print ('I am an iron door')
class DoorFittingExpert:
def getDescription(self):
pass
class Welder(DoorFittingExpert):
def getDescription(self):
print ('I can only fit iron doors')
class Carpenter(DoorFittingExpert):
def getDescription(self):
print ('I can only fit wooden doors')
class DoorFactory:
def makeDoor(self):
pass
def makeFittingExpert(self):
pass
class WoodenDoorFactory(DoorFactory):
def makeDoor(self):
return WoodenDoor()
def makeFittingExpert(self):
return Carpenter()
class IronDoorFactory(DoorFactory):
def makeDoor(self):
return IronDoor()
def makeFittingExpert(self):
return Welder()
if __name__ == '__main__':
woodenFactory = WoodenDoorFactory()
door = woodenFactory.makeDoor()
expert = woodenFactory.makeFittingExpert()
I am a wooden door
door.getDescription() I can only fit wooden doors
expert.getDescription() I am an iron door
I can only fit iron doors
ironFactory = IronDoorFactory()
door = ironFactory.makeDoor()
expert = ironFactory.makeFittingExpert()
door.getDescription()
expert.getDescription()
Composite Method
“Compose objects into tree structures to represent part-whole hierarchies”
Participant
The classes and objects participating in this pattern are:
Component Interface:
Leaf:
Composite:
Client
class BaseDepartment(ABC):
@abstractmethod
def __init__(self, num_of_employees):
pass
@abstractmethod
def print_department(self):
pass
class Accounting(BaseDepartment):
def __init__(self, num_of_employees):
self.num_of_employees = num_of_employees
def print_department(self):
print(f"Accounting employees: {self.num_of_employees}")
class Development(BaseDepartment):
def __init__(self, num_of_employees):
self.num_of_employees = num_of_employees
def print_department(self):
print(f"Development employees: {self.num_of_employees}")
class Management(BaseDepartment):
def __init__(self, num_of_employees):
self.num_of_employees = num_of_employees
self.childs = []
def print_department(self):
print(f"Management base employees: {self.num_of_employees}")
total_emp_count = self.num_of_employees
for child in self.childs:
total_emp_count += child.num_of_employees
child.print_department()
print(f'Total employees: {total_emp_count}')
#
acc_dept = Accounting(200)
dev_dept = Development(500)
management_dept = Management(50)
management_dept.add(acc_dept)
management_dept.add(dev_dept)
# print dept
management_dept.print_department()
Student Tasks:
Class Task
Home Task
a. Think about a real life example of the above implemented design patterns, and try to implement in
python programming language