Cobol Foro
Navegación en el Foro
Retroceder   Cobol Foro Entornos de desarrollo y compiladores Cobol COBOL - General Object-Oriented COBOL
Object-Oriented COBOL COBOL orientado a objetos. ISO/2002-2014
 
Otros temas que te pueden interesar
Tema Autor Foro Respuestas Último post
Nueva ley Antifraude (Sólo España) Josber Charladero - espacio libre 8 6 de mayo de 2022 15:36
"Aquí no hay quién viva" cumple 15 años Kuk Charladero - espacio libre 0 8 de marzo de 2018 10:36

Respuesta

  #1
Antiguo 22 de febrero de 2017, 13:53
Kuk
Administrador
Última Actividad 19.09.2026 20:11
Posts Posts: 2.500
Likes enviados Enviados: 1037
Likes recibidos Recibidos: 1207
Idea Asegurarse de que hay sólo 1 instancia de clase
0 Inactivo Inactivo

Ejemplo de Eslopes de una única instancia de clase:

Citación del post de Eslopes
Singleton

In traditional Cobol programming all programs are singletons. Well, this a sort of joke, but pretty much basically a procedural program cannot be instantiated. In the other hand OO Cobol classes are by nature just a model to create one, or two, or one billion objects. So, what if our application needs to deal with a single instance of a given class, ensuring that only one instance would exist at anytime? Here comes the GOF Singleton pattern!


As we can see in the diagram above, the class constructor is private, so no one can instantiate that class direclty. To get an instance we need to call the method "GetInstance". That method would check if the private attribute called "instance" is null. If so, the method would then call the class constructor NEW (which is visible for it), set the class attribute "instance" with the new instance, and return the instance to the caller. Next time that method is called it would return the already created class instance.

Too confuse? I thought so
Step-by-step
1.Program (or some object) invokes Singleton.GetInstance static method
2.GetInstance checks if private attribute contains null or a valid Singleton instance
3.If null, GetInstance calls the class constructor "NEW" to create a new instance
4.The new instance is stored in the private attribute "instance" and returned
5.If not null, the instance already created would be returned
6.There can be only ONE



Código COBOL:
  1.  CLASS-ID. Singleton AS "CoolThings.Singleton".
  2.     *> Static data and methods
  3.      static.
  4.          data division.
  5.             working-storage section.
  6.             01 instance usage object reference SINGLETON is private.
  7.          
  8.          procedure division.
  9.              method-id. GetInstance AS "GetInstance".
  10.                   data division.
  11.                       linkage section.
  12.                       01 localSingleton usage object reference SINGLETON.
  13.                      
  14.                   procedure division returning localSingleton.
  15.                       if (instance equal null) then
  16.                             invoke Singleton "NEW" returning localSingleton
  17.                             set    instance  to localSingleton
  18.                       else
  19.                             set    localSingleton    to instance
  20.                       end-if
  21.                      
  22.               end method GetInstance.
  23.       end static.
  24.      *> Instance's data and methods      
  25.       object.
  26.           environment division.
  27.               data division.
  28.                   working-storage section.
  29.                   01   counter pic s9(04) value zeros.
  30.          
  31.           procedure division.
  32.               method-id. NEW is private.
  33.                   data division.
  34.                   procedure division.
  35.                       move zeros   to counter.
  36.                  
  37.               end method NEW.
  38.              
  39.               method-id. GetCounter as "GetCounter".
  40.                   data division.
  41.                       linkage section.
  42.                       01 localCounter  pic s9(04).
  43.                          
  44.               procedure division returning localCounter.
  45.                  add   1       to counter.
  46.                
  47.                  move  counter    to  localCounter.
  48.                  
  49.               end method GetCounter.
  50.              
  51.       end object.        
  52.   END CLASS Singleton.

Testing our singleton

Código COBOL:
  1. program-id. usingSingleton as "usingSingleton".
  2.  
  3. environment division.
  4.      configuration section.
  5.          repository.
  6.              class ClassSingleton as "CoolThings.Singleton".
  7.  
  8.  data division.
  9.      working-storage section.
  10.          01  singletonInstance1  usage object reference ClassSingleton.
  11.          01  singletonInstance2   usage object reference ClassSingleton.
  12.  
  13.  procedure division.
  14.          invoke ClassSingleton "GetInstance" returning singletonInstance1
  15.          invoke ClassSingleton "GetInstance" returning singletonInstance2
  16.          invoke ClassSingleton "GetInstance" returning singletonInstance3
  17.  
  18.          display singletonInstance1::"GetCounter"
  19.          display singletonInstance2::"GetCounter"
  20.          display singletonInstance3::"GetCounter"

Running the code above we would see the following output in the console window.

Código:
 0001
 0002
 0003
Citación del post de Eslopes
In our example, the GetCounter method actually updates the counter and return its new value. First call to GetInstance forces the Singleton to be created and our counter to be initialized with zeros, incremented by 1 and returned. Subsequent calls just increase counter value. Notice that we are not creating the Singleton instance in the code above. That's impossible because the constructor (NEW) is private. All that we can do is to request the instance using the "GetInstance" method.

Where to use Singletons?

You name it! A class that controls the number of database connections? A round robin class that forward the request to a server in a cluster? Overall settings for the application? Possibilities are endless!


Download the source

Cool things with OO Cobol - Source Code

Some references in the web

Singleton .NET Design Pattern in C# and VB - dofactory.com
Singleton pattern - Wikipedia

Miniatura de Adjuntos
Pulsar la imagen para la versión amplia
Nombre:  SingletonUML.gif
Vistas: 46
Tamaño:  1,7 KB  



NORMAS DEL FORO - para garantizar el buen funcionamiento del Foro.
¿Te han ayudado? NO TE OLVIDES de darle a
¿Quieres dirigirte a alguien en tu post? Notifícale haciendo clic en su Nick
Kuk is offline   Responder Con Cita
Respuesta


Herramientas

Derechos de Publicación
No puedes publicar nuevos temas
No puedes publicar posts/responder
No puedes adjuntar archivos
No puedes editar tus posts

BB code is habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado

Saltar a Foro


La franja horaria es GMT +2. Ahora son las 15:45.
Powered by: vBulletin, Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.