Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Week8 DesignPatterns Singleton

Uploaded by

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

Week8 DesignPatterns Singleton

Uploaded by

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

Software Engineering

Week 8
Software Engineering
Wk Lecture Practical
1 Introduction Canvas, Assessment Understand the case
Software lifecycle study.
Write the user stories.
2 Work as a group! Agile
Design the database and
Plan the work on Review of the software
the software
the UI and the requirements and design
Set the version control
Use Case Review of the OOP concepts.
diagram.
3 User Stories Git
4 Plan the work on Graphical User Interface. MVC Coding
the user stories pattern. Retrospective
Check if you are on
5 Plan the work on Create and connect the database
track.
the database to the application.
6 Plan the current From UML to C# code
task
7 Plan the current Testing – Manual and Automated
task
8-9 Retrospective. Design patterns More coding.
Plan the current Accomplish the quality.
task. Secure coding.
10 Software Validation and Double check.
Verification No new features.
Enhancements.
11 Software Maintenance.
Learning Outcomes

Learn about Design Patterns and their importance.

Identify the situations where a certain design pattern can


be used.

Learn what the Singleton Design Pattern is and how to


use it.
House

How would you build a house?


House

So… a house needs:


• Walls
• A roof
• Windows
• Door(s)
• floors
• ceilings
• …

For each of these there are some existing patterns to


choose from!
House

Let’s have a look at the windows…

Different designs.

But they have something in common:


•a frame
•glass
•a mechanism to open and shut
Design Patterns

“Each pattern describes a problem which occurs over


and over again in our environment, and then describes
the core solution to that problem, in such a way that you
can use this solution a million times over, without ever
doing it the same way twice.”

Alexander, C., Ishikawa, S., Silverstein, M. & co., A Pattern Language, 1977
Design Patterns

E. Gamma, R. Helm, R. Johnson, J. Vlissides


(The gang of four)
described 23 design patterns.
Design Patterns
Creational Structural Behavioral
Abstract Factory Adapter Chain of Responsibility

Builder Bridge Command


Prototype Composite Iterator
Singleton Decorator Mediator
Factory Method Facade Memento
Flyweight Observer
Proxy State
Strategy
Visitor
Interpreter
Template Method
Design Patterns

A few examples….
Problem

Investors and stock

The registered investors have to be notified every time


the stock changes value.
Problem

A patient monitoring system:

When the system detects the heart stopping the system


must:
• Sound an alarm
• Call the crash team
• Inform the nurses station
• Page doctor currently on call for this patient

May have future functions not yet created


Problem

Products

When a company changes the product prices all the


companies using the affected products have to be notified.
Problem

What do all these problems have in common?


Problem

Can you think of a similar example?


Problem

The problems above define a one-to-many dependency


between objects so that when one object changes state, all its
dependents are notified and updated automatically.

If we know how to solve one of them then the same mechanism


should be applied to the other ones.

(Observer Design Pattern)

Gamma, E., Helm, R., Johnson, R., Vlissides, J.,Design Patterns, Elements of Reusable Object-Oriented Software, ISBN 0201633612
Let’s have a look at another set of similar examples…
Problem

Car Factory
A car factory builds three types of car:
small
luxury
saloon
The manufacturer (Ford, BMW) decides what components
go in each car.
Problem

Documents
Résumé, report, journal paper are types of document.
They contain different sections such as: introduction,
skills, education, experience, results, conclusion,
summary, etc.
Problem

Can you think of a similar example?


… from your assignment?
Problem

What do all these problems have in common?


Problem

The problems above define an interface for creating an object, but let
subclasses decide which class to instantiate. Factory Method lets a
class defer instantiation to subclasses.
If we know how to solve one of them then the same mechanism should
be applied to the other ones.

(Factory Method Design Pattern)

Gamma, E., Helm, R., Johnson, R., Vlissides, J.,Design Patterns, Elements of Reusable Object-Oriented Software, ISBN 0201633612
Design Patterns

We’ve seen so far two important design patterns:


Observer and Factory Method.

Let’s look at the most used design patterns into detail.


Design Patterns

Singleton Design Pattern


Problem

Logger classes

Are classes used to implement a logging system. All the application


components have access to one logging access point without being
necessary to create an object each time a logging operations is
performed.

See:
https://msdn.microsoft.com/en-us/library/microsoft.build.utilities.logger
.aspx
Problem

Database

One access point to the database. Only one connection


should exist.
Singleton Design Pattern

Definition:

Singleton pattern ensures that only one instance of the


class can be created.
The singleton class must provide a global access point to
get the instance of the class.

Gamma, E., Helm, R., Johnson, R., Vlissides, J.,Design Patterns, Elements of Reusable Object-Oriented Software, ISBN 0201633612
Singleton Design Pattern

Usage:

Singleton pattern is used for logging, drivers objects,


caching and thread pool.
Singleton Design Pattern

Implementation:
Private constructor - to restrict instantiation of the
class from other classes.
Private static variable of the same class - is the only
instance of the class.
Public static method - returns the instance of the
class. It’s the global access point for outer world to get the
instance of the singleton class.
Singleton Design Pattern

UML Class Diagram

Gamma, E., Helm, R., Johnson, R., Vlissides, J.,Design Patterns, Elements of Reusable Object-Oriented Software, ISBN 0201633612
Observer Design Pattern

Singleton
• defines an Instance()
method that lets the other
application’s components
access its unique
instance. Instance() is a
class method (static).
• responsible for
creating and maintaining
its own unique instance.

http://www.dofactory.com/net/singleton-design-pattern
Singleton Design Pattern

Example:

SingletonDesignPattern project.
Reading List

Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design


Patterns, Elements of Reusable Object-Oriented Software,
Addison Wesley, ISBN 0201633612

http://www.dofactory.com/net/design-patterns
Questions

You might also like