Design Pattern Assignment 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

University of Lahore

(FALL 2020)
Department of Software Engineering

Lecturer: Zunair Mahmood Course Title: Design Pattern


Assignment 1
Due Date: 23-Oct-2020

Submitted By : Irfan Muhammad


Sap id : 70067306
Section: (T)
Question 1:
Why are design patterns important? Support your answer with a couple of examples.

For a very long time there was a serious problem in software


engineering: you hire a newcomer to a project and no matter how
well they know the programming language, it takes them months to
get up to speed with how things are done in your project before they
can be productive.
The software industry has been trying to come up with ways to
improve this. Standardization of programming languages was one
way. Standard libraries (class libraries nowadays) has been another
way; however, one of the most important ways has perhaps been
design patterns.

 The pattern provides an abstraction of the data logic


 When implemented properly it facilitates the unit testing. It is easy to mock your repositories. Some
great libraries save you a lot of time as you don’t need to write any stubs which can be timely
 No repetition needed as the basic operations on your entities are taken care of by the pattern

Software is more and more complex nowadays and even in small applications we would find different
patterns associated together. If this is not the case it is probably a sign of bad architecture. The Generic
Repository pattern matches very well with the Unit of Work. The principle of the latter is to keep track of
everything during a business transaction to update the database only at the end. Let’s say for example that
during a transaction you need to change the data of three different entities. Everything goes well for the first
two but you encounter a problem for the third. By using a Unit of Work you are sure that your data won’t
change unless the modification of the three entities is a success. To summarise, the beauty of using the Unit
of Work pattern is that you can do the manipulation you want on your data and persist with the changes at
the end in one go.

 Communication, Learning and Enhanced Insight: Over the last decade design patterns have
become part of every developer’s vocabulary. This really helps in communication. One can easy tell
another developer on the team, “I’ve used Command pattern here” and the other developer understands not
just the design, but can also easily figure out the rationale behind it. Design Patterns really help in learning,
esp. when you are new on a project. Also this helps in providing developers with better insight about parts
of the application or 3rd party frameworks they use.
 Decomposing System into Objects : The hard part about OO Design is finding the appropriate
objects and decomposing a system. One has to think about encapsulation, granularity, dependencies,
flexibility, performance, evolution, reusability and so on. They all influence decomposition, often in
conflicting ways. Design Patterns really helps identify less obvious abstractions. These objects are seldom
found during analysis or even the early design, they’re discovered later in the course of making a design
more flexible and reusable.
 Determining Object Granularity: One thing I struggle a lot with is finding the right level of
abstraction and granularity. Design patterns helps in coming up with objects with different levels
of granularity that makes sense.
 Specifying Object Interface : Identifying the right interface and the relationship between various
interface is not a one-shot activity. Usually it takes several iterations to identify the right composition of
interfaces. Forget interfaces, most of the times, coming up with a method signature can also be quite tricky.
Design Patterns really helps in this area.
 Specifying Object Implementation : How should we implement an Object? Given an interface
there could be multiple concrete classes of that type, each one can have very different implementations.
Design Patterns provide guidance like Program to an interface (type) not an implementation (concrete
class) which can result in really good OO code.
 Ensuring right reuse mechanism : When to use Inheritance, when to use Composition, when to use
Parameterized Types? Is delegation the right design decision in this context? There are various questions
that comes to a programmer’s mind when they are trying to design highly reusable and maintainable code.
Knowledge of design patterns can really come handy when making such decisions.
 Relating run-time and compile time structures : An object oriented program’s run-time structure
often bares little resembles to this code structure. Sometimes looking at the code does not give us the
insights into run-time structure. Knowledge of design patterns can make some of the hidden structure
obvious.
 Designing for change : We all know that lack of continuous refactoring and design that doesn’t take
change into account risks major redesign in the future. Over the years we’ve also learnt that big upfront
designs can’t standup against the constant additon of software requirements. We’ve leant grouping
elements with similar change life cycle together yields in far more flexible and extendable design. If we
think some behavior or element of behavior is most likely to change, we try to abstract that behavior in one
place. While we understand these concepts are important design patterns really make it possible to design
such systems. Each design pattern lets some aspect of the system structure vary independently of other
aspects, thereby making a system more robust to a particular kind of change.

Question 2:
All kind of patterns can be divided into three kinds:
 Creational
 Structural
 Behavioral
Briefly discuss each kind of the above mention pattern types and establish key differences among all three.

Answer

Creational:
These design patterns provide a way to create objects while hiding the creation logic, rather than
instantiating objects directly using new operator. This gives program more flexibility in deciding which
objects need to be created for a given use case.
Structural:
These design patterns concern class and object composition. Concept of inheritance is used to compose
interfaces and define ways to compose objects to obtain new functionalities.

Behaviorals:
These design patterns are specifically concerned with communication between objects.

1) Creational Patterns:
The creational patterns are about class instantiation. These patterns are differentiated into class-creation
patterns and object-creation patterns is all about class instantiation al patterns. And this patterns use object
oriented programming efficiently during the instantiation, object-creation patterns use delegation
successfully which fulfil the work. These pattern helps in improving the performance to great extent.

Factory Creates object without showing the logic externally.


 Abstract Factory Offers an interface to create a family of related objects.
 Builder It helps in creating objects by defining an instance
 Object Pool Helps in referring and sharing of objects which are at high cost for creation.
 Prototype Helps in creation of new objects by referring the prototype.
 Singleton Provides global access point to objects and make sure that class is created with
only one instance.

2) Structural Patterns:
These are the outline designs which facilitate the structure by recognizing a straightforward approach to
acknowledge relationships between elements. It is all about class and object composition. These patterns
simplifies the design by finding a best method to realize relationships between entities. They use inheritance
to compose interfaces.

 Adapter Method Change over interface into an alternate interface.


 Composite Method Compose objects into tree structures to represent part-whole
hierarchies.
 Decorator Add additional responsibilities dynamically to object.
 Flyweight pattern The aim of this pattern is to utilize imparting to bolster countless that
have a piece of their inward state in like manner where the other piece of state can differ.

3) Behavioural Patterns:
This design patterns deals with Class's objects communication or their interaction. These patterns main
target of using object oriented programming is achieved by giving importance to the interaction between the
objects. These designing patterns are concerned with interaction between the objects.
Interpreter A method to define elements of language.
 Chain of responsibility Uses to send a request between objects chain.
 Command Encapsulate a request in an object
 Iterator Uses the elements in an order from the library
 Mediator Defines ease way of communicating between classes or objects
 Memento It does not violate encapsulation and helps in restoring the object to its original
state at any point of time.

Note: Plagiarism will straight up result in ZERO marks

You might also like