Skip to main content

Posts

Showing posts from December, 2012

Java Design Patterns: Decorator Pattern

Before talking about Decorator design pattern, think of decorators in real life. Let's consider a very common  example. In an ice cream parlor you can order ice creams in Vanilla, Butter Scotch, Chocolate and Pineapple flavors with or without an assortment of toppings. Here are your choices: Chocolate sauce Strawberry sauce Maple Syrup Fruits and nuts or just a Cherry You can even do a mix and match of toppings according to your liking. So you can have a scoop (or two) of vanilla ice cream with Strawberry syrup topped with fruits and nuts and a cherry.  Each of these toppings is a 'Decorator'. As evident from name itself, Decorators in real life 'decorate' an existing object. They ADD something to an existing object. Decorator design pattern also solves the same problem. If you think of implementing this system, your first instinct would be to create a base class Icecream and make Vanilla , ButterScotch , Chocolate and Pineapple extend it. Correc

Hands on review: Yahoo! Mail app for Windows 8

Before you start mocking me for using Yahoo! Mail, let me explain that this is NOT my primary mail account and I use Gmail and Outlook.com for my daily use. Yes, this Yahoo! account was once my daily haunt but that was during college days and those days are long gone. I don't use Yahoo! Mail much but still there are some group subscriptions and I just log in once in a while to see what's happening in those forgotten groups. And due to this reason, I have always kept track of changes in Yahoo! Mail and the fact is that you can blame them for everything but not really lack of trying. Though most of those tries just ended up cluttering the UI and adding somewhat needless features. However, now we hear that new CEO Marissa Mayer is focusing once again on the mail and first major product to come out is Yahoo! Mail app for Windows 8. So how is it? The Login screen is pretty neat and purple background with an envelope watermark is very pleasing, very Yahoo!. However why

Java Design Patterns: Simple Factory pattern

Design Patterns are much talked about yet much feared of, much explained and yet much confused topic. A lot of fundamentals used in Design Patterns are actually application of basic Object Oriented Design principles. Take example of 'Simple Factory' (or just 'Factory') pattern, which is not actually a pattern but more of a good design practice. Think of a scenario where you need to create a different variant of some class depending on some condition. E.g. You have a base class House and various sub classes extending House, say Villa, Flat and Penthouse. Now depending on some user input, say price range, you need to use objects of Villa, Flat or Penthouse to perform some function. public double getHouseArea(double higherPriceRange){     House house;     if(higherPriceRange < 4000000){       house = new Flat();     }else if(higherPriceRange < 10000000){       house = new Villa();     }else{       house = new Penthouse();     }          return ho

Java is always Pass-By-Value. Never Pass-by-Reference. Explanation.

We often read about how Java is pass by value but very often there is a confusion that behavior is different in case of primitives and object references leading to following statement (which is kind of 'aid to memory' rather than anything else): Java does pass-by-value for primitives and pass-by-reference for object references. Consider following code snippet. public class House{   String type;   public  House (String type){     this. type =  type ;   }      public String get HouseT ype (){    return this. type ;    }      public void set HouseT ype (String  type ){    this. type =  type ;   } } public class MainClass{   public static void main(String[] args){     House myHouse; ///Line 0     myHouse = new House("Flat"); ///Line 1     System.out.println("Output1: "+myHouse. get HouseT ype ());          foo(myHouse); ///Line 2     System.out.println( "Output2: "+ myHouse. get HouseT