Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I have several classes (C_1...C_n) and their instances (I_1....I_m). I have a "Registry" R of these. There are several pieces of data (d_1...d_k) spread across various instances. This much is set ...
Question
design-patterns
#2: Post edited
- I have several classes (`C_1`...`C_n`) and their instances (`I_1`....`I_m`). I have a "Registry" `R` of these.
- There are several pieces of data (`d_1`...`d_k`) spread across various instances. This much is set in stone, i.e. I consider the data being spread across the instances as appropriate design.
- Now while writing the code in `C_i`, each class does know how to access the data that it's responsible for (among the `d_j`), but I wouldn't like it to have that knowledge for the data in other classes / instances.
- I'm thinking of achieving this by declaring a singleton class `X` which has access to this registry, and it acts as a proxy between the actual location of the data (in `I_j`) and the name of the data `d_k`.
- So using `X`, anywhere in my program I can write `X.get_d2()` and I'll get `d2`, without worrying about where it came from.
- Is this a known design pattern? If not, then are there any obvious pitfalls with this design that some other patterns are known to mitigate?
- I have several classes (`C_1`...`C_n`) and their instances (`I_1`....`I_m`). I have a "Registry" `R` of these.
- There are several pieces of data (`d_1`...`d_k`) spread across various instances. This much is set in stone, i.e. I consider the data being spread across the instances as appropriate design.
- ```
- class Server { // C1
- public String host; // d_i
- public String port;
- //....
- }
- class Administrator { // C2
- public String username; // d_j
- public String hashed_password;
- public Server belongs_to;
- //....
- }
- // ....
- class SomethingElse { // Cn
- // more of this later
- }
- class RegistryOfWellKnownInstances {
- getServers(); // I_j
- getAdministrators(); // I_k
- // ...
- }
- ```
- Now while writing the code in `C_i`, each class does know how to access the data that it's responsible for (among the `d_j`), but I wouldn't like it to have that knowledge for the data in other classes / instances.
- ```
- /**
- * Let's say there's a concept of certain well-known instances with the Registry. There are servers "testing" and "prod" and we want access to the host name (d_i) and admin usernames (d_j) for them.
- * This class doesn't have too much knowledge about the `Server` class or `Administrator` class. They are complex classes, and perhaps managed by another set of programers. It doesn't even care about how/where these instances are created and how it'll have access to those instances.
- */
- class SomethingElse {
- // ...
- }
- ```
- I'm thinking of achieving this by declaring a singleton class `X` which has access to this registry, and it acts as a proxy between the actual location of the data (in `I_j`) and the name of the data `d_k`.
- ```
- // I could directly encode logic for getting the data from the registry in the class "SomethingElse", but it's actually needed by others as well, so let's define something common
- class X {
- // NOTE: I'm aware that I can parametrize this with "prod" or "testing" but for sake of example let's say these are simple getters without a parameter.
- // These methods would work by essentially getting instance from registry and just invoking appropriate getters / accessing appropriate fields
- get_prod_hostname() // d1
- get_testing_hostname() // d2
- get_prod_admins() // d3
- get_testing_admins() // d4
- // ....
- }
- ```
- So using `X`, anywhere in my program I can write `X.get_d2()` and I'll get `d2`, without worrying about where it came from.
- Is this a known design pattern? If not, then are there any obvious pitfalls with this design that some other patterns are known to mitigate?
#1: Initial revision
Is this a known design pattern: a piece of code is responsible for acting as a central proxy for data distributed in various places?
I have several classes (`C_1`...`C_n`) and their instances (`I_1`....`I_m`). I have a "Registry" `R` of these. There are several pieces of data (`d_1`...`d_k`) spread across various instances. This much is set in stone, i.e. I consider the data being spread across the instances as appropriate design. Now while writing the code in `C_i`, each class does know how to access the data that it's responsible for (among the `d_j`), but I wouldn't like it to have that knowledge for the data in other classes / instances. I'm thinking of achieving this by declaring a singleton class `X` which has access to this registry, and it acts as a proxy between the actual location of the data (in `I_j`) and the name of the data `d_k`. So using `X`, anywhere in my program I can write `X.get_d2()` and I'll get `d2`, without worrying about where it came from. Is this a known design pattern? If not, then are there any obvious pitfalls with this design that some other patterns are known to mitigate?