Monday, June 7, 2010

Strategy Pattern

defines a family of algorithms, encapsulates each one, and makes them interchangeable.Strategy lets the algorithm vary independently from clients that use it.

Class A{
SortInterface i;
}

Interface SortInterface{
sort();
}

Class ConcreteAlgorithm1 implements SortInterface{
sort(){
...
}

}

Class ConcreteAlgorithm2 implements SortInterface{
sort(){
...
}

}


a family of sort algorithms

Sunday, June 6, 2010

Design principles

1. Identify the aspects of your application that vary and separate them from what stays the same. Take the parts that vary and encapsulate them,

take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don't.

All patterns provide a way to let some part of a system vary independently of all other parts.

2. Program to an interface, not an implementation.

The point is to exploit polymorphism by programming to a supertype so that the actual runtime object isn't locked into the code.

The declared type of the variables should be a supetype, usually an abstract class or interface, so that the objects assigned to those variables can be of any concrete implementation of the supertype, which means the class declaring them doesn't have to know about the actual object type. (page 12)

3. Favor composition over inheritance.
class A{
BehaviorClass1 behavior1;
BehaviorClass1 behavior2;
}

When you put two classes together like this you're using composition. Instead of inheriting their behavior,the class A get their behavior by being composed with the right behavior object.

Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime as long as the object you'are composing with implements the correct behavior interface.

Wednesday, June 2, 2010

Floating point Types

float 4 bytes (7 significant decimal digits)
double 8 bytes (15 significant decimal digits)

The name double refers to the fact that these numbers have twice the precision of the float type. (double-precision). Most applications choose double. the only reaons to use float are in the rare situation in which the slightly faster processing of single-precision numbers is important.

float have a suffix F 3.402F

floating point numbers without an F suffix are always considered to be of type double. you can optionally suppfly suffix D

SE5.0 0x1.0-3 = 0.125 (floating point in hexadecimal)

three special floating point values to denote overflows and errors
Double.POSITIVE_INFINITY
DOUBLE.NEGATIVE_INFINITY
DOUBLE.NaN( not a number)

ALL 'not a number' values are considered distinct.