I remember when I was taking CS 101 And CS 102 I always write something that does needed in code or write one same code in different classes or making easy code really difficult and I still do. It’s really hard to write perfect code. This is why I chose this design pattern topic to write this blog. In this blog I will show you how I was writing code in beginning of computer science and How I write my code Now. I will show what makes changes in and how I did it.
Let’s start with how I was writing Poorly designed code that caused problems with systems, making them hard to understand, modify, and maintain.We can says that some of this poorly designed code as specific design smells. When I Went back too look at my code I found that my code had problem like I writhing long Method , Duplicate code , Rigidity,Fragility, Immobility, Viscosity, Needless Complexity, Needless, Repetition. This all is define that I was writing poor code. This kind a smell will prevent you from getting a job and it will extend time to make software too.The difficulty this creates in maintaining and modifying code this is called technical debt and can be removed by refactoring.
We we want to design software that is easy to maintain and easy to modify. But, the obvious solutions to design problems can lead to brittle software that cannot be modified or maintained easily. Design patterns represent common software design problems and well-tested solutions to those problems.There are are more than 1 type of design pattern where you can use in code to make it look professional. Today we will look at That pattern that I used in my code Call Singleton design pattern so this pattern is about define a class that has only one instance and provides a global point of access to it. In easy way a class must ensure that only single instance should be created and single object can be used by all other classes. In bottom you will example of Singleton design.
In this example How Price class GFC is implemented in public class. And this will make your code really easy and you can access it any time . The application of Singleton design pattern have multiple Uses you can Hardware interface access, logger, Cache, and many more Way. After learning couple of pattern my code improved and made it professional.
This are main point of learning of Singleton classes.
- Singleton classes can have only one instance and that instance should be globally accessible.
- java.lang.Runtime and java.awt.Desktop are 2 singleton classes provided by JVM.
- Singleton Design pattern is a type of creational design pattern.
- Outer classes should be prevented to create instance of singleton class.