Tag Archives: instance

(C#) LazyLoader – a simple, thread-safe class that can be used to load data on demand

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();

Posted in C# | Also tagged , , , , , , , , | Leave a comment

Operator overloading in C#

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 [...]

Posted in C# | Also tagged , , , , , , , , | Leave a comment

Exception handling in PHP

PHP5 supports exception handling with try and catch blocks. This can be used to notify the user of any exceptions. This example uses a modified version of the Person class of the Inheritance in PHP example to throw an exception if an invalid age has been supplied. The catch block is used to catch the [...]

Posted in PHP | Also tagged , , , , , , , | Leave a comment

State design pattern in C#

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.

Posted in C# | Also tagged , , , , , , , , , , , , | Leave a comment

Classes and inheritance in Python

This example is similar to the Inheritance in PHP snippet. Python is used instead of PHP.

Posted in Python | Also tagged , , , , , , , | Leave a comment

Classes in C++

C++ can be used for object-oriented programming. This example shows you how to define a class and a header file belonging to it.

Posted in C++ | Also tagged , , , , , , , | Leave a comment

Inheritance in PHP

Classes can contain subclasses and subclasses can in turn contain superclasses. For example, the class Person could be the superclass of the class PizzaDeliveryGuy.

Posted in PHP | Also tagged , , , , , , , , , , , | Leave a comment

Constructors and destructors in PHP

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 [...]

Posted in PHP | Also tagged , , , , , , | Leave a comment