123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Technology,-Gadget-and-Science >> View Article

Most Useable Core Creational Design Patterns In Java(singleton & Prototype)

Profile Picture
By Author: jatin
Total Articles: 28
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Explore how Singleton and Prototype patterns streamline Java applications by enhancing performance, reuse, and maintainability in real-world scenarios.
Design patterns are tried-and-tested solutions to common software design problems. 
In Java, they help developers write clean, reusable, and scalable code. 
This post focuses on two creational design patterns: 
1. Singleton Design Pattern
2. Prototype Design Pattern
1. Singleton Design Pattern:
The Singleton pattern ensures a class has only one instance and provides a global point of access to it.
Why Use It:
Use Singleton when you want exactly one object to coordinate actions across the system. It's perfect for things like:
Configuration settings
Database connections
Thread pools
 Logging
 Caches
Java Implementation (Thread-Safe Singleton):
public class ConfigManager {
 private static volatile ConfigManager instance;
private ConfigManager() {
 // private constructor
 }
public static ConfigManager getInstance() {
 if (instance == null) {
...
...  synchronized (ConfigManager.class) {
 if (instance == null) {
 instance = new ConfigManager();
 }
 }
 }
 return instance;
 }
public void printConfig() {
 System.out.println("App config data…");
 }
}
Database Connection Manager:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnectionManager {
 private static volatile DatabaseConnectionManager instance;
 private Connection connection;
 private static final String URL = "jdbc:mysql://localhost:3306/app_db";
 private static final String USER = "root";
 private static final String PASSWORD = "password";
private DatabaseConnectionManager() {
 try {
 this.connection = DriverManager.getConnection(URL, USER, PASSWORD);
 } catch (SQLException e) {
 throw new RuntimeException("Failed to connect to DB", e);
 }
 }
public static DatabaseConnectionManager getInstance() {
 if (instance == null) {
 synchronized (DatabaseConnectionManager.class) {
 if (instance == null) {
 instance = new DatabaseConnectionManager();
 }
 }
 }
 return instance;
 }
public Connection getConnection() {
 return connection;
 }
}
public class UserService {
 public void getUserData() {
 try {
 Connection conn = DatabaseConnectionManager.getInstance().getConnection();
 // Use the connection for a query (using Statement, PreparedStatement, etc.)
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
}
2. Prototype Design Pattern:
The Prototype pattern lets you clone existing objects instead of creating new ones from scratch. It's especially useful when object creation is expensive (e.g., loading from DB, complex setup).
Why Use It:
 When object construction is costly.
 When you need many similar objects with slight variations.
 When you want to avoid subclassing
 
Document Template System:
Building an enterprise app that generates business reports (invoices, summaries, charts). Each report starts from a base template, but the content is customized per user or client.
Instead of recreating everything from scratch, we clone a base report object and make changes.
public class ReportTemplate implements Cloneable {
 private String title;
 private String content;
 private String footer;
public ReportTemplate(String title, String content, String footer) {
 this.title = title;
 this.content = content;
 this.footer = footer;
 }
public void setContent(String content) {
 this.content = content;
 }
public void print() {
 System.out.println("=== " + title + " ===");
 System.out.println(content);
 System.out.println(" - - " + footer + " - -");
 }
@Override
 public ReportTemplate clone() {
 try {
 return (ReportTemplate) super.clone();
 } catch (CloneNotSupportedException e) {
 throw new RuntimeException("Failed to clone ReportTemplate", e);
 }
 }
}
public class Main {
 public static void main(String[] args) {
 // Base template
 ReportTemplate monthlyReport = new ReportTemplate(
 "Monthly Report",
 "This is a placeholder content.",
 "Confidential"
 );
// Clone for Client A
 ReportTemplate clientAReport = monthlyReport.clone();
 clientAReport.setContent("Revenue: $10,000\nProfit: $2,500");
// Clone for Client B
 ReportTemplate clientBReport = monthlyReport.clone();
 clientBReport.setContent("Revenue: $12,000\nProfit: $3,000");
// Display both
 clientAReport.print();
 clientBReport.print();
 }
}
When to Use What:
Use Case Singleton Prototype
One shared instance needed ✅ ❌
Performance matters in object creation ❌ ✅
Slight variations of the same object ❌ ✅
Global access point required ✅ ❌
Test yourself on what you've learned above, and compare your answers with the correct ones provided below.
Q1. What is the main purpose of the Singleton pattern?
A. Ensure multiple instances of a class
B. Allow cloning of objects
C. Ensure only one instance of a class
D. Avoid subclassing
Please visit our website to know more:-https://cyberinfomines.com/blog-details/most-useable-core-creational-design-patterns-in-java%28singleton-&-prototype%29

Total Views: 88Word Count: 497See All articles From Author

Add Comment

Technology, Gadget and Science Articles

1. How Dex Development Works
Author: davidbeckam

2. The Benefits Of Multilingual Social Media Marketing
Author: glasgowtranslationservices

3. Odoo For Healthcare Management System: Reasons Why Healthcare Businesses Should Get It Now
Author: Alex Forsyth

4. Spraying Drone Services: Bring Revolutionary Transformation In Weed Management And Sustainability
Author: Alex Wilkinson

5. Doordash Restaurant Data Extraction: Mapping Coverage With Web Scraping Tools
Author: Mobile App Scraping

6. Simple Methods To Scrape Airbnb Listings For Travel Insights
Author: travel

7. Navigating The Legalities Of Driver's License Translation
Author: londontranslationservice

8. The Impact Of Translation Services On International Litigation
Author: premiumlinguisticservices

9. The Best Materials For Durable And Eye-catching Business Cards
Author: printitusa

10. Sitecore Content Cloud: The Future Of Scalable And Seamless Digital Content Management
Author: Addact Technologies

11. Nlp Sentiment Analysis | Reviews Monitoring For Actionable Insights
Author: Mellisa Torres

12. How Does Walmart Product Code Scraping Help To Collect Upc And Asin Codes Fast?
Author: Mobile App Scraping

13. Tired Of One-size-fits-all Software? Odoo Customisation Has The Answer
Author: Alex Forsyth

14. The Future Is Now: How Augmented Reality Is Transforming Ecommerce
Author: Sdreatech

15. Ethical Ai In Decision-making: Best Practices | Impaakt
Author: Impaakt Magazine

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: