dev
dev

Reputation: 109

Design Pattern recommendation

I'm developing an application that is to be run in two separate offices. The UI needs to be consistent, however some of the buttons on the UI will change depending on which office is running the application along with the functional logic behind those buttons. Which design pattern is recommended to code the following in Python? The aim is to be able to re-use components for the application being developed for both offices without having to maintain duplicate code.

To help illustrate with a very simple example:

Sample UI

In the attached screenshot - if the application is being run in Office A only show "Contact Us" with it's own code logic, however if the application is running in Office B, show both "Contact Us" and "Lets work together" with their own separate code logic different from Office A.

Thanks

Upvotes: 0

Views: 79

Answers (1)

Jan Wilamowski
Jan Wilamowski

Reputation: 3599

You could try extracting the diverging behavior to separate modules or functions and import or call the one that is appropriate, based on your runtime configuration. A simple approach would be to map the name of the current runtime to function names, e.g.:

def show_contact_office1():
    # display buttons and set up callbacks

def show_contact_office2():
    # display some other buttons

contact_sections = {
    'office1': show_contact_office1,
    'office2': show_contact_office2,
}

def show_page(config):
    current_office = config.get_office()
    show_contact = contact_sections[current_office]
    show_contact()

If there are many such variable components you could wrap them into a class with a default implementation as the parent and specific overrides as child classes. Then instantiate the appropriate class in your code, similarly as shown above.

Upvotes: 1

Related Questions