Mid GoF Patterns

Simulation Systems:?

  • In simulation software, objects representing physical entities (e.g., cars,

animals) can be cloned from a prototype, allowing for rapid creation of

multiple instances with different states.

Deep Cloning Example:

If you need to perform deep cloning, where not just the properties but also the referenced

objects are cloned, you can adjust the Clone() method to handle the deep copy:

public class GameCharacter : ICloneable

public string Name { get; set; }

public int Health { get; set; }

public List<string> Inventory { get; set; } = new

List<string>();

public ICloneable Clone()

var clone = new GameCharacter

Name = this.Name,

Health = this.Health,

Follow:

Inventory = new List<string>(this.Inventory) // Deep

copy of the Inventory list

return clone;

In this case, the Inventory list will also be cloned to ensure that modifications to the

Inventory of the clone do not affect the original object.

Conclusion:

The Prototype Pattern is a powerful creational pattern that allows you to clone objects

instead of creating them from scratch. It's especially useful when dealing with complex

objects or systems where performance and resource management are important. By using

this pattern, you can quickly create new objects with similar attributes and save time and

resources that would otherwise be spent constructing them from scratch.

Proxy Pattern: Controlling Access to Expensive Resources

Definition:

The Proxy Pattern provides a surrogate or placeholder for another object to control access

to it. The proxy acts as an intermediary, enabling you to perform additional actions (e.g., lazy

loading, access control, logging) before or after delegating operations to the real object.

Use Case:

The Proxy Pattern is useful when you need to control access to an expensive or

resource-intensive object. A common use case is controlling access to resources like large

images, network connections, or database connections. Instead of creating the actual object

immediately, a proxy can delay its creation or manage its lifecycle efficiently.

Code Breakdown:

More from Design Patterns in C#

All questions for this course