Interfaces for:
create and read-only
| Do this second! This approach represents the sweet spot between safety and flexibility. |
Story: An application wants to control what a generic reader to produces.
- application can supply factory to choose implementation used
(factory must be provided everywhere needed)
Create |
|
Factory provided |
Read |
|
|
Visit |
|
not required |
Write |
|
|
Thread-safe |
|
|
Mixed Implementations |
|
|
The big win here is providing specific implementations to the task or environment at hand. This approach is strongly recommended.
This approached is used by the GeoAPI filter package.
Example Interface
Example Safe Implementation
Assume the User class is a singleton mapped per thread group; separate code
would need to perform the login.
Please note the CitationSecure constructor is no longer public.
Example Hidden Mutablity with Public Construction
This is a harder example to explain - only the application can modify the data once constructed (but it would like some generic reader code to do the construction). The nice thing about the factory pattern is that you can inject your factory into the system (somehow) and you know what objects are going to be available. Also see the Visit page for how this factory example can be powerfully used.