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