Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Comments on Is this a known design pattern: a piece of code is responsible for acting as a central proxy for data distributed in various places?

Post

Is this a known design pattern: a piece of code is responsible for acting as a central proxy for data distributed in various places?

+3
−1

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (10 comments)
General comments
Alexei‭ wrote over 3 years ago

This is an interesting question. I am thinking of something like Redux which deals with state management. It would be useful if you can provide a simple example like 3 classes with 3 data types to understand the nature of your application (e.g. is the data related to the state or is it something else?).

elgonzo‭ wrote over 3 years ago · edited over 3 years ago

Unless i am mistaken, your X sounds like an implementation of the Facade pattern (https://en.wikipedia.org/wiki/Facade_pattern). But, i am not entirely sure whether your question is specifically about X or about some other aspect of your code structure and logic, though...

peey‭ wrote over 3 years ago

I'll add an example so that things are clearer.

peey‭ wrote over 3 years ago

@Alexei that's a useful comparison. I think redux but without centralizing everything, and just centralizing a subset of data is a useful comparison. In my real scenario the data is related to state, but I've tried to generalize it in the question.

peey‭ wrote over 3 years ago

@elgonzo I'm also feeling similarities with Facade, but I am not completely sure due to it not being a facade on classes but on some well-known instances. Perhaps what I have here is a weird mix of 2 design patterns which is becoming the root cause of the confusion.

elgonzo‭ wrote over 3 years ago · edited over 3 years ago

Hmm, you said you have instances of C_1 ... C_n classes. Why is it then not about classes? The facade pattern is about "hiding" the structure and complexities of a (sub)system and present (some of) its functionality through a simplified interface (the facade, in your case X). Your comment leaves me even more confused :(

Skipping 2 deleted comments.

elgonzo‭ wrote over 3 years ago

If it is about having different "SomethingElse" instances with their individual incarnations of C_1 ... C_n instances, well, I don't know nothing about this "SomethingElse" except its name and that you call it a "registry" of C_1 ... C_n instances. So, again i can't tell with certainty, but from the little i know "SomethingElse" sounds like being just a composite datatype (as in "object composition"; which is not a design pattern and should not be confused with the composite design pattern)...

peey‭ wrote over 3 years ago

@elgonzo because it's about the two particular instances ("testing" server and "prod" server in the example). There may be other instances which X doesn't care about, and neither does SomethingElse

Alexei‭ wrote over 3 years ago

X class (instance) seems like a God object to me. Why do you need to centralize so much heterogenous functionality in one place?

Lundin‭ wrote over 3 years ago

You simply can't discuss proper program design in vague and abstract terms - that's nothing but a source of pointless & badly designed abstraction layers. What is the data that's being passed around and why does it need to be passed around? Does each node know the destination or is it the server object's responsibility to find out? How? Are there addresses? Are the nodes in physical different locations (clients, bus nodes etc) or in different processes, or in the same process? And so on.