C# Basics-Part 4(Static Class and Singleton Design Pattern)
Static Class
A static class can be created using the static keyword and it can contain only static members.
We cannot create an instance of a static class.
Members of the static class can be accessed using the class name.
So why you need static classes?
Where do I use static classes?
Static classes are used where there is a need for set methods that just operate on input parameters example utility classes, converter classes.
Requirement
Suppose your application under test requires your test script to enter details of user like the first name, last name etc and say this is a precondition of some of your test scripts.
You can have a utility class that provides random data to the calling code, check below
Important Point about static class
We can solve this using a singleton design patten
Now we require a single instance of a class, so we know that to create an instance of a class we use the new keyword.
So how do we restrict other classes from using the new keyword for our class?
Simple. we will make our constructor private.
Ok thats cool so far however we need a single instance of the class, so how do we create that as our constructor is private?
We can have the same class to create and manage its instance, all other class which needs the instance will ask this class.
How to do that?
Check below code
We started with creating an instance variable of our class that can be used by other classes
Then we created our constructor and marked it private so it can be accessed only from the same class.
In the constructor we created a new instance if it was not created and for all other calls returned the earlier created instance, this guarantees we are creating only one instance.
In the calling code, we are querying the instance twice and we notice that instance is created during our first call, for another call it returns the earlier created instance.
You must be thinking even static classes provide a single instance and how should one decide which one to use?
Well there are some advantages of using singleton over static classes
Now the main advantage I think of singleton class over static class is that of control.
Yes, control. I can create/dispose a singleton instance however in case of static classes I don't have any control over it.
Now the main question which one to use in our test automation code?
For developers, they use minimum static classes due to the issue of testability of static code.
For Automation code, I think you should choose your option wisely
If you need just some set of methods to operate on input data and do not need any OOP features then you can go with static classes.
If you may need some OOP features and want control on the instance then you should go for singleton instance as we saw in our above example .
That brings us to the end of this lecture ✌
A static class can be created using the static keyword and it can contain only static members.
We cannot create an instance of a static class.
Members of the static class can be accessed using the class name.
So why you need static classes?
Where do I use static classes?
Static classes are used where there is a need for set methods that just operate on input parameters example utility classes, converter classes.
Requirement
Suppose your application under test requires your test script to enter details of user like the first name, last name etc and say this is a precondition of some of your test scripts.
You can have a utility class that provides random data to the calling code, check below
- Contains only static members
- Cannot be instantiated
- Use the class name to access the class members
- Is sealed, cannot be inherited from any class expect object
- Cannot contain instance constructor however they can contain a static constructor
Singleton Design Patterns
Design patterns are solutions to problems that have worked for people.
Is an idea to consider to see if they would apply in your situation.
All in all, I will call design patterns as solutions which have worked for many out there, so you need not to reinvent the solution for a similar problem
Now suppose we have a requirement in our automation project which says the browser manager module (DriverManager class) single instance should be shared across the classes.
We will break the requirement as below
- A single instance of a class
- Single instance available for other classes
We can solve this using a singleton design patten
Now we require a single instance of a class, so we know that to create an instance of a class we use the new keyword.
So how do we restrict other classes from using the new keyword for our class?
Simple. we will make our constructor private.
Ok thats cool so far however we need a single instance of the class, so how do we create that as our constructor is private?
We can have the same class to create and manage its instance, all other class which needs the instance will ask this class.
How to do that?
Check below code
We started with creating an instance variable of our class that can be used by other classes
private static DriverManager instance=null;
Then we created our constructor and marked it private so it can be accessed only from the same class.
In the constructor we created a new instance if it was not created and for all other calls returned the earlier created instance, this guarantees we are creating only one instance.
public static DriverManager GetInstance() { if(instance==null) { instance = new DriverManager(); } return instance; }
In the calling code, we are querying the instance twice and we notice that instance is created during our first call, for another call it returns the earlier created instance.
DriverManager.GetInstance(); DriverManager.GetInstance();
You must be thinking even static classes provide a single instance and how should one decide which one to use?
Well there are some advantages of using singleton over static classes
- Both of them guarantees single instance however singleton instance can be passed as a parameter to other methods
- Singleton can have child objects
- Singleton supports object-oriented programming features like it can implement an interface.static classes cannot do so.
Now the main advantage I think of singleton class over static class is that of control.
Yes, control. I can create/dispose a singleton instance however in case of static classes I don't have any control over it.
Now the main question which one to use in our test automation code?
For developers, they use minimum static classes due to the issue of testability of static code.
For Automation code, I think you should choose your option wisely
If you need just some set of methods to operate on input data and do not need any OOP features then you can go with static classes.
If you may need some OOP features and want control on the instance then you should go for singleton instance as we saw in our above example .
That brings us to the end of this lecture ✌
Comments
Post a Comment