Apr 4, 2009

Software Engineering: OO Basics and Principles

OO Basics:
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance


OO Principles:
  • Encapsulate what varies
  • Favor composition over inheritance
  • Program to interfaces, not implementations
  • Strive for loosely-coupled designs between objects that interact
  • Classes should be open for extension but closed for modification
  • Depend on abstraction. Don't depend on concrete classes

Design Patterns: Creational - Singleton

Singleton Pattern:
  • a class manages the creation of its one and only instance through a static variable
  • no Public constructors, constructors are declared as Private
  • a Singleton object is not instantiated using the new() operator but calling a static method to request for an instance
  • may implement "lazy instantiation", if an instance is not needed then it doesn't get created
  • "eager instantiation" may be implemented if the overhead of creation and the runtime aspects of the Singleton is not burdensome
  • Singletons are meant to be used sparingly
Lazy Instantiation:

in Java:
public class Singleton {
private static Singleton uniqueInstance;

private Singleton() {}

public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}


Eager Instantiation:

in C#:
public sealed class Singleton
{
static readonly Singleton uniqueInstance = new Singleton();

Singleton() {}

public static Singleton getInstance {
get { return uniqueInstance; }
}
}

in Java:
public class Singleton {
private static Singleton uniqueInstance = new Singleton();

private Singleton() {}

public static Singleton getInstance() {
return uniqueInstance;
}
}

Mar 30, 2009

C# Notes: Value Types

3 General Value Types:
  1. Struct / User-defined Types
  2. Enum Types
  3. Simple / Built-in Types

Notes on Value Types:

  • contains the actual data instead of just a reference to the data.
  • stored in the stack memory
  • an operation to a copy of a variable doesn't affect the other copies
  • derived implicitly from System.ValueType
  • has an implicit default constructor that initialises the default value
  • may contain a NULL value