1 / 14

Singleton and Basic UML

Singleton and Basic UML. CS340100, NTHU Yoshi. What is UML. Unified Modeling Language A standardized general-purpose modeling language in the field of software engineering UML includes a set of graphical notation techniques to create  visual models  of software-intensive systems.

jill
Download Presentation

Singleton and Basic UML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Singleton and Basic UML CS340100, NTHU Yoshi

  2. What is UML • Unified Modeling Language • A standardized general-purpose modeling language in the field of software engineering • UML includes a set of graphical notation techniques to create visual models of software-intensive systems.

  3. Diagrams Overview

  4. Basic Class Diagram • Accessibility • States and behaviors • Association • Aggregation • Composition

  5. Class

  6. Association

  7. Composition and Aggregation

  8. Design Pattern - Singleton • We have learnt • Accessibility • A method or a constructor can be set as private accessibility, i.e., can only be used in the same class • While a constructor has been set as private, how to use it? i.e., how to create such an object? Is it useful? class Example { … private Example() { //empty constructor } … }

  9. Design Pattern – Singleton (cont’d) public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor prevents instantiation from other classes private Singleton() {} public static Singleton getInstance() { return INSTANCE; } }

  10. When to Use? • In system design, the object should have only one copy • Any example?

  11. Lazy Initialization • Create an instance when you really need it • Can you give me an example which has the idea of lazy initialization?

  12. Check the Code public class Singleton { private static final Singleton INSTANCE; private Singleton() {} public static Singleton getInstance() { if( INSTANCE == null ) { INSTANCE = new Singleton(); } return INSTANCE; } } Any Bug?

  13. Race Condition • The race condition may result in two copies of a singleton object • Disobey the definition of the singleton pattern How to cause race condition? Try to write it down!

  14. Solutions • By synchronization • Review your OS textbook! • Throw lazy initialization away, use eager initialization

More Related