Category Archives: Design

Open-Closed principle Revisited

Reference: http://kamalmeet.com/system-design-and-documentation/understanding-openclosed-principle/

Open closed principle states that your classes should be open for extension but closed for modification. One way to look at it is that when you provide a library or a jar file to a system, you can ofcourse use the classes or extend the classes, but you cannot get into the code and update it.

At a principle level, this means you should code in a manner that you never need to update your class once code. One major reason behind this principle is that you have a class which is reviewed and Unit tested, you would not like someone to modify and possibly corrupt the code.

How do I make sure that my class follow open closed principle?

Let’s look at a design of this MyPizza class

public class MyPizza {
public void createPizza(Pizza pizza)
{
if(pizza.type.equals("Cheese"))
{
//create a cheese pizza
}
else if(pizza.type.equals("Veg"))
{
//create a veg pizza
}
}
}

Following pizza type classes use this

class Pizza
{
String type;
}

class CheesePizza extends Pizza{
CheesePizza()
{
this.type=”Cheese”;
}
}

class VegPizza extends Pizza{
VegPizza()
{
this.type=”Veg”;
}
}

The above design clearly violates the open closed principle. What if I need to add a double cheese pizza here. I will have to go to MyPizza class and update it, which is not following “closed for modification” rule.

How can fix this design?

public class MyPizza {
public void createPizza(Pizza pizza)
{
pizza.create();
}
}


class CheesePizza extends Pizza{
CheesePizza()
{
this.type="Cheese";
}

public void create()
{
//do the creation here
}
}

With this simple modification we are making sure that we will need not change the code in MyPizza class even when we will add new types of pizza, as actual responsibility of creation would be with the new class being created (DoubleCheese).

Message Oriented Middleware

In last post I talked about what is middleware, I will focus on message implementation of same today. Message oriented middleware or MOM mostly uses message queues to send and receive data between two systems.

In simple terms, a message is anything that is being sent from one system to another. Mostly MOM uses XML formats, sometimes SOAP based requests or plain texts. An example MOM system will send message to a  message queue or MQ, from where the receiver will pick up the message.

Advantages of Message Oriented Middleware

  1. Persistence: In normal client-server architecture, we will need to make sure both the systems to be available to have a successful communication. Whereas if we are using MQs, one system can still send messages even if the second is down.
  2. Support for Synchronous and Asynchronous communication: by default the communication is asynchronous but we can implement a synchronous system where a request message sender will wait for the response from other party.
  3. Messages can be consumed at will: If one system is busy when messages are received (which do not need immediate response), it can consume the messages when load is less. For example, a lot of systems are designed to consume the messages at non business hours.
  4. Reliability: As messages are persistent, threat of losing information is low even if one or more systems are not available. Additional security mechanism can be implemented in MQ layer.
  5. Decoupling of systems: Both client and server work independently, and often do not have knowledge for other end. System A creates a message and adds to message queue, without concerning who will pick it up as long as it gets the response message (if required). So one system can be written in Java and other can be in Dot Net.
  6. Scalability: As both machines involved in interaction are independent of each other, it is easier to add resources at either end without impacting the whole system.
  7. Group communication: Sender can send message to multiple queues or same queue can have multiple listeners. In addition Publisher- Subscriber approach can help broadcast a message.

Types of Messaging:

Point to Point: This is a simple messaging architecture where a sender will directly send a message to receiver through message queue.

Publisher-Subscriber (Pub-Sub): This type of communication is required when sender wants to send messages to multiple receivers. Topics are defined to which subscriber can subscribe and receive requests based on same. For example, say a core banking system can trigger messages on various events like new account open, a withdrawal is made, interest rate changed etc. For an event, multiple other systems might want that information to take an action, so say for all withdrawal events, systems like fraud detection, mobile messaging system, daily reporting system, account maintenance system subscribe. Whenever, publisher publishes the message to “Withdrawal” topic, all of these systems will receive the message and take appropriate action.

Implementing Scalability

These days there is a lot of buzz around the word ‘scalability’ in IT world. The concept is actually not new, I have been designing scalable systems for last 9 years, but the idea has definitely changed since then.

How meaning of scalability has changed in last few years?

If 5-6 years back, I was creating a system to support say 10K users, and someone would have told me to make it scalable, I would have thought of making the system it in such a way that it can support double or may be 4 times or max 10 times the users in next 3-4 years. But with the applications like facebook, amazon, ebay, twitter the idea about scalable system is different. Now the user base can increase exponentially in matter weeks. And that is what every organization wants.

What is the impact of change?

Impact of the change is that  now you do not want a system which will need a good amount of change if your user base is increasing. Earlier, as number of users used to grow slowly, it will give you time to think, design, redesign, upgrade the system, but now, as user base can increase with much more speed, you want a system which can scale up within minutes and it should be able to it automatically.

How to achieve scalability in today’s world?

As the demand has changed, so has technology. Cloud computing has made it much easier for us to create scalable systems. Key component choices to be made while creating a scalable solution

1. Design: Design of application/ code should be able to handle infinite load.

2. Database: If you are expecting your data load to grow beyond a few million, you might want to go for NOSQL over RDBMS.

3. Hardware: you should be able to add in new hardware and replicate the application on new servers within minutes. Cloud system can help you here.

4. Load balancing: If our application is getting distributed/replicated over multiple servers, we will need to take care of load balancing so that no server will choke.

Testing your website in different browsers- Cross browser compatibility

There are many services available on the internet which will let you know how you website/ webpage will appear on different browser or different version of same browser or same browser in different Operating systems. I tried looking for some free services for a project. Two services that look good

http://browsershots.org/

Pros- FREE, supports 100+ combination of browser and OS, No Registration required- you are good to go by just providing site link

Cons- Slow, incomplete results for some cases

https://browserlab.adobe.com/

Pros- Free, Fast, Chose browser at runtime and change

Cons- Limited browser selection, Registration required, Did not work for some of the mentioned browsers(may be temporary issue)

Creating a website, in less than an hour, for free

Over the weekend a friend discussed his idea of creating a web portal for some specific services with me. The failed entrepreneur in me liked his idea and encouraged him to go ahead (anyone, who is trying, should be encouraged). He shared some wireframes for his portal, but he was not sure how will it actually look on the web. So after returning home, I decided to get some basic version of the website up on the internet to help him. Resulting in the journey mentioned below.

Step 1: I knew I will have to share my email address with service providers (hosting space/ domain name etc.). And as I am looking for free stuff, it would be more than possible that I come across some fake websites. So I thought of creating a dummy email id which could be used to register to all these websites.

Step 2: Now I needed a domain name. I could buy, but as this was just a dummy site, why waste money. Next option is using subdomain from web hosting provider. Here I had to look for a free web hosting provider, and my past experience with free hosting providers is ‘don’t trust them’. The site can go down anytime without notice (its for free. What were you expecting?). So I thought of getting a free domain name, yes, a co.cc domain. I registered a domain with http://www.co.cc/, which I could use with any service provider.

Step 3: Now I needed web hosting. I googled around some free web hosting sites, zeroed upon one (checked features and reviews), registered, and bam, it is not working. Moved to second one, registered, and this time it worked. It was awardspace.com. I got Linux hosting with Apache, PHP, MYSQL. Basically all I needed, for free. I set up the nameservers for the domain and all is good. (Ofcourse I tried a hello world example, I am still the same old fashioned developer at heart).

Step 4: Now we have all set to host our web pages. I had the wireframe with me, and I had frontpage, I could easily create the HTML pages. BUT, from my past experience, I knew designing web pages is a sin for a developer (“Are you colorblind! Why are all the pages grey?”). So instead I pinged a designer friend and asked for some templates. Who suggested checking http://projectw.org for free html/ php templates (you will find more interesting stuff on the forum, but handle with care :D). Ok so I had a template with me, now I needed to customize it for my pages. Actually this was the part which took most of the time. Once done! I just had to upload the pages to my website using ftp. And we are done.

Wasn’t it simple. If instead of a website you are trying to create a blog, you can simply upload the blogging software. Infact most of control panels come with blogging softwares, so that is actually simpler.

If you don’t have your website yet, what are you waiting for? Go ahead! Get your hands dirty.

Difference between visibility hidden vs display none

I spent almost an entire day on this, so wanted to share the information in case it might be helpful for others. We at times need to remove a DOM object (textbox/ label/ dropdown etc) from the UI. For which we use two things object.style.visibility=”hidden” and object.style.display=”none”. Though both of them can remove the object from UI, yet there is a significant difference between these two approaches. Whereas visibility hidden will “hide” the object, it will still leave the object on the UI (in hidden format). On the other hand display=”none” will completely remove the object from UI.

The differentiation is more important in a case, where the css being used here does not specify the x and y coordinates for DOM objects, it instead places one object after another. So the trick here is, if I make an object invisible by hiding it (visibility=”hidden”), it will still occupy that one space in the flow, and a blank cell will appear on the screen. Whereas if display is none, then the next object will take place of this object and hence no empty spaces are shown on the screen.

So both the approaches can be used as per ones requirement