The proxy design pattern is a layer that prevents you from instantiating heavy objects that will not be needed at a certain time.

The proxy pattern could be used to:

One example of the proxy structure could be the following:


I can have a proxy to access a store, if the store is closed it shouldn't even bother loading unnecessary resources.

  1. public interface IStore
  2. {
  3. void ListItems();
  4. }
  5. public class ProxyStore : IStore
  6. {
  7. private RealStore realStore;
  8. public void ListItems()
  9. {
  10. if (DateTime.Now.Hour >= 6 && DateTime.Now.Hour <= 10)
  11. {
  12. if (realStore == null)
  13. {
  14. realStore = new RealStore();
  15. }
  16. realStore.ListItems();
  17. }
  18. else
  19. {
  20. Console.WriteLine("We're closed!");
  21. }
  22. }
  23. }
  24. public class RealStore : IStore
  25. {
  26. public void ListItems()
  27. {
  28. Console.WriteLine("Heavy graphics Weapon 1");
  29. Console.WriteLine("Heavy graphics Weapon 2");
  30. Console.WriteLine("Heavy graphics Weapon 3");
  31. Console.WriteLine("Heavy graphics Weapon 4");
  32. Console.WriteLine("Heavy graphics Weapon 5");
  33. }
  34. }

This example is something we can see in Assassins's Creed where this pattern might have been used. In an early game there is a shop that has many unavailable options. Instead of loading all the resources required for the items it uses a proxy saying there is nothing available, so it saves many resources.

Proxy Design Pattern - Assassins's Creed