The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello, dear friends. In this video, we will examine the Singleton Design Pattern. So, let's begin. Singleton Design Pattern is in the category of creational design pattern. The purpose of this design pattern is to create only one instance of a class. In other words, when an instance is wanted to be created from any class, if there is no instance created before, a new one is created. If it has been created before, the existing instance is used. you can see the UML diagram of the singleton design pattern. According to the schema, to create the singleton class, we need to have a static member of class, private constructor, and static factory method. If you want, let's create a project with singleton. First, create a new project and name it Singleton. We will implement singleton to a database connection project.
Add a new class and name it Database. As I said, first we need a private constructor. Now, it's time to create a static method called getInstance. If we already have an instance, we don't need to create a new one. So, we have to check if it is null or not. Is null? Create an instance. Return. Let's add 'Create a connection method.' System out. Let's send a message, Connection is successful. Let's call this in main. Database, database. Call database, get Instance. Now, get connection. Done. If we run the project, we can see the connection is successful message. Run. Great. So, I hope you understand singleton a little better now. In the next video, we will look into the Abstract Factory Pattern. So, I'll see you in the next video, friends.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.