Question

I'm trying to avoid creating a monster class, and I noticed a not-so-good pattern and was curious if anyone had any insight into this problem.

Problem

A is complex. To avoid the "god object", some of the logic is pushed out into B and C.

A now serves as a façade. It depends on B and C.

A -> B
  -> C

However, A has configuration data that B and C need. So we have to keep the data in sync.

b.value1 = a.value1
b.value2 = a.value2

c.value1 = a.value1
c.value2 = a.value2

Also, B and C must convert the data to use it.

b.sum -> b.value1 + b.value2
b.doSomething -> b.sum() ...

c.sum -> c.value1 + c.value2
c.doSomethingElse -> c.sum() ...

To avoid writing duplicate code, we move these conversion methods back to A.

a.sum -> a.value1 + a.value2

b.doSomething -> a.sum() ...
c.doSomethingElse -> a.sum() ...

But this means B and C now depend on A, not just its data.

A -> B -> A
  -> C -> A

And this results in a circular dependency.

I've been studying dependency injection and the factory pattern, and these two ideas have solved a lot of problems, but I'm stumped on how to create an object graph whose nodes are configurable at runtime. In other words, I need the ability to change the behavior of a specific node or group of nodes at runtime from the outside. What's the best way to propagate configuration state down an object graph?

Edit

Having spent the last couple hours trying to formulate my thoughts into a question, I think I may have stumbled into a solution. I think what I need to do is create a configuration provider class D that gets passed into the nodes that depend on this information.

A -> B -> D
  -> C -> D
  -> D

D manages value1 and value2 and provides the sum method. D also has events that fire when value1 and value2 change so that dependent nodes (A, B and C) can respond to the change.

I think this solution works, but I wanted to post anyway in case there's a better solution. If this is the pattern I'm looking for, what is it called? The "provider" pattern? (Not to be confused with ASP.NET's provider model!)

Was it helpful?

Solution

are you looking for observer or mediator pattern. check it out on http://sourcemaking.com/design_patterns/observer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top