Saturday, December 18, 2010

Chain of responsibility

 
http://nchain.sourceforge.net/
class CheckAuthority {
 
    public static void main(String[] args) {
        ManagerPPower manager = new ManagerPPower();
        DirectorPPower director = new DirectorPPower();
        VicePresidentPPower vp = new VicePresidentPPower();
        PresidentPPower president = new PresidentPPower();
        manager.setSuccessor(director);
        director.setSuccessor(vp);
        vp.setSuccessor(president);
 
        //enter ctrl+c to kill.
        try{
            while (true) {
                System.out.println("Enter the amount to check who should approve your expenditure.");
                System.out.print(">");
                double d = Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine());
                manager.processRequest(new PurchaseRequest(0, d, "General"));
           }
        }
        catch(Exception e){
            System.exit(1);
        }  
    }
 
}

Generator

In short, a generator <I>looks like</I> a function but <I>behaves like</I> an iterator.
 
public static IEnumerable<int> GetEven(IEnumerable<int> numbers)
{
    foreach (int i in numbers)
    {
        if ((i % 2) == 0)
        {
            yield return i;
        }
    }
}

Lazy evaluation

The benefits of lazy evaluation include: performance increases due to avoiding unnecessary calculations, avoiding error conditions in the evaluation of compound expressions, the capability of constructing potentially infinite data structures, and the capability of defining control structures as abstractions instead of as primitives.

In computer windowing systems, the painting of information to the screen is driven by "expose events" which drive the display code at the last possible moment. By doing this, they avoid the computation of unnecessary display content

The opposite of lazy actions is eager evaluation, sometimes known as <I>strict evaluation</I>. Eager evaluation is the evaluation behavior used in most programming languages.

Lazy evaluation refers to how expressions are evaluated when they are passed as arguments to functions and entails the following three points:

Private class data pattern

 public class Circle
 {
   private double radius;
   private Color color;
   private Point origin;
   public Circle(double radius, Color color, Point origin)
   {
     this.radius = radius;
     this.color = color;
     this.origin = origin;
   }
   public double Circumference
   {
     get { return 2 * Math.PI * this.radius; }
   }
   public double Diameter
   {
     get { return 2 * this.radius; }
   }
   public void Draw(Graphics graphics)
   {
     //...
   }
 }