Monday, January 23, 2012

Liskov Substitution Principle, a simple example

The Liskov Substitution Principle of object oriented design states:

In class hierarchies, it should be possible to treat a specialized object as if it were a base class object.


using System;

namespace Liskov
{
 class Rectangle
 {    
  int _height;
  public virtual int Height
  {
   get{
    return _height;
   }
   set {
    _height = value;
   }
  }
  
  int _width;
  public virtual int Width
  {
   get {
    return _width;
   }
   set {
    _width = value;
   }   
  }
  
  public int Area
  {
   get {
    return _width * _height;
   }
  }
  
  
  public Rectangle (int width, int height)
  {
   Height = height;
   Width = width;
  }
  
 }
 
 
 class Square : Rectangle
 {
  public override int Height {
   set {
    base.Height = value;
    base.Width = value;
   }
  }
  
  public override int Width {
   set {
    base.Width = value;
    base.Height = value;
   }
  }
  

  public Square (int side) : base(width: side, height: side)
  {
  }
  
 }
 
 class MainClass
 {
  public static void Main (string[] args)
  {
   Rectangle s = new Square(side: 10);
   
   // 100, correct
   Console.WriteLine ("Area: {0}", s.Area); 
   
   // reusing the object r as rectangle object     
   s.Height = 4; 
   s.Width = 3;
   
   // expecting 12, actual output is 9. incorrect. Liskov Substitution Principle is violated
   Console.WriteLine ("Area: {0}", s.Area); 
  }
 }
}

The code above violates Liskov Substitution Principle, there should be no side effects on results even I use Square object for Rectangle purposes. Don't override properties just because it look smart to do so.

To set the Square's sides properly, set the two sides via one property only:

public int Side {
 get {
  return Height;
 }
 set {
  Height = Width = value;
 }
}



Web Sharing not working with mod_mono

If Web Sharing is not working after you upgraded to Lion, and you happen to configured ASP.NET Mono on your Snow Leopard before. Just do these 5 steps:

1. download the latest mod_mono Apache module

2. extract the content of the archive

3. do these on Terminal:

./configure
make
sudo make install

4. Click Web Sharing

5. No step five. Voila! Happy .NET computing on Mac and Mono again


No tedious steps taken, if the above steps doesn't work for you. Try this:

http://stackoverflow.com/questions/8815403/trouble-installing-mod-mono-on-mac-osx-lion


How to rule out that it is not a problem on Apache per se (most articles explain how to fix Apache itself) ? Use sudo apachectl configtest. Using that, I saw error message on httpd.conf regarding Mono, that makes googling easier for me, I narrowed down my search, and arrived on stackoverflow's solution.

P.S. I forgot to copy the exact mod_mono error on httpd.conf, it's something like: syntax error on httpd.conf dlopen mod_mono.so