-
Notifications
You must be signed in to change notification settings - Fork 0
Proxy

Structural pattern
Provides a proxy (or so-called surrogate) for another object to control access to it, which may be remote, expensive to create or requires additional security measures.
Proxy is usually related to one of these groups:
-
Remote proxy - Controls access to remote object/resource (for example you might encapsulate calls to an object in another VM via Java RMI).
-
Virtual proxy - Controls access to resource, that is expensive to create (e.g. lazy loading).
-
Protection proxy - Controls access to resource by restricting caller with some security policy (e.g. restricting calls frequency per user, restricting by user rights/privileges).
Advantages:
+ Allows to introduce additional control over original object without any needs to change original object's class or client code.
In this project we have Virtual proxy example with the following model:
1. Accident - interface, that represents abstract accident that happened at some point of time. Contains:
- Summary info - summary info about given accident
- Accident date - date of given accident
- Victims - people who died at given accident
- Survivors - people who survived given accident
Each accident is stored as two files in resources folder:
accident_name.properties - contains basic info, such as Summary info and Accident date. (Fast-to-read small file)
accident_name.csv - contains info about Victims and Survivors. (Potentially slow-to-read huge file)
Accident has two implementations
AccidentData - original class, that stores info about given accident.
LazyAccidentData - virtual proxy for AccidentData class. Provides Summary info and Accident date eagerly (so-called basic info). Lists of victims and survivors are loaded lazily upon first call.
AccidentFactory - produces LazyAccidentData, meanwhile reading basic info from accident_name.properties.
Lazy loading of Survivors and Victims might be very handy if our client does not need to know them at all. In that case proxy saves him from parsing potentially huge .csv files.