SDA Lab 05
SDA Lab 05
College of Engineering
(Software Engineering)
Total Marks: 10
Objective:
1) Make attendee understand the basic concept of Creational Design Pattern
2) Make attendee understand the concept of Null Object Design Pattern
3) Make attendee implement example of .net to create creational design pattern (Null Object)
Tools to be used:
Visual Studio is used to implement .net to create creational design pattern (Singleton).
What is Creational Design Pattern?
The Creational Design Pattern deals with object creation mechanisms; trying to create objects in a manner suitable
to the situation. It focuses on how the objects are created and utilized in an application. Creational Design Pattern
have five categories:
1. Singleton
2. Null Object
3. Abstract Factory
4. Prototype
5. Builder
A null object is also known as a Stub, an Active Nothing or an Active Null. It helps our program logic to get rid of
null checks where ever possible. We all know, that we cannot call methods on a null reference as it results in a
NullReferenceException. The null object pattern provides a non-functional object in place of a null reference and
therefore allows methods to be called on it. Null object design pattern is used to handle the following type of
exceptions:
To implement Null Object Design Pattern, we have to go through the following steps:
Step#03: Write the methods inside interface class. These methods we are going to use in further implementation.
Step#04: Add new class for mobile data. The class will contain the detail inside methods which has already
named in Interface class.
Step#05: Now inside Mobile Data class, write the methods one by one. In this solution, we are going to write details
of three types of mobile. We will define three classes of three different mobiles which will inherit the methods of
Interface class.
Step#06: After writing the details of three types of mobile, we have to define the null object class. Null object is
defined by using Singleton Design Pattern. Inside NullMobile class, we will define the methods which will do
nothing.
Step#07: Add new class of Mobile Repository. Our repository will return the details of different mobiles.
Step#08: We will use switch case statement in order to show the data of any mobile.
Step#09: Inside main program, create a variable name mobilerepository. By using that variable, we will
get the property GetMobileByName.
Step#10: Now write the mobile name you want to see details of. Check output, it will display the details of
samsung mobile.
Step#11: It’s time to check what null object design pattern will do. It will give nothing in result of nothing.
Lab Task:
1) Implement Null Object Design Pattern in Vehicle Scenario. (Marks: 4)
● Take at least 5 types of vehicles.
● Add 5 methods
● Ask user, which type of vehicle he wants to see.
Home Task:
2) Implement Null Object Design Pattern in a walking Scenario. (Marks: 4)
● Take at least 4 person
● Add 4 methods (up, down, right, left)
● Ask user, which person he wants to see.