0

I'm writing pretty big and complex application, so I want to stick to design patterns to keep code in good quality. I have problem with one instance that needs to be available for almost all other instances.

Lets say I have instance of BusMonitor (class for logging messages) and other instances that use this instance for logging actions, in example Reactor that parses incoming frames from network protocol and depending on frame it logs different messages.

I have one main instance that creates BusMonitor, Reactor and few more instances. Now I want Reactor to be able to use BusMonitor instance, how can I do that according to design patterns?

Setting it as a variable for Reactor seems ugly for me: self._reactor.set_busmonitor(self._busmonitor)

I would do that for every instance that needs access to BusMonitor. Importing this instance seems even worse.

Altough I can make BusMonitor as Singleton, I mean not as Class but as Module and then import this module but I want to keep things in classes to retain consistency.

What approach would be the best?

4
  • what's wrong with setting it as a variable? why do you think it's ugly? Commented Jul 6, 2012 at 11:43
  • Hmm I'm not sure if this is good approach, wont it rot in future? Or look at this example. I have main instance of gui interface, it doesn't need access for busmonitor, this gui makes instance of tabs, tabs makes some deeper widgets, and then we get to the point where instance needs access to busmonitor, now I'll have to pass busmonitor as variable for every instance from top to bottom to be able to pass it. Commented Jul 6, 2012 at 12:11
  • hmm... the widget could ask the parent to give him the object, the parent would ask its parent and so forth.. Commented Jul 6, 2012 at 12:39
  • this would be a disaster, Usually you dont remember how many instances deep it is. and it would look like this: self._busmonitor = self.parent().parent().parent().parent().busmonitor() Commented Jul 6, 2012 at 13:09

3 Answers 3

3

I want to keep things in classes to retain consistency

Why? Why is consistency important (other than being a hobgoblin of little minds)?

Use classes where they make sense. Use modules where they don't. Classes in Python are really for encapsulating data and retaining state. If you're not doing those things, don't use classes. Otherwise you're fighting against the language.

Sign up to request clarification or add additional context in comments.

1 Comment

But I'm encapsulating data. Filters states, messages that get logged and few other things. Of course we can say it's not worth it and I should use module. However, how this problem is solved in other languages?
1

As you already have a hierarchy, you could use a chain to get it.. it's not the Chain-of-responsibility pattern, but the idea is similar.

Each widget has a getbusmonitor call, which is return self.parent().getbusmonitor() for all widgets except the root one. You could also cache the results..

Comments

0

I found good way I think. I made module with class BusMonitor, and in the same module, after class definition I make instance of this class. Now I can import it from everywhere in project and I retain consistency using classes and encapsulation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.