Design mode, implement with Delphi ---> Singleton mode

zhaozj2021-02-08  201

Singleton mode

origin

Delphi's Singleton mode is extension based on Singleton. For more information on Singleton mode, please refer to "Design Mode 84"

purpose

Ensure that only one instance is only one instance and provide a global access point to its access, a relatively simple application design mode

motivation

This mode is most important that ensuring a large number of correct access to a single instance. Although a system may have multiple printers, the system only allows only one print cache. Also, for example: a system has only one file system, a form management system. For Delphi's VCL: TAPPLICATION, TSCreen, TSCreen, Tscreen, Tclipboard, which is in contact every day. This mode is better that you can provide a global object for your application at any time. Other uses: Yes, some global abnormal handles, safety control, provide a single access point for cross-trip.

How to keep a class only one instance and establish a good access performance? A global variable guarantees the accessibility of the instance, but has not yet guaranteed the simultaneous possibilities of multiple instances.

A good solution: Establish a class itself to maintain its own instance mechanism. The first instance of the class can save an instance that does not have a class is created (request is mid) when the new instance of the class is created. And provide a way to access classes. This is our Singleton mode, typical application is a class of service types.

application

Assume that there is a TPROGRESSOR for displaying time progress. Categories include two typical methods: StartProgress, EndProgress, Abort, and some common properties such as: Progress, Aborted.

The following code is the interface part of the TProgressor class

Type

TProgressor = Class (TOBJECT)

Private

FPROGRESS: INTEGER;

protected

Procedure setProgress (Value: Integer);

public

»Procedure StartProgress;

»Property Progress: Integer Read FProgress Write setProgress;

END;

The following code is a interface part of the class after the Singleton mode.

Type

TProgressor = Class (TOBJECT)

Private

FPROGRESS: INTEGER;

protected

CONSTRUCTOR CREATEINSTANCE;

Class Function AccessInstance (Request: Integer): TProgressor;

Procedure setProgress (Value: Integer);

public

Constructor crete;

DESTRUCTOR DESTROY; OVERRIDE;

Class Function Instance: TProgressor;

Class Procedure ReleaseInstance;

»Procedure StartProgress;

»Property Progress: Integer Read FProgress Write setProgress;

END;

Introduction to the class interface part:

· Method Class Function Instance Used to access instances of single part of the class. This method is generally accessed in the first time, and instances of the class are created.

• The class constructor is overloaded if you try to build a new instance without passing the instance method will throw an exception.

· Clear an instance of a single class by calling ReleaseInstance. Generally, the regulator can be used when you want to clear the part. Call during Delphi 1 exit, call in unit Finalization in Delphi 2/3/4/5/6. Don't access tprogressor.instance.free to clear instances, let us see the Singleton mode implementation.

Constructor tprogressor.create;

Begin

Inherited Create;

Raise Exception.createfmt ('Access Class% s THROUGH Instance Only ",

[Classname]);

END;

Constructor TProgressor.createInstance;

Begin

Inherited Create;

END;

Destructor TProgressor.DESTROY DESTRUCTOR

Begin

If AccessInstance (0) = Self the AccessInstance (2);

Inherited destroy;

END;

Class Function TProgressor.accessInstance (Request: Integer): TProgressor;

Const Finstance: TProgressor = NIL;

Begin

Case Request of

0:;

1: if not assigned (finstance) the finstance: = createInstance;

2: finstance: = nil;

Else

Raise Exception.createfmt ('Illegal Request% D in AccessInstance ",

[Request]);

END;

Result: = FINSTANCE;

END;

Class Function TProgressor.instance: TProgressor;

Begin

Result: = AccessInstance (1);

END;

Class Procedure TProgressor.ReleaseInstance;

Begin

AccessInstance (0) .free;

END;

Procedure TProgressor.SetProgress (Value: Integer);

Begin

»Join the implementation code

END;

Procedure tprogressor.startprogress;

Begin

»Join the implementation code

END;

The secret of this mode is in the AccessInstance method, and the AccessInstance method uses constant to store instances.

This method must be used because Delphi does not support static class fields. AccessInstance confirms the different return mode according to the request parameters, returns the current instance (Request = 0), create an instance (Request = 1), reset the instance (Request = 2). Delphi 2/3/4/5/6, you must set the compiler $ J to support the form constant (see Delphi Online Help)

Set CreateInstance to Protec, and we ensure that other classes cannot call classes. However, because the constructor itself is a virtual function, it can be overloaded by future generations. In the case, the specific type of the class will be confirmed when the class instance is called for the first time.

The only code that needs you to manually add is: Initialization or Finalization to the Unit to Clear Code. Here is the application example (Delphi1.0)

UNIT Progressortest;

...

...

IMPLEMENTATION

...

...

PROCEDURE SHUTDOWN; FAR;

Begin

»TProgressor.releaseInstance;

END;

Initialization

MMWIN: START Initialization

»AddExitProc (Shutdown);

End.

Specific operation of TProgressor:

Procedure TsomeClass.DOSMETHING;

VAR i: integer;

Begin

»Tprogressor.instance.startprogress;

»For i: = 0 to 100 do

»Begin

»TProgressor.instance.progress: = i;

».. {Complete other work}

»END;

»TProgressor.instance.endprogress;

END;

Delphi instance

Be organized

转载请注明原文地址:https://www.9cbs.com/read-3027.html

New Post(0)