wickedcoolthoughts
Monday, December 31, 2007
  Object Relations:my friends car can run on my car's tyre....!
See how my friends car can run on my car's tyre!
Car myFerrai = new Car();

Car myFriendsFerrari = new Car();

Tyre tyre = myFerrari.removeTyre();

Tyre flatTyre = myFriendsFerrari.replaceTyre(tyre);

myFriendsFerrari.drive();




And with the Building and Floor, No you cannot exchange the Floor(s)
 
Tuesday, December 18, 2007
  The beauty of return this;
As a beginner I was fascinated by the repeated append calls on the StringBuffer (Java) object. Especially because of the saving in the typing and no more string concatenation.
new StringBuffer("start").append("more").append("more")
Obviously the return statement of the append method should look like
return this;

Later I came across more interesting examples of this in the mock libraries like below.
Expect.Call(someObj.someMethod()).Return(anotherObject).Repeat().Once();

This time more than typing benefit what I liked about it is the readability of thecode. This is very close to the natural language translation of the above.
"Expect a method call someMethod on someObj. someMethod returns anotherObject. someMethod call is expected only once." Isn't that very close to how the code reads itself?

Look at this now.
 
"Create a new bug on build 45576 with description "this is really bugging ....". 
 
new Bug("new one").onBuild(45576)
   .withDescription("this is really bugging")
   .WithPriority("medium").reportedBy("QA")
and this
 bug = new Bug("new one");
 bug.setDescription("....");
 bug.setBuild(45576)......
 
We can improve the readability by adding 'And', 'With', 'To' etc.
Below I have added a creational method and used 'and' method to improve the readability a bit more.
 
 
Bug.CreateWithTitle("new one").onBuild(45576)
  .withDescription("this is really bugging")
  .WithPriority("medium").reportedBy("QA").and().assignTo("a dev")...
 
Friday, December 14, 2007
  Singleton Method Ruby vs Java


I thought this was cool

// This was the closest i could get in java.
// still confused about the terminology 'SingletonMethod'
public class SingletonMethodJava {

void theOnlyMethod(){
System.out.println("theOnlyMethod");
}

public static void main(String[] args){

new SingletonMethodJava(){
void addedOneMoreMethod(){
System.out.println("addedOneMoreMethod");
}
}.addedOneMoreMethod();
}
}


But isn't this cooler ?


class SingletonMethodRuby

def theOnlyMethod
puts "theOnlyMethod";
end
end

singleton_method = SingletonMethodRuby.new

def singleton_method.addedOneMoreMethod
puts "addedOneMoreMethod";
end

singleton_method.addedOneMoreMethod

 
Monday, December 10, 2007
  Redundancy of Language
"Couple of boys are riding their bikes!"

How many words in the above sentence tell you the presence of more than one person?
 
Friday, December 7, 2007
  Excel POI Wrapper in JRuby
Disclaimer:This is one of my very first JRuby/Ruby programs.
Dont be surprised if I am writing Java/C# in JRuby.



require '../../poi-3.0.1-FINAL-20070705.jar'
include Java

include_class java.io.FileInputStream
include_class Java::org.apache.poi.hssf.usermodel.HSSFWorkbook
include_class Java::org.apache.poi.hssf.usermodel.HSSFSheet
include_class Java::org.apache.poi.hssf.usermodel.HSSFRow
include_class Java::org.apache.poi.hssf.usermodel.HSSFCell

class ExcelWorkBook

def initialize(xlsFile)
input = FileInputStream.new(xlsFile)
@hssfworkbook = HSSFWorkbook.new(input)
end

def worksheet(sheet_position)
ExcelWorkSheet.new(@hssfworkbook.getSheetAt(sheet_position),sheet_position)
end

class ExcelWorkSheet

def initialize(hssfworksheet, sheet_position)
@index = sheet_position
@hssfworksheet = hssfworksheet
end

def row_at(rownumber)
return Row.new(@hssfworksheet.getRow(rownumber))
end

def rows
rows = []
for row in @hssfworksheet.rowIterator
rows << Row.new(row)
end
rows
end

class Row

def initialize(hssfrow)
@hssfrow = hssfrow
end

def value_at(columnindex)

cell = @hssfrow.getCell(columnindex)

if(cell == nil)
''
elsif(HSSFCell::CELL_TYPE_NUMERIC == cell.getCellType())
cell.getNumericCellValue()

elsif(HSSFCell::CELL_TYPE_STRING == cell.getCellType())
cell.getRichStringCellValue()
end

end

end

end
end



 
Wednesday, December 5, 2007
  Object Relations:Composition
It took me a while before I could find a convincing real world example of composition. Thanks to civil engineering and Christopher Alexander; I got the enlightenment ;).

Yes it’s a Building and the Floors. The life time of a Floor is totally dependent on the Building. The Floor is constructed, used and demolished through the Building!

After finding the right example the next interesting challenge was, how to model the relation? Clearly Building and Floor are the two key objects in the problem and I designed them as separate public classes as follows.

public class Building {
private List floors;
……….
}

public class Floor {
private int level;
}

Functionally this model was fine and I was about to convince myself that the both Composition and Aggregation are same in the design and the difference is conceptual and existed only in the real world. However the lack of conceptual integrity in the model was bugging me for quite sometime before I realized that inner classes is the right way to go.

The following code (trivial) demonstrates how the lifetime of the Floor is tied to the building unlike in the previous model. Please excuse my coding conventions.



public class BuildingTest extends TestCase {
private static final int NO_OF_FLOORS = 5;

public void testFloorOrder(){
Building building = new Building(NO_OF_FLOORS);

Building.Floor previousFloor = null;

for(Building.Floor floor : building.Floors()){
if(null == previousFloor) {
previousFloor = floor;
continue;
}
Assert.assertEquals(true, previousFloor.isAtLowerLevelThan(floor));
previousFloor = floor;
}
}
}


public class Building {

private List<Floor> floors;

public Building(int numFloors) {
floors = new ArrayList<Floor>();


for (int level = 0; level < numFloors; level++)
floors.add(new Floor(level));
}

public int numberOfFloors() {
return floors.size();
}

public List<Floor> Floors() {
return floors;
}

public class Floor {
private int level;

private Floor(int level) {
this.level = level;
}

public boolean IsAtLowerLevelThan(Floor floor) {
return this.level <floor.level;
}
}
}


 

My Photo
Name:
Location: India

I am passionate about making Better Software Solutions,that helps businesses to stay agile, by applying Agile,Lean and Systems Thinking principles. Also I grow coffee,spices and in general has big interest in pesticide free food!

Archives
December 2007 / January 2008 / February 2008 / March 2008 / June 2011 /


Powered by Blogger

Subscribe to
Posts [Atom]