Bridgekeeper
Bridgekeeper

Reputation: 354

Sharing controller between views

I have a frame which holds 2 views. I want to use a common controller for both of them but not instantiate 2 controllers. How do i achieve this? Singleton? Send the controller as an argument?

Note, the controller will hold temporary data (bad idea?) that will be sent away on command. That's why i only want one controller object. Don't be shy to suggest other design ideas.

First view

Class ViewAlpha
    def __init_(self, parent):
        self.controller = Controller()

    def on_click(self, event):
        self.controller.do_this(event.data)

Second view

Class ViewBeta
    def __init_(self, parent):
        self.controller = Controller()

    def on_click(self, event):
        self.controller.do_this(event.data)

Controller

Class Controller
    def __init_(self):
        self.client = Client()

    def do_this(self, data):
        self.client.store(data)

Upvotes: 0

Views: 71

Answers (1)

Joonazan
Joonazan

Reputation: 1416

Passing things as arguments, just like in your code, is the most common solution to problems like this. If you only need the do_this -method, you can even just pass the method:

beta = ViewBeta(controller.do_this)

Upvotes: 1

Related Questions