Skip to main content

Posts

Java Design Patterns : Adapter Pattern

Whenever and wherever you will read about Adapter pattern you will come across example of electric adapters. Be it a 3-pin to 2-pin adapter, round pin to flat pin adapter or AC to DC adapter, its purpose is to act as an intermediary between electrical equipment and the socket. Above is a representation of a 3-pin to 2-pin adapter which exposes a 3-pin socket which takes in a 3 pin plug and gives a 2 pin output. (These 2-pins can be plugged into any 2-pin socket.)  Note that the adapter contains a 3-pin plug inside and behavior of its 3 pins is altered to suit the behavior of the adapter pins which will fit in the 2-pin socket. Now if we interpolate this in terms of Java concepts, the 3-pin socket is an interface and every 3-pin plug implements this interface. Let's call this as 'adaptee' interface. Similarly every 2-pin socket is also an interface which is implemented by every 2-pin plug. Let's call the 2-pin socket (which our adapter is trying to be...

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. Co...

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{ ...

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: "+myH...

What does it mean by Vector being synchronized?

So you found that there is a very famous (and hence commonly found on all the question sites and hence not asked anywhere anymore) interview question: What is difference between Vector and ArrayList? And the answer is simple: Vector is synchronized but ArrayList is not. Fine. But what does this mean? If you understand how synchronized works I will tell you the simplest way to understand the difference. Just look at the implementation of Vector ( here ) and ArrayList ( here ). In the implementation of Vector you will find that many of the methods are synchronized. size(), indexOf(), isEmpty(), set() etc. all are synchronized and hence all these methods can be accessed by one thread at a time. So the state of the Vector can be modified/accessed by only one thread at a time. On the other hand, none of the methods in ArrayList are synchronized. This is what Java doc says: Note that this implementation is not synchronized. If multiple threads access an ArrayList instance co...

Hands On Review : AOL's Alto Mail

I am a sucker for new online services, specially emails and when AOL announced its new offering Alto Mail, I was really excited as anything coming on the heels of Microsoft's Outlook.com with a premise of making mail arrangement a more visual affair had to be definitely worth a try. So does it really deliver the goods? First of all let's get this straight: Alto Mail is NOT a new Email service, it is just a new web based email client which you can connect to view your Gmail/Yahoo/AOL/iCloud content. (Only Yahoo.com accounts will work, no ".co.in" or ".co.uk" or any of the other Yahoo local domain names work with Alto). Once you login to Alto with any of your above mentioned accounts, give it a few hours (well it will depend on the size of your inbox and folders) to pull in all the emails/contacts from your account. But even before the import is completed, any emails sent from Alto will reflect in your 'Sent' folder of your linked mail account. ...