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

  • Home
  • SEARCH
  • 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 7967683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:57:43+00:00 2026-06-04T06:57:43+00:00

I’m trying to create an API so that the implementation and class names are

  • 0

I’m trying to create an API so that the implementation and class names are not needed when other programmers use my service. The service performs a variety of operations on “items” but the main contributing factor to how the service should deal with a request for an item is based on the user type. So far I have an Interface (Item) and an implementing service (ItemService) and then several user type classes that extend from this (e.g. CompanyItemService). The user types also have a structure with the super class being User and subclasses such as Company:

public class ItemService<T extends User> extends Item {
    User user;
    public ItemService(User user) {
        this.user = user;
    }

    @Override
    public boolean belongsToUser() {
        //Check all records
    }
}

public class CompanyItemService<T extends Company> extends ItemService<Company> {
    public CompanyItemService(Company user) {
        super(user);
    }

    @Override
    public boolean belongsToUser() {
        //Check company specific records
    }
}

public class User {
    //Common user stuff
}

public class Company extends User {
    //Company specific stuff
}

And so I want to be able to use my service with a call like:

User company = new Company();
//stuff
Item item = new ItemService<Company>(company)
item.belongsToUser();

However when I do this the method in the ItemService gets called instead of the overriding method in the CompanyItemService. Is it possible to get this overriding method without needed to know it belongs in the class CompanyItemService?

Cheers,

Alexei Blue.

The general idea to all this is that when another user type is invented another user specific service can be added if needed leaving the existing code untouched. And also if there are lots of user types I only want people using the API to have to remember to use ItemService and for generics to do the rest.

  • 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-06-04T06:57:44+00:00Added an answer on June 4, 2026 at 6:57 am

    You need to use the abstract factory pattern. In short, you need a static method someplace which knows about all the possible ItemService subclasses and, given an Item object or a Class which is a subclass of User, returns an instance of the correct ItemService subclass. In other words, something like

    public static ItemService<?> instance(Class<? extends User> c) {
        if (c == Customer.class)
            return new CustomerItemService();
        else if (c == Employee.class)
            return new EmployeeItemService();
        ...
    }
    

    Then you can say

    ItemService<?> service = ItemService.instance(Customer.class);
    

    Alternatively, you could build a map:

    Map<Class<? extends User>, Class<? extends ItemService>> services = 
        new HashMap<Class<? extends User>, Class<? extends ItemService>>() {{
        put(Customer.class, CustomerItemService.class);
        put(Employee.class, EmployeeItemService.class);
        // more...
    }};
    

    Then instance() could just look like

    ItemService<? extends User> instance(Class<? extends User> c) {
        return services.get(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.