Skip to content
/ singleton Public template

Educational repository demonstrating the Singleton design pattern in Java and C++. Includes simple, clear implementations for students to explore, compile, and run.

Notifications You must be signed in to change notification settings

dataproctech/singleton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Singleton Pattern β€” Java & C++ Examples

This repository provides a minimal and easy-to-understand implementation of the Singleton design pattern in Java and C++. The goal is to help students learn how the pattern works, why it is used, and how to run simple example implementations on their own.


πŸ“Œ What is the Singleton Pattern?

The Singleton pattern ensures that a class has only one instance during the program’s lifetime and provides a global point of access to it.

Typical use cases include:

  • Configuration managers
  • Logger classes
  • Database connection managers
  • Global state containers

Both examples in this repository demonstrate:

  • A private constructor
  • A static method that returns the single instance
  • Prevention of copying or instantiation from outside

βœ” Java Example (Geeks.java)

This file shows a classical lazy-initialized Singleton in Java.

βœ” C++ Example (main.cpp)

This file demonstrates a simple pointer-based Singleton implementation in C++ (and confirms that two calls return the same instance). The example corresponds to the uploaded code .


β–Ά How to Run the Examples

Run the Java Version

  1. Navigate to the folder:

    cd java
  2. Compile:

    javac Geeks.java
  3. Run:

    java Geeks

Run the C++ Version

  1. Navigate to the folder:

    cd cpp
  2. Compile:

    g++ main.cpp -o singleton
  3. Run:

    ./singleton

🧠 Learning Objectives

By experimenting with these files, you will learn:

  • How Singleton works in Java and C++
  • How constructors are hidden to prevent multiple instances
  • How static methods provide access to the single object
  • How memory allocation differs between languages (JVM vs manual C++)
  • Why Singleton must block copying (deleted copy constructor in C++)

πŸŽ“ License & Usage

This repository is created purely for educational purposes.

You are encouraged to modify the examples and experiment with alternative Singleton implementations.

About

Educational repository demonstrating the Singleton design pattern in Java and C++. Includes simple, clear implementations for students to explore, compile, and run.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published