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:
- prevent loading unnecessary resources
- access real data in a remote location
- prevent control from accessing non-declared members/methods
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.
- public interface IStore
- {
- void ListItems();
- }
-
- public class ProxyStore : IStore
- {
- private RealStore realStore;
-
- public void ListItems()
- {
- if (DateTime.Now.Hour >= 6 && DateTime.Now.Hour <= 10)
- {
- if (realStore == null)
- {
- realStore = new RealStore();
- }
-
- realStore.ListItems();
- }
- else
- {
- Console.WriteLine("We're closed!");
- }
- }
- }
- public class RealStore : IStore
- {
- public void ListItems()
- {
- Console.WriteLine("Heavy graphics Weapon 1");
- Console.WriteLine("Heavy graphics Weapon 2");
- Console.WriteLine("Heavy graphics Weapon 3");
- Console.WriteLine("Heavy graphics Weapon 4");
- Console.WriteLine("Heavy graphics Weapon 5");
- }
- }
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.