Direkt zum Hauptbereich

Posts

Posts mit dem Label "Patterns" werden angezeigt.

Introduction to Design Patterns (2): The Singleton

The Singleton The Singleton design pattern can be seen as a special case of a Lazy Getter . Indeed, the Lazy Getter is the basic construct of a Singleton. Additional, the Singleton does not use a member variable, but a global (static) variable, so that any call to the class would return the same variable. Why is this useful? Just imagine, You are developing an e-mail application with an address book function. Hence an address book can contain a lot of contacs, stored somehow in a data file that maybe need to be loaded by an SQLite database, this can take a few moments on initial data access. So when a user opens the address book the first time, the Laze Gette will cteate the address book object and load the contacts from SQLite. Now it is not that difficult to imagine, that the user may opens more than one e-mail window and writes some e-mails in those windows and every e-mail window is using the address book. To avoid multiple timespans for initially loading the contacts for each

Introduction to Design Patterns (1): The Lazy Getter

The Laze Getter pattern Example for Java Example for Lazarus and language hint The term "Lazy Getter" refers to the Java vocabular, where a Getter is a method, that returns a feedback value or object. And lazy means, that the effect of this Getter method is somehow delayed. Some more precise, it is delayed until one need it. But what do we need? An object, of course! So we need an instantiation of an object delayed. or maybe we don't need this object at all and this is, why we are using a Lazy Getter. To save resources, it is inevitable to not create the maybe required object at startup. But of course we need to create it, when it is needed. To reach this, the Lazy Getter method is used as a synonyme for the late created object in the source code. This method does nothing else than looking, if the object is created - if yes, then return it - or not - then create and return it. That's all. A simple basic pattern. Example for Java: // somewhere you have de