Home » Articles


Sponsored links

CodeDigest Navigation

Technology News
No News Feeds available at this time.

Community News
No News Feeds available at this time.
Design Pattern Interview Question - part 2

By Shivprasad Koirala
Posted On Aug 25,2008
Article Rating:
Average Rating: 5
No of Ratings: 1
No of Comments: 7
Category: ASP.Net
Print this article.

Design Pattern Interview Question - part 2

Builder, prototype and singleton pattern




Hi friends, please do not think you get an architecture position by reading interview questions. But yes there should be some kind of reference which will help you quickly revise what are the definitions. Just by reading these answers you get to a position where you are aware of the fundamentals. But if you have not really worked you will surely fail with scenario based questions. So use this as a quick revision rather than a shot cut.


To give you a practical understanding, i have put all these design patterns in a video format and uploaded on QuestPond. You can visit here and download the complete architecture interview questions PDF which covers SOA , UML , Design patterns , Togaf , OOPs etc.


Design pattern interview questions Part 1 here.


(I)Can you explain builder pattern?

Builder falls under the type of creational pattern category. Builder pattern helps us to separate the construction of a complex object from its representation so that the same construction process can create different representations. Builder pattern is useful when the construction of the object is very complex. The main objective is to separate the construction of objects and their representations.  If we are able to separate the construction and representation, we can then get many representations from the same construction. 

Figure: - Builder concept

To understand what we mean by construction and representation lets take the example of the below ‘Tea preparation’ sequence.


You can see from the figure ‘Tea preparation’ from the same preparation steps we can get three representation of tea’s (i.e. Tea with out sugar, tea with sugar / milk   and tea with out milk).

Figure: - Tea preparation

Now let’s take a real time example in software world to see how builder can separate the complex creation and its representation. Consider we have application where we need the same report to be displayed in either ‘PDF’ or ‘EXCEL’ format. Figure ‘Request a report’ shows the series of steps to achieve the same. Depending on report type a new report is created, report type is set, headers and footers of the report are set and finally we get the report for display.

Figure: - Request a report

Now let’s take a different view of the problem as shown in figure ‘Different View’. The same flow defined in ‘Request a report’ is now analyzed in representations and common construction. The construction process is same for both the types of reports but they result in different representations.

Figure: - Different View


We will take the same report problem and try to solve the same using builder patterns. There are three main parts when you want to implement builder patterns.


Ø       Builder: - Builder is responsible for defining the construction process for individual parts. Builder has those individual processes to initialize and configure the product.

Ø       Director: - Director takes those individual processes from the builder and defines the sequence to build the product.

Ø       Product: - Product is the final object which is produced from the builder and director coordination.


First let’s have a look at the builder class hierarchy.  We have a abstract class called as ‘ReportBuilder’ from which custom builders like ‘ReportPDF’ builder and ‘ReportEXCEL’ builder will be built.

Figure: - Builder class hierarchy

Figure ‘Builder classes in actual code’ shows the methods of the classes. To generate report we need to first Create a new report, set the report type (to EXCEL or PDF) , set report headers , set the report footers and finally get the report. We have defined two custom builders one for ‘PDF’ (ReportPDF) and other for ‘EXCEL’ (ReportExcel). These two custom builders define there own process according to the report type.


Figure: - Builder classes in actual code

Now let’s understand how director will work. Class ‘clsDirector’ takes the builder and calls the individual method process in a sequential manner. So director is like a driver who takes all the individual processes and calls them in sequential manner to generate the final product, which is the report in this case. Figure ‘Director in action’ shows how the method ‘MakeReport’ calls the individual process to generate the report product by PDF or EXCEL.


Figure: - Director in action

The third component in the builder is the product which is nothing but the report class in this case.

Figure: - The report class

Now let’s take a top view of the builder project. Figure ‘Client,builder,director and product’ shows how they work to achieve the builder pattern. Client creates the object of the director class and passes the appropriate builder to initialize the product. Depending on the builder the product is initialized/created and finally sent to the client.

Figure: - Client, builder, director and product

The output is something like this. We can see two report types displayed with their headers according to the builder.

Figure: - Final output of builder


(I) Can you explain prototype pattern?


Prototype pattern falls in the section of creational pattern. It gives us a way to create new objects from the existing instance of the object. In one sentence we clone the existing object with its data. By cloning any changes to the cloned object does not affect the original object value. If you are thinking by just setting objects we can get a clone then you have mistaken it. By setting one object to other object we set the reference of object BYREF.  So changing the new object also changed the original object. To understand the BYREF fundamental more clearly consider the figure ‘BYREF’ below. Following is the sequence of the below code:-

Ø       In the first step we have created the first object i.e. obj1 from class1.

Ø       In the second step we have created the second object i.e. obj2 from class1.

Ø       In the third step we set the values of the old object i.e. obj1 to ‘old value’.

Ø       In the fourth step we set the obj1 to obj2.

Ø       In the fifth step we change the obj2 value.

Ø       Now we display both the values and we have found that both the objects have the new value.



Figure :- BYREf


The conclusion of the above example is that objects when set to other objects are set BYREF. So changing new object values also changes the old object value.


There are many instances when we want the new copy object changes should not affect the old object. The answer to this is prototype patterns.


Lets look how we can achieve the same using C#.  In the below figure ‘Prototype in action’ we have the customer class ‘ClsCustomer’ which needs to be cloned. This can be achieved in C# my using the ‘MemberWiseClone’ method. In JAVA we have the ‘Clone’ method to achieve the same. In the same code we have also shown the client code. We have created two objects of the customer class ‘obj1’ and ‘obj2’. Any changes to ‘obj2’ will not affect ‘obj1’ as it’s a complete cloned copy.


Figure: - Prototype in action


Note :- You can get the above sample in the CD in ‘Prototype’ folder. In C# we use the ‘MemberWiseClone’ function while in JAVA we have the ‘Clone’ function to achieve the same.


(A) Can you explain shallow copy and deep copy in prototype patterns?


There are two types of cloning for prototype patterns. One is the shallow cloning which you have just read in the first question.  In shallow copy only that object is cloned, any objects containing in that object is not cloned.  For instance consider the figure ‘Deep cloning in action’ we have a customer class and we have an address class aggregated inside the customer class. ‘MemberWiseClone’ will only clone the customer class ‘ClsCustomer’ but not the ‘ClsAddress’ class. So we added the ‘MemberWiseClone’ function in the address class also. Now when we call the ‘getClone’ function we call the parent cloning function and also the child cloning function, which leads to cloning of the complete object. When the parent objects are cloned with their containing objects it’s called as deep cloning and when only the parent is clones its termed as shallow cloning.

Figure: - Deep cloning in action

(B) Can you explain singleton pattern?

There are situations in a project where we want only one instance of the object to be created and shared between the clients. No client can create an instance of the object from outside. There is only one instance of the class which is shared across the clients.  Below are the steps to make a singleton pattern:-

Ø       Define the constructor as private.

Ø       Define the instances and methods as static.

Below is a code snippet of a singleton in C#. We have defined the constructor as private, defined all the instance and methods using the static keyword as shown in the below code snippet figure ‘Singleton in action’. The static keyword ensures that you only one instance of the object is created and you can all the methods of the class with out creating the object. As we have made the constructor private, we need to call the class directly.


Figure: - Singleton in action


Note :- In JAVA to create singleton classes we use the STATIC keyword , so its same as in C#. You can get a sample C# code for singleton in the ‘singleton’ folder.



Thus, we have understood Builder, prototype and singleton patterns in part 2 of this article series. Remember, reading this article will not get you a architect position but it can be thought as a quick reference.

Thanks for reading my article!

Please post your queries and doubts in article feedback section.


Similar Articles
You can contribute to CodeDiget.Com:
Donate to
Article Feedback
Design Patterns
Simple and Super :)
You made my life easy with this article!!

So simple, very easy to understand
Excellent article.. Its very usefull to understand the basics of Design Patterns and indepth of architecture. Thanks a million....Shiv
Note: Plz provide sample project which include all layers by using design pattrens in C#
Simple and good
Author has explained the complex things in very simple manner
Simply Super
i can say this is the best article i ever read in design patterns, so simple, very easy to understand
Design Patterns
Very Useful!!
Fan of Koirala
This series is great. I have read so many design pattern books but when it comes to the simplicity of the explanation i can only see shivprasad koirala. A doubt sir we can also do deep copy using serialization so how is it different from using memberwiseclose method of the object.