1. Trang chủ
  2. » Công Nghệ Thông Tin

Software design: Lecture 30 - Sheraz Pervaiz

20 5 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Software design - Lecture 30. The main topics covered in this chapter include: categories of design patterns; singleton design pattern; application of singleton; possible implementations; definition of singleton design pattern; class diagram; singleton in multithreaded environment;...

1 Software Design Lecture : 30 Categories of Design Patterns Creational Structural Behavioral Singleton Design Pattern  Sometimes,  there  may  be  a  need  to  have  one  and  only  one  instance  of  a  given  class  during  the  lifetime  of  an  application  This  may  be  due to  necessity  or,  more  often, due  to  the  fact that only a single instance of the class is sufficient  For  example  maintaining  single  database  connection,  registry values etc Application of Singleton In  environments  where  resources  are  rare,  you  may need to restrict how many counted instances  of a class are created Possible Implementations  Using Global Variables i Having an instance of the class in a global variable seems  like an easy way to maintain the single instance ii. All client objects can access this instance in a consistent  manner through this global variable.  • 6 Problem in the approach  This  approach does not prevent clients from creating  other instances of the class  For this approach to be successful, all of the client objects  have to be responsible for controlling the number of  instances of the class  This is  not desirable since client classes should be free  from  class creation process Solution  The responsibility for making sure that there is only one  instance  of  the  class  should  belong  to  the  class  itself,  leaving  client  objects  free  from  having  to  handle  these  details 8 Definition of Singleton Design Pattern Singleton Design Pattern ensures that there is  only  one  instance  of  a  class  and  provides  global point of access to it 9 Class Diagram 10 11 Sample Java Code 12 public class Singleton  { private static Singleton uniqueins; private Singleton() // Private Constructor {} public static Singleton getInstance() // Class Method { if (uniqueins==null) { uniqueins= new Singleton(); } return uniqueins; }} 13  class testsingleton {  public static void main(String args[]) { Singleton.getInstance(); // call to static method } } 14 Singleton in Multithreaded Environment There  is  a  problem  with  the  code  above  in  a  multithreaded  environment.  Two  threads  might  get a hold of two different instances if they enter  at same time in the getInstance method 15  Multithreaded Safe Code 16 public class Singleton { private static Singleton uniqueinst; private Singleton() {} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; }} 17 Problem with Solution  Synchronization decreases performance by factor of 100 !!!  This solution fixes our problem but it is very expensive   Indeed we only need synchronization for the first call. After  that synchronization is totally unneeded.  18 Solution to Problem  Double­Checked Locking i With this solution, we first check to see if an instance is created and  only then we synchronize.  ii This  way  we  only  synchronize  the  first  call  and  there's  no  performance issue anymore 19 Code with Double Checked Locking 20 public  class Singleton {     private volatile static Singleton instance;         private Singleton() {}     public static Singleton getInstance() {       if (instance == null) {           synchronized(Singleton.class) {              if (instance == null) {                 instance = new Singleton();              }}}        return instance;     }}

Ngày đăng: 05/07/2022, 14:05

Xem thêm:

Mục lục

    Categories of Design Patterns

    Problem in the approach

    Definition of Singleton Design Pattern

    Singleton in Multithreaded Environment

    Code with Double Checked Locking

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN