Loading, please wait ...
Open its course - Design Patterns
Singleton By Mukhtiar Zamin
Summary

Summary

2
Teachers with
Mukhtiar Zamin
178
Followers
For Learning
All
Visibility
Security Status
3
Contributions
By Teachers
Notes
  1. Students are auto-evaluated against related topics to ensure they learned it.

This lectures covers singleton design pattern.

Watch the interactive learning videos (Requires to be login). We also covered the following two concepts: 

  • Thread Safe Code (Demonstrated in recording of Configuration example)
  • Static Vs Dynamic Context of Object and their calling constraints (Recorded in the following video)

Singleton Pattern Code Example and Explanation for All, By Ali hassan

HTML 5 Boilerplate

Singleton.java

    
       /**
 * Singleton class implements singleton pattern. Only one object can be
 * instantiated.
 * 
 */
public class Singleton {

  /**
   * Holds reference to single instance.
   */
  private static Singleton INSTANCE;

  /**
   * Overrides public Constructor.
   */
  private Singleton() {
  }

  /**
   * Creates the instance if it does not yet exist(lazy instantiation).
   * 
   * @return a reference to the single instance.
   */
  public static Singleton getInstance() {
    if (INSTANCE == null) {
      INSTANCE = new Singleton();
    }
    return INSTANCE;
  }
}

    

SingletonTest.java

    
       import static org.junit.Assert.assertEquals;
       import static org.junit.Assert.assertNotNull;
       import org.junit.Test;

/**
 * Test implementation of the Singleton pattern.
 */
public class SingletonTest {

  @Test
  public void testSingleton() {

    // invokes Singleton.getInstance() for first time,
    // object will be created
    Singleton singleton = Singleton.getInstance();
    assertNotNull(singleton);

    // invokes Singleton.getInstance() for second time,
    // reference to the same object will be returned
    Singleton secondSingleton = Singleton.getInstance();
    assertEquals(singleton, secondSingleton);
  }
}

    

Lecture Slides for All, By Mukhtiar Zamin

Lab Tasks for All, By Mukhtiar Zamin

Available in the diary and class teacher will have to enable it against the class.

×

Singleton Evaluation

To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.

  • CAREFULLY READ THE FOLLOWING INSTRUCTIONS!
  • Make sure you have learned this lecture along with its topics and tutorials.
  • On first evaluation you get 100% marks on correct answer, then on 2nd you get 95% marks and so on.
  • Answer the questions with a gap NOT MORE THAN A MINUTE, Otherwise it will re-start.

I have read above instrucitons and ready for evaluation.
Your's
Status
Not Registered
Teacher
Mukhtiar Zamin
Alert
Your performance monitoring will start when you register in a class of this course.

Questions on

Contact Us

support@subexpert.com
Write to Us View Help
Subject Expert Logo

Subject Expert

Learn, Evaluate and Optimize

Follow Us
Facebook Switch Display Mode Enable Translation
© 2024 - Subject Expert