Code Monkey home page Code Monkey logo

singletonexample's Introduction

Singleton

Singleton is the simplest design pattern. The concept is you only use 1 instance of class in different object. It means that you are not going to create a new object of the class continuously in different context. So this code below will be not used :

Animal animal = new Animal();

Instead, we use this :

Animal animal = Animal.getInstance();

static method getInstance() declared in the Animal Class :

public class Animal{
    private static Animal instance;
  
    public static getInstance(){
        if (instance == null){
            instance = new Animal();
        }
        return instance;
    }
}

getInstance() method will check whether the private object "instance" is null or not. If the instance" is null, then we create a new object and save it to "instance", otherwise we just return the instance. We only create a new object once for all use. It will reduce your memory allocation because we only use one source of the instance.
"Singleton can act as global variable". Yes, the purpose of using singleton in our project is to make the certain class act as the global variable. For example, when we want to use Calendar class that have a static behavior. Rather than we create a new Calendar object continuously in different use, its better to only create one object and use it when we need it.

singletonexample's People

Contributors

jonathandarwinedu avatar jonathandarwin avatar

Watchers

James Cloos avatar

singletonexample's Issues

Thread Safe

The static function is not thread safe. Consider to use synchronized keyword

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.