When we create an object by using new keyword, it takes memory. If we create 1000 objects in a computer, we don’t care about its memory. Because we know that, computer has lot of memory. But think about mobile. Its memory is very low. If we create lot of objects for a mobile game, it will arise problem.
In this situation prototype pattern will help use. We will make a clone or prototype of a particular object. When we need that object, we will use the clone object. Every time we don’t need to create new object.
Consider the bellow Customer class.
public Customer Person
{
public String Name { get; set; }
public String Email { get; set; }
}
public static void Main(string[] args)
{
var customer= new Customer();
customer.Name = "John Abrar";
customer.Email = "[email protected]";
Console.WriteLine("Name:{0} and Email:{1}", customer.Name, customer.Email);
}
Now if we need to use this Customer class in 1000 places, we have to create 1000 objects. This is not good practice.
Let’s clone the Customer class.
public interface IClone
{
Object Clone();
}
public class Customer : IClone
{
public String Name { get; set; }
public String Email { get; set; }
public Object Clone()
{
var customer= new Customer
{
Name = Name,
Email = Email
};
return customer;
}
}
public class Program
{
public static void Main(string[] args)
{
Customer customer = new Customer();
customer.Name = "John Abrar";
customer.Email = "[email protected]";
Console.WriteLine("Before Cloning.......");
Console.WriteLine("Name:{0} and Email:{1}", customer.Name, customer.Email);
Customer customer1= customer.Clone() as Customer;
Console.WriteLine("After Cloning..............");
Console.WriteLine("Name:{0} and Email:{1}", customer1.Name, customer1.Email);
}
}
Source link
Related posts:
Scrum Methodology | Agile Scrum Framework | Scrum Master Tutorial | Edureka
We Need One Complete Product Team - Coach Lankford
Jessica (#Jessitron) Kerr learned enough about any topic to start a conversation #WhoIsagile #WIa47 ...
Singleton Pattern - Cybarlab
Patrick Debois talks about changing the quality of life of others - Yves Hanoulle
Imagine…
Agile Scrum Master Interview Questions and Answers| Agile Scrum Master Training | Invensis Learning
aye Thompson likes to optimising the results instead of the use of her time - Yves Hanoulle
Top 50 Scrum Master Interview Question and Answers | Scrum Master Certification | Edureka
Scrum Jakarta – Chapter CiKemPon 2nd Meetup
Scrum Master Training & Job Opportunities: Learn What It Takes to be a Successful Scrum Master!
Taylorism isn’t as far from Agile and Lean as you would think