Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:20:09+00:00 2026-05-24T17:20:09+00:00

we are implementing a system with Strategy pattern. Some callers to a specific methods

  • 0

we are implementing a system with Strategy pattern.

Some callers to a specific methods require a Hashmap of data while in some case it would be a user-defined DTO. Some need both.

How do we decide which return type to use, instead of an Arraylist containing Hashmap and DTO ? Is there anything more approporiate?

Example:

Some caller needs only 2 strings and prefers a Hashmap.

Some other caller to the same API needs 12 values which are suited to put in a DTO. 1st caller does not need all the elements in the DTO.

3rd caller needs all 14 values and hence both structures.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:20:09+00:00Added an answer on May 24, 2026 at 5:20 pm

    If some callers require Map (BTW don’t use concrete types like HashMap in your interfaces) and others an object, there might be two reasons for that:

    • the method returns completely different things depending on the arguments (e.g. if the flag is set, return a map of privileges whereas if it’s not, return object representing user himself)

    • you are returning different view of the same entity

    In the first case your interface is seriously flawed and should be redesigned. Probably you need two methods performing different tasks. What do you mean by “different callers“? Depending on who calls the method, you decide what to return?

    In the second case think of a more general model and return it. Then let the clients transform it to appropriate format. There are several ways to do this:

    • return format-agnostic type and provide methods converting to map and to object so the client can decide. For instance if you are not sure whether the client prefers weakly-typed map with properties or strongly-typed object having the same attributes/fields, return simple wrapper having toMap and toObject methods. Note that in this case the internal representation is not important, client always uses a view and you can always add new format in the future

    • return generic Model class having two subclasses: MapModel and ObjectModel, exposing appropriate getters in subclasses. Do not use instanceof but Visitor pattern instead. In this model you always return a single value and the client has to be able to deal with every format. If you add a new format (like XmlModel), all the places where Model is used need to be customized to deal with new type. However in this scenario you don’t need to transform the internal representation to different formats.

    Note that returning an Object or any variation of that is simply landing in dynamic languages land where you no longer have any static-typing help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in implementing a Forth system, just so I can get some experience
I'm implementing an image cache system for caching downloaded image. My strategy is based
I'm implementing system which uses role based access to different methods in classes. For
I am implementing a betting system and each user has a balance , how
I am implementing a simple license-file system, and would like to know if there
I’ve been tasked with implementing a system for continuously receiving large amounts of data
I am implementing a contest system in which the user has to choose the
I am implementing a system where the user can Reset their password if they
I'm implementing a system to send Messages between different parts of a program I'm
I am implementing a system where I need real-time updates. I have been looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.