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

SDA-7 lecture for algorithm

Data structure and algorithm lecture number 7

Uploaded by

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

SDA-7 lecture for algorithm

Data structure and algorithm lecture number 7

Uploaded by

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

Lecture-7

Software Design & Architecture


(SDA)
Muhammad Jamil
Software Developer
[email protected]
Today We will cover

• Introduction to Design Pattern


• Importance of Design Patterns
• Design Problems
• Elements of Design Patterns

• Design Pattern Types


• Creational Design Pattern
• Factory Pattern
• Abstract Factory Pattern
• Introduction to Behavioral Design Pattern
Introduction to Design Pattern

• Common solution to reoccurring problem in design


• Give way to design the solution of a problem
• We will follow GOF (Gang of Four) design pattern
classification
Importance of Design Pattern
• Give help in providing more understandable code
• Provide code maintainability
• Provide help to communicate design goals amongst
programmers
• Provide help to identify common solutions to common
problems
• Allow you to write less code because more of your code
can derive common functionality from common base
classes.
Importance of Design Pattern

• Provide well documentation


• Reduce development time
• Reduce errors
• Decompose system into objects
• Specify object interfaces
Design Problems

• Which design pattern will follow?


• Applying wrong pattern causes project failure
• Each problem has its own pattern to solve
• Bad design will consume the developer’s time
• Bad design will consume so much cost of an organization
Elements of Design Patterns
• A name, that is a meaningful reference to the pattern
• Description of problem area that explains when the
pattern may be applied
• A solution description of the parts of the design solution,
their relationships, and their responsibilities
• A statement of the consequences—the results and trade-
offs—of applying the pattern. This can help designers
understand whether or not a pattern can be used in a
particular situation
Types of Design Patterns

Classification of Design Patterns

GOF Design
Patterns

Creational Behavioral Structural


Types of Design Patterns

• “Purpose” based classification


• Creational: – concerns with creation process of objects & classes
• Structural: – composition of classes & objects
• Behavioral: – characterizes interaction & responsibility of objects
& classes
Types of Design Patterns
• “Scope” based classification
• decided if the pattern applies to mainly classes or objects
• Two categories
• Class scope
• Relationship between classes & subclasses
• statically defined at run-time object scope
• Object Scope
• Object relationships
• Can be manipulated at runtime
Creational Design Pattern

Creational Design
Pattern

Abstract Factory
Factory Method
Method
Factory Pattern
• Factory pattern is one of most used design pattern in
Java.
• This type of design pattern comes under creational
pattern as this pattern provides one of the best ways to
create an object.
• In Factory pattern,
• we create object without exposing the creation logic to the client
and
• refer to newly created object using a common interface.
Implementation
• We're going to create
a Shape interface and
concrete classes
implementing the Shape
interface.
• A factory class
ShapeFactory is defined
as a next step.
• FactoryPatternDemo, our
demo class will use
ShapeFactory to get
a Shape object.
• It will pass information
(CIRCLE / RECTANGLE /
SQUARE) to
ShapeFactory to get the
type of object it needs.
Step 1: Create an interface

14
Step 2: Create concrete classes
implementing the same interface

15
Step 3: Create a Factory to generate object of
concrete class based on given information.

16
Step 4: Use the Factory to get object of
concrete class by passing an information such as
type

17
Step 4: Verify the output

18
Practical Use of Factory Pattern

• Can be used to create Plug-ins.


• For Example, You can have your main application call the class
factory to instantiate the specific plugin implementing some
interface that you've developed to in your main app.
• This way, you can code the main portion of your application
without ever needing to know what is going to be plugged in.
• You can find various other uses on the net.
Abstract Factory Pattern
• Abstract Factory patterns works around a super-factory
which creates other factories.
• This factory is also called as Factory of factories.
• This type of design pattern comes under creational
pattern as this pattern provides one of the best ways to
create an object.
• In Abstract Factory pattern an interface is responsible for
creating a factory of related objects, without explicitly
specifying their classes.
• Each generated factory can give the objects as per the
Factory pattern.
Implementation

• We're going to create


a Shape and Color interfaces and
concrete classes implementing
these interfaces.
• We create an abstract factory
class AbstractFactory as next
step.
• Factory classes
ShapeFactory and ColorFactory
are defined where each factory
extends AbstractFactory.
• A factory creator/generator
class FactoryProducer is created.
Implementation
• AbstractFactoryPatternDemo, our
demo class
uses FactoryProducer to get a
AbstractFactory object.
• It will pass information (CIRCLE /
RECTANGLE /
SQUARE for Shape)
to AbstractFactory to get the
type of object it needs.
• It also passes information (RED /
GREEN / BLUE for Color)
to AbstractFactory to get the
type of object it needs.

22
Step 1: Create an interface for Shapes

23
Step 2:
Create concrete classes implementing the same interface

24
Step 3+4:
Create an interface for Colours + Implementation classes

25
Step 5: Create an Abstract class to get factories for
Colour and Shape Objects

26
Step 6: Create Factory classes extending Abstract
Factory to generate object of concrete class based on given
information

27
Step 6: Create Factory classes extending Abstract
Factory to generate object of concrete class based on given
information

28
Step 7: Create a Factory generator/producer class to get
factories by passing an information such as Shape or Color

29
Step 8: Use the FactoryProducer to get AbstractFactory in
order to get factories of concrete classes by passing an
information such as type.

30
Step 9: Verify the output
Behavioral Design Pattern

Behavioral
Design Pattern

Strategy Observer
Design Pattern Design Pattern
Summary
• Introduction to Design Pattern
• Importance of Design Patterns
• Design Problems
• Elements of Design Patterns
• Design Pattern Types
• Creational Design Pattern
• Factory Pattern
• Abstract Factory Pattern
• Introduction to Behavioral Design Pattern
Thank You!

You might also like