home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / singleton < prev    next >
Text File  |  2020-01-01  |  3KB  |  94 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Date: Wed, 23 Jun 1999 20:38:06 +0000
  3. ; Subject: Incoming Script - singleton.ksc
  4. ; URL: http://www.smalltickle.com
  5. ;
  6. ; IN THE PATTERNS COMMUNITY, THE SINGLETON IS A CLASS THAT CAN
  7. ; HAVE ONLY ONE INSTANCE.  ALL OBJECTS INSTANTIATED FROM THAT
  8. ; CLASS REFER TO THE ONE AND ONLY SINGLETON!
  9. ;
  10. ; THE SINGLETON IS VERY USEFUL WHERE THERE IS ONLY ONE RESOURCE
  11. ; AVAILABLE AND VARIOUS USER-DEFINED FUNCTIONS ACCESS THAT
  12. ; RESOURCE UNDER DIFFERENT REFFERENCES.
  13. ;
  14. ; THE SINGLETON CLASS ENSURES THAT ONE AND ONLY ONE OBJECT CAN BE
  15. ; INSTANTIATED FROM IT, THOUGH UNDER DIFFERENT NAMES.
  16. ; THE FOLLOWING SCRIPT DEFINES A SINGLETON CLASS.
  17.  
  18. ;********************************************************
  19. ;    DEFINITION OF THE SINGLETON CLASS                  *
  20. ;********************************************************
  21. define singleton {
  22.     if NOT define \m(singleton::singleton) {
  23.     assign singleton::singleton 0
  24.     }
  25.  
  26.     _define \%1 {
  27.         singleton::\%1 \v(macro)
  28.     }
  29. }
  30.  
  31. ;******************************************************
  32. ;    IMPLEMENTATION OF THE PUBLIC USAGE INTERFACE     *
  33. ;    OF THE SEMAPHORE CLASS                           *
  34. ;******************************************************
  35.  
  36. ; Smalltalk syntax
  37. define singleton::up {
  38.     assign singleton::singleton \feval(\m(singleton::singleton) + 1)
  39.     echo   \m(singleton::singleton)
  40. }
  41.  
  42. define singleton::down {
  43.     assign singleton::singleton \feval(\m(singleton::singleton) - 1)
  44.     echo   \m(singleton::singleton)
  45. }
  46.  
  47. define singleton::query {
  48.     echo   \m(singleton::singleton)
  49.     return \m(singleton::singleton)
  50. }
  51.  
  52. ; C++ syntax
  53. define singleton::++ {
  54.     assign singleton::singleton \feval(\m(singleton::singleton) + 1)
  55.     echo \m(singleton::singleton)
  56. }
  57.  
  58. define singleton::-- {
  59.     assign singleton::singleton \feval(\m(singleton::singleton) - 1)
  60.     echo \m(singleton::singleton)
  61. }
  62.  
  63. ;********************************************************
  64. ;    USAGE SAMPLES                    *
  65. ;********************************************************
  66.  
  67. take singleton.ksc
  68.  
  69. ; In a user-defined function:
  70. singleton printer            ; create a singleton for printer
  71.                     ; there is only one printer !!!
  72.  
  73. if = \fexec(printer query) 0 {        ; if printer is available
  74.     printer up                    ; register for printer
  75.     do_some_processing            ; 
  76.     printer down            ; release printer
  77. }
  78. ;
  79. ;
  80. ; In another user-defined function
  81. singleton my_printer            ; refer always to the same singleton
  82.                                         ; as the printer above
  83. if = \fexec(my_printer query) 0 {    ; if printer is available
  84.     my_printer up            : Register for printer
  85.     do_some_processing        ; 
  86.     my_printer down            ; release printer
  87. }
  88.  
  89. ; EXPERIMENT
  90. ;
  91. for \%i 1 10 1 { printer up }
  92. printer query            ; 'printer query' and 'my_printer query'
  93. my_printer query        ; produce the same result, that's singleton
  94.