Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether.

Slides:



Advertisements
Similar presentations
Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?
Advertisements

Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
SE2811 Week 7, Class 2 The Gang of Four and more … Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder.
Brief Introduction on Delphi one of the OOP Languages Presented by Wu-hsun Chan.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Façade Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Design Patterns Ref : Chapter 15 Bennett et al. useful groups of collaborating classes that provide a solution to commonly occuring problems. provide.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Vision ePCS Version 2 Palliative Care Guideline The Version 2 Guideline has been redesigned to assist users in the process of entering data for sharing.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Design Patterns Trends and Case Study John Hurst June 2005.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Aniruddha Chakrabarti
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Y2 eProjects Session 4 – Advanced Topics. Objectives  Dynamic Models  Design Patterns (Optional)  Software testing (for S4) ACCP i7.1\Sem3_4\eProject\T4.
Object-Oriented PHP (Chapter 6).
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CSE 432: Design Patterns Introduction What’s a Pattern? What’s an Idiom? According to Alexander, a pattern: –Describes a recurring problem –Describes the.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Introduction General and reusable solutions to common problems in software design SoftUni Team Software University
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Object Oriented Software Development
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Advanced Object-oriented Design Patterns Creational Design Patterns.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Refactoring1 Improving the structure of existing code.
Singleton Pattern Presented By:- Navaneet Kumar ise
Chapter 7 Creational Design Pattern. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed.
Object-Oriented Programming: Inheritance and Polymorphism.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Learners Support Publications Constructors and Destructors.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Introduction To Design Patterns
Presented by Igor Ivković
PH page GoF Singleton p Emanuel Ekstrom.
Overview of Memory Layout in C++
Object Oriented Design Patterns - Creational Patterns
What is Singleton Category: Creational pattern
CS 350 – Software Design Singleton – Chapter 21
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Introduction To Design Patterns
Presented by Igor Ivković
Introduction To Design Patterns
Creating and Using Classes
Presentation transcript:

Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether it be objects in Delphi's VCL such as Application, Screen or Clipboard, or our own constructions for status logging, memory tracking or app-specific requirements. These have come to be generically known as Singletons. The seminal text, "Design Patterns" (aka GOF), describes the intent of the Singleton Pattern thus: "To ensure a class has only one instance, and a global point of access" A cursory search of Delphi resources will reveal several published implementations of the Singleton Pattern. Most strictly follow GOF's generic implementation. I find the more interesting versions stick with the intent of the Pattern, but take advantage of Delphi's features, to produce Singletons which are used like any other class. This session will examine such a beast, TXPSingleton, developed in glorious oblivion of all others...

Yet another Singleton? By The EPC (xpro.com.au) * Slide 2 What's Wrong With Good Ol' Global Variables? Global variables are fine in small one-person projects. As the project grows in size and/or personnel, ensuring the globals are created before being referenced, and not referenced after being destroyed, can become a maintenance headache. Creating and destroying globals in corresponding initialization and finalization sections is not a fail-safe solution. If the globals are referenced in other initialization blocks, it is quite easy to create a situation (via uses clause shuffling) where the global will be invalid when referenced. Accessing globals via a function which conditionally creates the global is safe. This is the essence of the "classic" Singleton implementation. Safely destroying the Singleton is still an issue - an issue which is not covered at all by GOF. John Vlissides, of GOF fame, does cover the topic in a later book, "Pattern Hatching".

Yet another Singleton? By The EPC (xpro.com.au) * Slide 3 TObject Customisation TObject = class... class function NewInstance: TObject; virtual; class function InitInstance( Instance:Pointer): TObject; procedure AfterConstruction; virtual; procedure BeforeDestruction; virtual; procedure FreeInstance; virtual;... end; Allocate memory for instance. Called indirectly as first action of outermost constructor Initialise memory and various structures. Must be called, implicitly or explicitly, by NewInstance. User hook. Empty implementation in TObject. Called indirectly in outermost constructor. User hook. Empty implementation in TObject. Called indirectly in outermost destructor. Release memory and finalise various structures. Called indirectly as last action of outermost destructor.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 4 Object Creation and Destruction Scenarios constructor TMyClass.Create; begin System._ClassCreate asm // If this is the "outermost" // constructor then... // Call to NewInstance entry // in VMT end inherited Create;... // If this is the "outermost" // constructor then... System._AfterConstruction asm // Call to AfterConstruction entry // in VMT end end; destructor TMyClass.Destroy; begin // If this is the "outermost" // destructor then... System._BeforeDestruction; asm // Call to BeforeDestruction entry // in VMT end.... inherited Destroy; // If this is the "outermost" // destructor then... System._ClassDestroy; asm // Call to FreeInstance entry // in VMT end end;

Yet another Singleton? By The EPC (xpro.com.au) * Slide 5 TXPSingleton Features " Suitable as a base class for new or exisiting classes " Implementations based on TObject, TForm and TInterfacedObject. " Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness. " Supports creation of member data in subclasses with minimal additional coding. " Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 6 TXPSingleton Features " Suitable as a base class for new or exisiting classes " Implementations based on TObject, TForm and TInterfacedObject. " Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness. " Supports creation of member data in subclasses with minimal additional coding. " Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 7 TXPSingleton Features " Suitable as a base class for new or exisiting classes " Implementations based on TObject, TForm and TInterfacedObject. " Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness. " Supports creation of member data in subclasses with minimal additional coding. " Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 8 TXPSingleton Features " Suitable as a base class for new or exisiting classes " Implementations based on TObject, TForm and TInterfacedObject. " Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness. " Supports creation of member data in subclasses with minimal additional coding. " Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 9 TXPSingleton Features " Suitable as a base class for new or exisiting classes " Implementations based on TObject, TForm and TInterfacedObject. " Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness. " Supports creation of member data in subclasses with minimal additional coding. " Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Yet another Singleton? By The EPC (xpro.com.au) * Slide 10 Let's Code...

Yet another Singleton? By The EPC (xpro.com.au) * Slide 11 Resources " Contact " Paul Spain, " Slides and source code " Web: " Books " "Design Patterns, Elements of Reusable Object-Oriented Software" by Erich Gamma et al. Addison Wesley. ISBN " "Pattern Hatching, Design Patterns Applied" by John Vlissides. Addison Wesley. ISBN " Other Singleton implementations " and follow- up comments from several people " Delphi Magazine, Issues 41 (Jan 1999) and 44 (April 1999)