0% found this document useful (0 votes)
103 views5 pages

Lab 11 (Creational Design Pattern (GoF) )

This document discusses design patterns and the factory pattern specifically. It provides definitions of design patterns, their elements and types. It then describes the factory pattern as a creational design pattern that deals with object creation. It gives the design purpose of the factory pattern as creating objects in situations where the constructor is inadequate. It provides a class diagram and code example to illustrate the factory pattern. It also summarizes the singleton pattern and provides steps to apply it to a MyClass example. Finally, it provides tasks to apply the singleton pattern to a student class.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views5 pages

Lab 11 (Creational Design Pattern (GoF) )

This document discusses design patterns and the factory pattern specifically. It provides definitions of design patterns, their elements and types. It then describes the factory pattern as a creational design pattern that deals with object creation. It gives the design purpose of the factory pattern as creating objects in situations where the constructor is inadequate. It provides a class diagram and code example to illustrate the factory pattern. It also summarizes the singleton pattern and provides steps to apply it to a MyClass example. Finally, it provides tasks to apply the singleton pattern to a student class.

Uploaded by

Wasie Urrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Course Code: Software Design & Architecture.

Lab 09: Creational Design Pattern (GoF)

Design Pattern:

Design patterns are evolved as reusable solutions to the problems that we encounter everyday of
programming.

“A pattern describes a problem that occurs often, along with a tried solution to the problem”
- Christopher Alexander, 1977

Design patterns are descriptions of communicating objects and classes that are customized to
solve a general design problem in a particular context.
A design pattern is not a finished design that can be transformed directly into code. It is a
description or template for how to solve a problem that can be used in many different situations.

Elements of Design Pattern:

In general, a pattern has four essential elements:


1. Pattern Name- Useful part of software design
2. Problem- When to apply the pattern
3. Solution- Elements that make up the pattern, their
relationships, responsibilities, and collaborations.
4. Consequences- Results and trade-offs of applying the pattern

Types of Design Patterns:

1. Creational patterns
– Creating a collection of objects in flexible ways
2. Structural patterns
– Representing a collection of related objects
3. Behavioral patterns
– Capturing behavior among a collection of objects.

Creational Design Pattern:

• In software engineering, creational design patterns are design patterns that deal with
object creation mechanisms, trying to create objects in a manner suitable to the
situation.
• The basic form of object creation could result in design problems or added complexity
to the design. Creational design patterns solve this problem by somehow controlling this
object creation

Centre for Advance Studies in Engineering Page 1


Course Code: Software Design & Architecture.
Lab 09: Creational Design Pattern (GoF)

Factory: Creational Design Pattern:

Design Purpose
Create individual objects in situations where the constructor alone is
inadequate.

Design Pattern Summary


Use methods to return required objects.
In class-based programming, the factory method pattern is a creational pattern that uses
factory methods to deal with the problem of creating objects without having to specify the exact
class of the object that will be created.

Example:
Class Diagram:

Centre for Advance Studies in Engineering Page 2


Course Code: Software Design & Architecture.
Lab 09: Creational Design Pattern (GoF)

Code:

Centre for Advance Studies in Engineering Page 3


Course Code: Software Design & Architecture.
Lab 09: Creational Design Pattern (GoF)

Centre for Advance Studies in Engineering Page 4


Course Code: Software Design & Architecture.
Lab 09: Creational Design Pattern (GoF)

Lab Task:

Singleton Design Pattern:

Design Purpose
Ensure a class (e.g S) has only one instance, and provide a global point of access to it.
Design Pattern Summary
Make the constructor of S private; define a private static attribute for Singleton class of type S;
define a public access or for it.

The Singleton Design Pattern Applied to MyClass: 3 Steps

1. Define a private static member variable of MyClass of type MyClass


private static MyClass singletonOfMyClass = new MyClass();
2. Make the constructor of MyClass private
private MyClass() { /* ….constructor code …. */ };
3. Define a public static method to access the member
public static MyClass getSingletonOfMyClass()
{
return singletonOfMyClass;
}

 Create a class named student.


 Define two attribute: name & id
 Make getters and setters of name & id.
 Apply singleton pattern, so one object of Single_Object could be created.
 Create the main method.
 Call getters and setters method from main method

Centre for Advance Studies in Engineering Page 5

You might also like