Thursday, May 29, 2008

UML and some stuff

Here are some useful links for UML class diagrams
http://www.objectmentor.com/resources/articles/umlClassDiagrams.pdf
http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell/


And a book, thanks to Asmaa Magdi ACMaa
http://portal.aauj.edu/portal_resources/downloads/uml/sybex_mastering_uml_with_rational_rose2002.pdf


The difference between Aggregation and Association


Aggregation is when an object contains a collection of other object. This is done mainly via data structures. Like a class that contains an Array, a List, a Dictionary, a HashTable, ...


Association is when objects/classes (sub-systems) interact together in a larger system. This can be achieved either by letting the objects have references to other objects in the system (this involves aggregation), by using static classes, ...


Examples,


  1. In Asp.Net, the use of static classes is obvious. You have the MembershipProvider, the RuleProvider, the ConfigurationManager, ... which provide services (functionality) to other objects/classes.
  2. Suppose you're designing a strategy game where soldier must know the location of other soldiers. So you will make a list of references to other soldiers in the soldier class.

Object Composition at wikipedia


This file contains a sample C# code and it's simple UML class diagram (in both Word 97-2003 and Word 2007 formats)

Wednesday, May 21, 2008

Constructors and Destructors

Constructors are special functions that are executed to initialize the object's data members at creation.
Constructors have the same name of the class and do not have a return type.


class Employee

    public Employee()
    {
    }
}


Constructor overloading


You can have more than one constructor with different parameter signature i.e. different number of parameters or different datatype arrangement.


class Employee
{
    public Employee()
    {
    }

    public Employee(string employeeName)
    {
    }

    public Employee(string employeeName, decimal salary)
    {
    }

    public Employee(decimal salary)
    {
    }
}


The default constructors


If you haven't defined ANY constructor in a non-static or non-abstract class, the compiler creates for you a public constructor with no parameter.


Structs do not accept explicit parameterless constructors although they have one by default. All data members of the struct must be explicitly assigned to value.


Base constructors


class Person
{
    public Person()
    {
    }

    public Person(string name)
    {
    }
}

class Employee : Person
{
    public Employee()
    {
    }
}


Let's talk philosophy. You cannot have a glass of water before it has been a glass first. You cannot have an employee before he/she has been a person first.
So, in order to create an object as an Employee you have to create it first as a Person. Let's see code.


new Employee();
When this statement get executed; if you don't explicitly specify which base constructor to use, the default constructor will get executed if exist; otherwise, you will get a compiler error.


How to explicitly specify the base class constructor?
Simply you use the base keyword as follows.


class Employee : Person
{
    public Employee() : base("No name")
    {
        // you can here write employee specific initialization
    }
}


You can also pass parameters from the current constructor to the base constructor as follows.


class Employee : Person
{
    public Employee(string employeeName) : base(employeeName)
    {
    }
}


Actually, the concept of base constructors is not bound only to base class's constructors. It extends to current class's constructors.


The concept is simply you define a constructor to construct the object before it get constructed by the current constructor being executed.


You can make a constructor call another constructor in the same class as its base constructor using the this keyword. You can then make a chain of constructors.


class Emplyee : Person
{
    public Employee() : this(0)
    {
    }

    public Employee(decimal salary) : base("No name")
    {
    }
}


Static constructors


A static constructor gets executed directly before you first create an object of the class or access any of its static members. In fact it gets executed when loading the class definition in memory.


Static constructors increase performance with the fewest code. Another technique would be creating a "dirty-bit" to indicate whether the class has ever been used or not and keeping checking it in each constructor and static method and static property (note that this technique doesn't work with static fields of course).


Static constructors don't accept access modifiers or parameters, thus you will have only one static constructor per class/struct (no overloading). And you cannot call static constructors directly.


How constructors work?


  1. What happens when you execute something like
    new Employee("abc");
    You use the new keyword in constructor context so the runtime knows you want to create object of a class.
  2. Then the runtime gets which type you want the object to be, here we want an Employee.
  3. The runtime allocates a location in memory which will hold the data and assign them to their default values.
  4. The runtime gets the constructor that matches the parameters you provide.
  5. The runtime executes the base constructor of the current constructor (if not specified, the default constructor is executed).
  6. Then the code of the current constructor gets executed.
  7. Then you get the reference to the object you've just created.

Keeping in mind that static constructors get executed at the first use of the class/struct.


Constructors and access modifiers


With the exception of static constructors and struct's constructors, you can define constructors with any access modifier (public, protected, internal, internal protected, private), see the Access modifiers post.
You make a protected constructor to use it as a base constructor of other constructors in derived classes. You can not make protected constructors in structs because there is no inheritance in structs.
You can do something like that.


class Employee
{
    private Employee() // a constructor to be used only by this class
    {
    }

    protected Employee(string employeeName) // to be used by derived classes
    {
    }
}


Destructors


They are functions that get executed when the Garbage Collector collects objects. It's called automatically by the GC so you don't have control over when they get executed.


Destructors are used as cleanup functions you use to free up resources used by the object.


class Employee
{
    ~Employee()
    {
        // here, you can free up resources that were used by the instance (Streams, Brushes, ...)
    }
}


Destructors are parameterless and have no return datatype. You can have only one destructor per class.


Another way to do this is using the IDisposable's Dispose() thus you may use the using statement. This is the recommended way to free up resources.


See destructors at the MSDN here.



A simple demo of constructors and destructors

Sunday, May 11, 2008

Access modifiers

.Net assemblies


Before we dive into access modifiers we need first to define .Net assemblies. A .Net assembly is an IL (Intermediate Language) code library, this definition includes both process assemblies (exe) and library assemblies (dll).


Access modifiers are keywords used to specify the declared accessibility of a member or a type. The significance of access modifiers comes from their use of defining a type's interfaces (the parts of the type exposed to other types, see the Encapsulation post). A type can be a class, a struct, an interface ... . Access modifiers keywords are:

  • private
  • protected
  • public
  • internal
C# keywordMeaning
privateaccessible only to this type
protectedaccessible only to this type and derived types (even outside the assembly)
internalaccessible to all classes within the same assembly
protected internalaccessible only to this type and derived types within the assembly
publicunrestricted


The private keyword

A member marked with the private keyword means it's only accessible to this type. Only other members of the type can access it.
The private keyword is used to hide both the data and the methods of the inner implementation of the type.


The protected keyword

A member marked with the protected keyword means it's accessible only to this type and derived types (inside or outside the assembly). The private keyword shapes the protected interface of the type (the parts of the type that is exposed to derived types).


class A
{
    protected int x;
}

class B : A
{
    // x is visible here because it's a protected member of the base class. x is still protected and it became a member of this class
}

class C : B
{
    // x is visible here because it's a protected member of the base class. x here is still protected
}

class D // doesn't inherit from any any class
{
    A ref_a = new A();

    // ref_a.x is not accessible from here because D doesn't inherits from A, B or C
}


The internal keyword

A member marked with the internal keyword means it's accessible to all other types located only in the same assembly. If you reference this assembly from somewhere else, you wont be able to access internal or protected internal members because they are public or protected at the assembly level.


protected internal

A member marked with the protected internal combination means it's both protected AND internal that is it's accessible only to derived types located only in the same assembly.


The public keyword

A member marked with the public keyword means it's accessible to any other type (inside or outside the assembly).
The public keyword is used to define the public interface of the type (the parts of the type that is exposed to any other type).


Nested types


A nested class is a class that is defined within another class. You sometimes nest classes to organize them or to expose the outer class's private and protected members to the nested class. A nested class can be thought of as a member of the containing class although it's not. What I mean is you can have a private class inside another class so this nested class is only accessible to the class containing it.


class A
{
    private int x;
    ...
    class B
    {
        // we can access A's x from here. in fact, we can access all the members of A
    }
    ...
}


You access nested classes the way you access members. you write A.B to access the class B from out side the class A if the class B was part of the public interface or the protected interface.


You can not make the outer class inherit from any of its inner classes. You can not do something like class A : A.B


Remarks


  • The default access level of classes and structs is private and the default access level of enums and interfaces is public.

  • Once you declare a member with a specific access level you cannot change its access level in derived types.

  • There are some restrictions on the use of access modifiers. For example, you can not have a private class A and a public class B that inherits A. Because the base class must be at least as accessible as the child class otherwise it wouldn't make any sense. The same rule is applied with function return types and function parameters. You can find more about Restrictions on Using Accessibility Levels here.

  • Members of enums and interfaces can not be marked with access modifiers. While the members of a struct can be marked only with public, internal or private.



Again this codeproject article by Marc Clifton.
And access modifiers at the MSDN.

I want to thank all my teachers and everyone who support me specially my teacher Mohamed Samy for his efforts and vision during the preparation of the previous couple posts.