Professional Documents
Culture Documents
Dr. Md Samiullah
Assistant Professor
Department of Computer Science and Engineering
University of Dhaka
Outline
● Classical Mistakes in Software Engineering
● Software Engineering Principles
● Introduction to Software Design Patterns
● List of Patterns
● Patterns to be covered in this lab
Classical Mistakes in Software Engineering
● Undermined motivation
● Silver-bullet syndrome
● No risk management
● ………
Software Engineering Principles
● KISS (Keep It Simple Stupid)
subtype - Polymorphism)
general-purpose interface)
Solution:
Definitely we need separate new subclasses for other types of pizzas. Along
with a new “else if” in the method “orderPizza” in the class
“OriginalPizzaGhor”.
HOW TO ALLOW THE NEW CHANGES WITHOUT STOPPING THE WORKING SYSTEM/CODE.
Simple Factory …
ছিব ৬: নতু ন িপৎজা ঘর কাড। িপৎজা এখন ফ্যাক্টির থেক বািনেয় আনা হয়।
Simple Factory …
A friend of the pizza shop owner want a franchise for another city, say, “Sylhet”. He
wants a little change though in the condiments of pizza and elements. A little
thicker dough,more cheese and pieces of “Satkora”.
So, pizza from your factory will not be popular (will not work) in Sylhet.
Solution: GIVE THE PIZZA RECIPE TO YOUR FRIEND AND ALLOW HIM TO
MAKE SOME CHANGES AS HE FIND SUITABLE.
Factory Method …
Solution:
Now, the factory is no more needed. Location-wise different pizzas will be
baked by different subclasses.
Factory Method …
ছিব ৮: অ্যাবস্ট্রাক্ট িপৎজা ঘর। অডর্তিার কাড আেগরটাই, িকন্তু য যার িপৎজা িনেজরাই বানােব
Factory Method …
Everything was going well. A greedy manager started mixing some low quality
materials and degrading the quality of the pizzas. How to make sure a controlled
change in subclasses? Another friend from “Noakhali” wants a franchise. How to
ensure high quality in all the branches (subclasses)?
Solution:
● Inheritance vs composition
Decorator … (How inheritance may introduce problem?)
● Introduces dependency
● Example:
○ The HashSet Class has a method “size()” that return the elements in a hashSet
○ HashSet keeps unique elements and duplicate entries are not inserted
○ Say, we want to count the number of attempts to add numbers instead of the number of
● More problems:
○ If HashSet class gets a new method addFront(),
■ it subclass needs an overridden method
○ If subclass has a method not available in HashSet and later
HashSet gets such a method
■ The subclass will not compile unless “@override” is not
added
Scenario:
● First imagine a building that can be entered and exited through a door.
○ Buildings may have multiple floors.
● A floor like a building can be entered or exited (perhaps through an elevator) door.
○ Each floor can have several rooms.
● Each room can be entered or exited through its door.
Thus, a building can be thought of as a composite or combination of different floors and
rooms. Similarly, a floor can be thought of as a composite of several rooms. But a room is
just a room, no need to think of a room as a composite. A room is a leaf node here in a
hierarchy tree.
Composite …
Another scenario:
Let's say you are implementing a playlist of songs in Java.
Requirement: A playlist will contain many songs. But a playlist can also contain another
playlist.
● A playlist (where many songs are listed), and a single song - what can be the
common function between these two?
○ Both can actually be 'played'.
● Think of the attributes, the common attribute would be name.
○ Because just like a song has a name, we can give a playlist a name (like a playlist of patriotic songs).
● What can be the different functions or attributes between the two
○ A song can be 'added' or 'removed' in the playlist.
○ A song has a singer (singer name), a playlist does not have a singer.
Composite …
Composite …
Composite …
Composite …
Scenario:
Any system usually needs only one database controller or file management
controller. Their implementation is done using the singleton design pattern.
Singleton …
Singleton …
Singleton …
Singleton …
Singleton …
Singleton …
Observer
Scenario:
A celebrity comes live quite often on FB and you want to know about such live
sessions asap. Your option is to “Like” his page. Otherwise, you have to check
time-to-time in his page for an update or latest videos. In this scenario, you may
also miss some live sessions.
● Subject needs
○ A register method
○ An unregister method
○ Notify all
● Observer
○ An update method to get notifications
Observer …
2. https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm