0% found this document useful (0 votes)
7 views

Lab Manual 07

Uploaded by

umarzain2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Manual 07

Uploaded by

umarzain2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Usman Institute of Technology

Department of Computer Science


Course Code: SE308
Course Title: Software Design and Architecture
Fall 2022

Lab 07

OBJECTIVE: Working on Design Patterns


 To Understand Creational and Structural Design Patterns.
 To implement Abstract Factory and Composite Design Patterns.

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.

UML class diagram

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”

UML class diagram

Class Diagram of Composite Method

Participant
The classes and objects participating in this pattern are:

Component Interface:

The interface that all leaves and composites should implement.

Leaf:

A single object that can exist inside or outside a composite.

Composite:

A collection of leaves and/or other composites.

Client

Manipulate objects in the composition through the component interface


Example in Python
from abc import ABC, abstractmethod

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}')

def add_child_dept(self, dept):


self.childs.append(dept)

#
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

For Abstract FactoryPattern and Composite Design Patterns

a. Generate (from StarUML) UML diagram of the above patterns


b. Generate (from StarUML) UML diagram of the above patterns
Tools - > Apply Pattern - > Pattern Repository -> GoF
c. Compare your generated UML diagram with given code (example in python)
d. Convert your generated UML diagram according to the given code
e. Run the code and analyze the output

Home Task

a. Think about a real life example of the above implemented design patterns, and try to implement in
python programming language

You might also like