A simple, thread-safe wrapper class for lazy-loading data into an instance on-demand (i.e. when the instance is first accessed.) Usage: LazyLoader l = new LazyLoader(() => Foo.LoadFromDataSource(“DB Connection String”)); // For access to the lazy-loaded instance: var x = l.Instance.Bar; // Need to ensure that the data is loaded deterministically? Use this: l.EnsureLoad();
Tag Archives: instance
C# allows you to overload certain operators. The figure below shows a table of overloadable operators. Suppose there is a BankAccount class which contains a Number property and a Balance property. The + (addition) operator can be overloaded to combine two BankAccount instances and to return a new BankAccount instance with the combined balances. This [...]
The state pattern is a software design pattern to represent the state of an object. It allows an object to alter its behavior when its state changes. It shows a similarity to the strategy pattern.
This example is similar to the Inheritance in PHP snippet. Python is used instead of PHP.
C++ can be used for object-oriented programming. This example shows you how to define a class and a header file belonging to it.
Classes can contain subclasses and subclasses can in turn contain superclasses. For example, the class Person could be the superclass of the class PizzaDeliveryGuy.
You might want to execute code when an instance of an object gets constructed or destructed. PHP offers constructors and destructors for this purpose. A constructor can be created by declaring a function called __construct within a class and a destructor can in turn be created by declaring a function called __destruct. <?php class Foo [...]