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 4617898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:11:22+00:00 2026-05-22T02:11:22+00:00

I’ve two different type of users, and I’ve mapped them to two Java classes

  • 0

I’ve two different type of users, and I’ve mapped them to two Java classes UserWheel and UserSea and they have a common abstract superclass called User. The data saved for these user types is about the same, but the behavior is different.

Then I created an abstract class called UserCollection with derived classes UserWheelCollection and UserSeaCollection to search for subusers or load a subuser.

Then I’ve added an abstract method to UserCollection class with signature

public abstract List<User> listAllSubusers()

this is because the implementation will differ. Each User created will be a UserWheel or a UserSea, depending on which method was called, but also all the rest of the implementation is quite different.

Then I want to add a new method to UserCollection with signature public User loadById(int idUser). In this case the implementation would be the same except for the fact that the User returned would be an instance of either UserWheel or UserSea. I’m reluctant in this case to use an abstract method in the base class because of code duplication.

I could check the concrete class of UserCollection with instanceof and create an appropriate subclass, but it doesn’t seem object oriented and breaks the open-close principle.

Another idea would be to add an abstract method createNewUser() to UserCollection and concrete implementations in the subclasses to return a new instance, so the base class would just call this createNewUser() method.

Do you think this second path makes sense? Or you would organize things in a different way and how?


UPDATE. The current situation is:

abstract class User
   public String getAddress()
   public void setAddress()
   ...

class UserSea extends User
class UserWheel extends User

abstract class UserCollection
   protected abstract User createNewUser();
   public abstract List<User> listAllSubUsers();
   public User loadById(int idUser) {
       User newUser = createNewUser();
       //populate it
       return newUser;
   }

class UserSeaCollection
   protected User createNewUser() {
        return new UserSea();
   }
   public List<User> listAllSubusers()

class UserWheelCollection
   protected User createNewUser() {
       return new UserWheel();
   }
   public List<User> listAllSubusers()

I tried to understand the strategy pattern, as suggested by trashgod, and here is my first attempt:

interface SubuserManagement
    List<User> listAllSubUsers();
    ...

interface UserCrud
   void create();
   User readById(int idUser);
   void update();
   void delete();

class UserSeaCollection implements SubUserManagement, UserCrud

   private SubUserManagement subuserBehavior = new SubUserManagementSeaImplementation();
       private UserCrud userCrudBehavior = new UserCrud();

   void create {
       subUserBehavior.create();
   }
   ...

class UserWheelCollection implements SubUserManagement, UserCrud
       ...

class SubUserManagementWheelImplementation implements SubUserManagement
    List<User> listAllSubUsers();

class SubUserManagementSeaImplementation implements SubUserManagement
    List<User> listAllSubUsers();

class UserCrudImplementation implements UserCrud //only 1 implementation
   void create();
   User readById(int idUser);
   void update();
   void delete();

In this first attempt, I’ve created UserCollectionWheel and UserCollectionSea that don’t share anymore a common superclass, but implement the same interfaces. The actual implementation is in external classes.

Now UserCollectionWheel and UserCollectionSea are really the same class, with the only difference of the behavior that I assign to them. Alternatively I could write just one class with setters:

UserCollection userColl = new UserCollection();
userColl.setSubUserBehavior(new SubUserManagementSeaImplementation());
userColl.setCrudBehavior(new UserCrud());

But the initialization would be cumbersome, especially if I had more behavior classes. So what am I doing wrong? How to organize this properly?

UPDATE 2: I wrote a blog post with the design that I’ve implemented.

  • 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-22T02:11:23+00:00Added an answer on May 22, 2026 at 2:11 am

    Instead of inheriting behavior, consider encapsulating it using interfaces in a strategy pattern. Users would differ in having either of two concrete implementations of an interface ListSubUsersStrategy, interface CreateUserStrategy, etc.

    See also the related bridge pattern.

    Addendum: In the example below, every user has a concrete strategy for finding sub-users. In particular, listAllSubUsers() invokes the interface method, automatically dispatching to the right concrete implementation. The pattern doesn’t relieve you of writing concrete implementations of the interface, but it does de-couple them, ensuring that changing one won’t break another.

    Console:

    A has wheel users.
    B has sea users.
    C has wheel users.
    

    Code:

    import java.util.ArrayList;
    import java.util.List;
    
    /** @see http://stackoverflow.com/questions/6006323 */
    public class UserMain {
    
        private static final List<User> users = new ArrayList<User>();
    
        public static void main(String[] args) {
            users.add(new User("A", new WheelStrategy()));
            users.add(new User("B", new SeaStrategy()));
            users.add(new User("C", new WheelStrategy()));
            for (User user : users) {
                user.listAllSubUsers();
            }
        }
    
        private static class User {
    
            private String name;
            private SubUsersStrategy suStrategy;
    
            public User(String name, SubUsersStrategy suStrategy) {
                this.name = name;
                this.suStrategy = suStrategy;
            }
    
            public void listAllSubUsers() {
                System.out.print(name + " manages ");
                List<User> subUsers = suStrategy.getList();
            }
        }
    
        private interface SubUsersStrategy {
    
            List<User> getList();
        }
    
        private static class WheelStrategy implements SubUsersStrategy {
    
            @Override
            public List<User> getList() {
                System.out.println("wheel users.");
                return null;
            }
        }
    
        private static class SeaStrategy implements SubUsersStrategy {
    
            @Override
            public List<User> getList() {
                System.out.println("sea users.");
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I am currently running into a problem where an element is coming back from
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.