0

Consider a program structured essentially like this:

Structure Diagram

ClassA, B, and C fit together logically in a parent/child relationship, and they never expose their private properties. The program would make perfect sense (and would compile and run) with just these three components. However, in order for the program to be particularly useful, the user needs to have the ability to change all of the properties while the program is running, hence the "control" object, which could be a Windows Form for example. The control should be seen as a sort of phantom: it should have the ability to alter properties, but as far as the rest of the program is concerned, it doesn't exist. The problem is, I am not aware of way to do this without changing the fundamental structure of the main program (essentially making all the properties public).

So my question is really on a general programming level: is there a name for this kind of structure, and if so how is it accomplished? If not, is there a better approach to what I'm trying to do?

Edit: Since the solution may depend on the language, assume that the core program is completely cross-platform c++, and the control is some type of GUI programmed for any specific platform.

Madison Brown
  • 331
  • 3
  • 10
  • Consider whether a delegate scheme might work better. – Hot Licks Oct 07 '13 at 22:04
  • Good point, that would solve one problem by allowing only the control to access the getters/setters. However, I believe such a scheme would still require the classes to "know" about the control, where ideally they should not. – Madison Brown Oct 07 '13 at 22:13
  • There is no way to not "know", to some degree. The code must at the very least be "aware" that values can change dynamically, due to some "invisible" process. – Hot Licks Oct 07 '13 at 22:21
  • And I was thinking maybe the other way -- make the "control" the delegate and have the classes interrogate it. – Hot Licks Oct 07 '13 at 22:22
  • Ok, I see. That will probably be my best bet then, thanks. – Madison Brown Oct 07 '13 at 22:41

1 Answers1

0

I think you are looking for the Publish-Subscribe Pattern

In most cases, you're properties should be private and you should expose getters/setters when necessary.

Ethan
  • 2,754
  • 1
  • 21
  • 34
  • Even so, i wouldn't want to add a getter and setter for every property in the program just so that the control can access them, because that still exposes variables that otherwise would remain hidden. I will look into the publish-subscribe pattern though, thanks. – Madison Brown Oct 07 '13 at 21:00
  • Like I said, only expose the getter/setter when necessary. However, modifying your properties directly (without a setter) can be dangerous because you can't validate the data being modified. – Ethan Oct 07 '13 at 21:02
  • Sorry, I'm not sure that you understood my question. The program runs just fine without the control, and in that case doesn't require any getters/setters. If I want to add the control though (which would be like a windows form in my case), I would have to add a getter and setter for just about every property, which seems like the wrong approach to me. – Madison Brown Oct 07 '13 at 21:17
  • depends on what language you're using. if your using c# you could write your control to use reflection to get the properties of the objects - thereby having a generic control that you can point at any object. In other languages, an interface, perhaps. Could expose properties through COM, etc, etc – mp3ferret Oct 07 '13 at 21:27
  • ok, how would you approach it if the core program was all cross-platform c++, and you wanted to program separate controls depending on the platform? – Madison Brown Oct 07 '13 at 21:51