4Р(Singleton), Exercises of Object Oriented Programming

(Participants). SingleTon. 4 Instance. P 4 PInstance 4. C++ = 1 Static member function 2000/12/14. D R 4. C(Collaborations). P Db 4 Instance.

Typology: Exercises

2022/2023

Uploaded on 02/28/2023

houhou
houhou 🇺🇸

4

(7)

265 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
(Singleton)
(Intent)

(Motivation)
 !"#$% &'()*
+ %,-./012 34567892
:; <=>?@AB
CDE 5CDFGHIJ%
,0KL+MNO'P QRSTUVWX Delphi
YZ[!,IJK\] ^_`abcdFe
!fghij\]QRklm!noQR
FNSTUV Delphi pM C++qrFN(Static)L<sqrF
N!FN class functiond +!FNMN privated I
JLtuvw$/0IJFea
(Applicability)
 Lxyz{|
}~yzL/0yz /0
Me
(Structure)







pf3
pf4
pf5

Partial preview of the text

Download 4Р(Singleton) and more Exercises Object Oriented Programming in PDF only on Docsity!

 (Singleton)

 (Intent)

 (Motivation)

:; <=>?@AB

CDE  5CDFGHIJ%

,0K L+MNO' P QRSTUVWX Delphi Y Z[!,IJK]  ^_`abcdFe !fghij]QR klm!no QR FNSTUV Delphi pM C++qrFN(Static)L<sqrF N!FN class functiond +!FNMN  privated I JLtuvw$/0IJFea

 (Applicability)

  Lxyz{| }~  yz€L/0‚yz ƒ„ /0€ †M‡ˆ‰Š‹eŒ

 (Structure)

(Participants)

SingleTon Ž Instance uE/0‚~ Instance ! u‘C++’<qr“”FN Static member function•2000/12/ % –—_`i˜™  

 (Collaborations)

/0‚ 𛐠 Instance u 

 (consequences)

 œ!V

  1.  p<žeŸ   ^ _`Lp¡%,¢£]/0‚CD5Dk¤
  2.  %,‰¥IJ/0
  3.  <že%,¦§†x%,Š€  FG¨r©0 †x‡$kªj«¬%,¨r© 0 
  4. ! %,®¯°QRJ± J±L,5]©0 %QR J±C¡‡$ˆ ‰!²!³& uSTUV'že L +P´'μ¡J±a
  5. "#$%&'( ¶·Fe¸;že!/0u ‘C++qr¹J Static function•L+!~·FeMNuJ±  L¡º C++qr¹JP !»¼(virtual)p¡MN ‰½

 (Implementation)

1. )* !V$¾¿  ^_`À/

0FN!Á¹JL†f¹JÃÄL~¹JÅÆ–—_ ºÇk È~¹J] IJ~ IJÉ ÊËÌÍyzÎ~ ÏÐÑÒ~Fe%, že /0Í%,^`5ÐÑÒSTUVDelphi  create ¹JM NÁÂLÓ,yz create ¹J uÔ a

TSingleton = class public constructor Create; virtual; destructor Destroy; override; end; var Glob_Singleton1: TSingleton;//

implementation

constructor TSingleton.Create; begin if Glob_Singleton1 <> nil then abort else Glob_Singleton1 := Self; end;

destructor TSingleton.Destroy; begin if Glob_Singleton1 = self then Glob_Singleton1 := nil; inherited Destroy; end; Delphi /0IJ Glob_Singleton1 à_ Singleton  , È†xP !~Çk Ó Ÿ ¦§¡ Tsingleton Ÿ  %,^_}Ó Ÿ  ÈÚyz‰½ Create ¹JijæIJP› C¡ ÈŠ/0 ºIJM ²o ÈÆ¶·89WXa

  1. +,-#$V$P! ¦§ †!¡ Singleton E/0‚/0 /žeCDÐÑÒù!°  Instance() °$/0 Singleton ‘Y‹e %,® !IJ°_`🕶·Fe!"#u$ æ Instance()†%& ²Ž Instance()~yz link-timt ²°‘ C'( PÇu •)/0‚ÁÂÈ+C¡ªj «²MN"#/0*e°!bc+ÈFe+!yz,

(hard-wires)¨%  singleton Ó,- ·Fe!.ƒ+

È·bc+ÈFe!/0(registry of singleton)singleton  %,Š /0¿{|/0) ~/0)bŠ/01’ 5 singleton  2 Instance()‡$ singleton k%å/0)Ù 3 L /0)Ù 3 gh/4}M¡ singleton ÚË 5 %,~·Fe %,E Instance()M‡| 6 Ó %  singleton 1 C¡ Ӈ$ !Ó singleton %,/0789:‘Yã ;• Register()/0Š 1 ’μ_registery  NameSingletonPair  Lookup() , 1 ’<=bӇ$ singleton Çk>¸  !IJ°Ó ‡$ singleton  1 ’‘Yã;• ?μ singleton Dk/0ŠghL% !Š_¹J ‘Y ã;•p<¹J _kß:^õö?μ~ Z[%,Ž qr Instance ¹JC MySingletonC¡ singleton P–—_singleton L†ÄðÉ@—!° singleton  %n +~¬ ABV²!%  singleton  yz^_n Ú¤MN^/0 STUV C Delphi uYCW interface uses classes,SysUtils;

type TSingletonList = class(TList);

TSingletonl = class public constructor Create;virtual; destructor Destroy; override; protected function Lookup(PSingleton: TSingletonl): boolean; end;

TChildSingletonl = class(TSingletonl);

var Glob_Singletonlist: TSingletonList; //

implementation

L Lookup()¹JbÓ  1 ’‘plSingleton.ClassName = PSingleton.ClassName•tD/¦§ TSingleton ‘TChildSingletonl = class(TSingletonl)•P8&EFĬ ŠîĐÈ}$P8F ÄG  HIJvKY%a

 (Sample Code)

vX‹eŒ

 (Known uses)

L

 (Related Patterns)

&'že%, Singleton uCMNOP(Abstract Factory)Q`¸ (Builder)QR(Prototype)2000/12/ http://home.kimo.com.tw/areca_chen/