1

In my MVP app, my model (a service) returns a collection of ISession pointers. Each pointer points to an object of a derived type HttpSession or HttpsSession. The model (the service) knows nothing about any GUI, Views, Presenters, etc.

In my Presenter, I need to create and show a View, which should be different depending on the type of the session. How can I do this without using dynamic_cast check on the pointers and without making ISession -- and thus the model (the service) -- know about the existence of the GUI, MVP, etc.?

Note: In general, I have the same problem with writing the sessions' data into DB. The Presenter should call the database manager and it should write the sessions into the DB. But I don't understand how it can differentiate between the two types of the sessions without using the check on the dynamic type of the session. I just decided to choose one of the problems and ask here. Help me, please. I read and read about visitor, strategy, etc. patterns, but still cannot grab how developers normally do it.

13
  • If you need to distinguish these session types, add methods to ISession to make them distinguishable. Implementing the visitor pattern is one way to do this, but maybe a virtual bool is_secure() const = 0 method would be enough. If this is actually about HTTP vs HTTPS, note that nowadays the only reasonable response to a HTTP request is to respond with a permanent redirect to the HTTPS version (with very few exceptions). Such a redirect would typically be handled in a reverse proxy, before the backend with your business logic is reached.
    – amon
    Feb 13 at 18:53
  • @amon, thank you for your comment! No, in reality I put here Https and Http just for example. I honestly read that it's nearly the same -- to use an enum or smth like that -- to determine the real dynamic class of a object. That this is bad.
    – JenyaKh
    Feb 14 at 5:52
  • 1
    Typically a Presenter (or Controller) in the MVC or MVP pattern would already know the exact type for each different Model it needs because it would either provide that to its View or convert it into a ViewModel. So instead of using ISession perhaps you could remove inheritance and any relationships between the classes then use separate functions or services for each Model, returning the correct type directly. Feb 14 at 7:20
  • @BenCottrell Thank you for your comment! But the model in my case is a network service which gets a list of sessions (Type1 or Type2 sessions) from network and then the service returns the list to the Presenter and the Presenter would use diffrerent View forms for different types of sessions. So unfortunately, Presenter really does not know the type of each particular session in the returned list of sessions.
    – JenyaKh
    Feb 14 at 9:10
  • 1
    @JenyaKh Don't use design patterns unless they solve an actual problem. The OCP is a somewhat ill-defined principle, and it doesn't really apply in an internal code base where you can always refactor everything without having to maintain stable APIs or ABIs.
    – amon
    Feb 14 at 13:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.