-1

Background: I need to render the same information in two text formats. To do this, I have a set of model classes that store the appropriate information. I then need to render that same information in two different ways.

Each model has a list of 'actions'. Different actions have different attributes.

How do I handle this in my rendering code without a switch on the type of action? I'm working on the assumption that a switch statement on object type is a bad thing.

if isinstance(action, ActionOne):
    render_action_one(action)
elif isinstance(action, ActionTwo):
    render_action_two(action)

The action classes, being part of the model, don't know how to do this rendering.

Edit: This happens to be in Python, but I don't think that matters so much here.

4
  • what programming language are you using?
    – gnat
    Mar 16, 2017 at 9:28
  • model.RenderAction()
    – Ewan
    Mar 16, 2017 at 9:32
  • 1
    I don't have time for a proper answer, but take a look on the Visitor pattern Mar 16, 2017 at 10:12
  • @BartvanIngenSchenau that looks promising. Thanks.
    – wrgrs
    Mar 16, 2017 at 14:44

1 Answer 1

-2

You don't. you are doing it wrong.

Instead of render methods on your model, have multiple Views for the same model.

5
  • That's basically what I'm trying to do. There's more than one type of Action in the model. How do I associate the correct View with the type of Action when I come to render it?
    – wrgrs
    Mar 16, 2017 at 9:40
  • you will have to expand on the language and frameworks you are using. there shouldnt be a problem just calling model.action()
    – Ewan
    Mar 16, 2017 at 9:44
  • Hm. Python, no framework.
    – wrgrs
    Mar 16, 2017 at 9:49
  • urg. upgrade to .net?
    – Ewan
    Mar 16, 2017 at 9:51
  • stackoverflow.com/questions/68986/…
    – Ewan
    Mar 16, 2017 at 9:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.