Prototype Pattern
Prototype Pattern
& Singleton
Design Pattern
`
Prototype Pattern
• A creational pattern
• Specify the kinds of objects to create using a prototypical
instance, and create new objects by copying this prototype
Problem
Prototype solution
Prototype Pattern UML
Participants:
• Prototype
o declares an
interface for cloning
itself.
•
ConcretePrototyp
e o implements an
operation for cloning
itself.
• Client
o creates a new
object by asking a
prototype to clone
itself.
Exampl
e
Animal farm
Prototype Pattern Example code
Prototype Pattern Example code
Prototype Pattern Example code
Prototype Pattern
When to Use
• When product creation should be decoupled from system behavior
• When to avoid subclasses of an object creator in the client
application
• When creating an instance of a class is time-consuming or complex in
some way.
Consequences of Prototype
Pattern
• Hides the concrete product classes from the client
• Adding/removing of prototypes at run-time
• Allows specifying new objects by varying values or structure
• Reducing the need for sub-classing
Drawbacks of Prototype Pattern
• class Program
• {
• static void Main(string[] args)
• {
• Employee emp1 = new Employee();
• emp1.Name = "Anurag";
• emp1.Department = "IT";
• Employee emp2 = emp1.GetClone();
• emp2.Name = "Pranaya";
• Console.WriteLine("Emplpyee 1: ");
• Console.WriteLine("Name: " + emp1.Name + ", Department: " +
emp1.Department);
• Console.WriteLine("Emplpyee 2: ");
• Console.WriteLine("Name: " + emp2.Name + ", Department: " +
emp2.Department);
• Console.Read();
• }
• }
• public class Employee
•{
• public string Name { get; set; }
• public string Department { get; set; }
• public Employee GetClone()
•{
• return (Employee)this.MemberwiseClone();
•}
Output
• Singleton
.• Intent
• Singleton is a creational design pattern that lets you
ensure that a class has only one instance, while providing
a global access point to this instance.
Problem
The Singleton pattern solves two problems at the
same time, violating the Single Responsibility
Principle:
1.Ensure that a class has just a single
instance. Why would anyone want to control how
many instances a class has? The most common
reason for this is to control access to some shared
resource—for example, a database or a file.
2.Here’s how it works: imagine that you created
an object, but after a while decided to create a
new one. Instead of receiving a fresh object, you’ll
get the one you already created.
3.Note that this behavior is impossible to
implement with a regular constructor since a
constructor call must always return a new object
by design.
2.Provide a global access point to that
instance. Remember those global variables that
you (all right, me) used to store some essential
objects? While they’re very handy, they’re also
very unsafe since any code can potentially
overwrite the contents of those variables and
crash the app.
3.Just like a global variable, the Singleton pattern
lets you access some object from anywhere in
the program. However, it also protects that
instance from being overwritten by other code.
4.There’s another side to this problem: you don’t
want the code that solves problem #1 to be
scattered all over your program. It’s much
better to have it within one class, especially if the
rest of your code already depends on it.
Nowadays, the Singleton pattern has become so
Solution
All implementations of the Singleton have these
two steps in common:
Structure
The Singleton class declares the static method
getInstance that returns the same instance of its own
class.
1.The first and most important advantage of using the singleton design pattern in C# is
that it takes care of concurrent access to the shared resource. That means if we are
sharing a resource with multiple clients simultaneously, then concurrent access to that
resource is well managed by the singleton design pattern.
3.To share common data i.e. master data and configuration data which is not changed that
frequently in an application. In that case, we need to cache the objects in memory.
5.To reduce the overhead of instantiating a heavy object again and again.
SINGLETON IMPLEMENTATION
METHODS
1. EAGER
2. LAZY
3. SYNCHRONIZED METHOD
4. DOUBLE LOCKING
1. EAGER
2.LAZY INITIALIZATION
3.SYNCHRONIZED METHOD
4.DOUBLE LOCKING