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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:32:27+00:00 2026-05-27T02:32:27+00:00

Let’s consider the following simplified Resource hierarchy: public abstract class Resource { static public

  • 0

Let’s consider the following simplified Resource hierarchy:

public abstract class Resource {
    static public boolean accepts(String resource);
}

public class AudioResource extends Resource {
    static public boolean accepts(String resource) {
        //Check if we can proceed this resource as audio
        //Some complex logic there ...
    }
}

public class VideoResource extends Resource {
    static public boolean accepts(String resource) {
        //Check if we can proceed this resource as video
        //Another complex logic there
    }
}

Resource has dozens subclasses and number grows. Each sub-resource:

  • has some logic to determine if it accepts resource or not. E.g. it may parse resource URL with regexp or something;
  • is not singleton by design;

Now, we want to create a factory which iterates through all available subclasses and creates one which accepts resource (checks it using the accepts method).

Something like this (let’s suppose for a moment that Java has static methods polymorphism):

public class ResourceFactory {

    private static List<Class<Resource>> registry;
    {
        //Populate registry once on start
    }


    public static Resource createResource(String resource) {
        for (Class<Resource> clazz : registry) {
            if (clazz.accepts(resource)) 
                return clazz.createInstance(resource);
        }
    }    
}  

Unfortunately (or not?), Java doesn’t support polymorphic static methods. Considering that, what are the possible ways to design Resource and ResourceFactory?

  • 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-27T02:32:28+00:00Added an answer on May 27, 2026 at 2:32 am

    You could use:

    public interface Resource {
      // some methods
    } 
    
    public interface ResourceFactory {
      boolean acceptsResource(String resource);
      Resource createResource(String resource) throws UnsupportedResourceException;
    }   
    
    public final MultiResourceFactory implements ResourceFactory{
       private static final ServiceLoader<ResourceFactory > resourceFactoryLoader
           = ServiceLoader.load(ResourceFactory .class);
       private static final MultiResourceFactory INSTANCE;
    
      private MultiResourceFactory(){
      }
    
      public static MultiResourceFactory getInstance(){
        if (INSTANCE == null){
            INSTANCE = new MultiResourceFactory();
        }
        return INSTANCE;
      }
      @Override
      public boolean acceptsResource(String resource){
          for (ResourceFactory resourceFactory : resourceFactoryLoader) {
             if (resourceFactory.acceptsResource(resource)){
                 return true;
             }
          }
          return false;
      }
    
      @Override
      public Resource createResource(String resource) throws UnsupportedResourceException{
          for (ResourceFactory resourceFactory : resourceFactoryLoader) {
             if (resourceFactory.acceptsResource(resource)){
                 return resourceFactory.createResource(resource);
             }
          }
          throw new UnsupportedResourceException(resource);
      }   
    

    See ServiceLoader for how to register the factories:
    http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html

    Note: the code is untested

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

Sidebar

Related Questions

Let's say I have following POJO's using Hibernate. public class User { private String
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say you have a class called Customer, which contains the following fields: UserName
Let's consider the following scenario: I've a master table with a detail table. The
Let's imagine I have a Java class of the type: public class MyClass {
let's say I have the following string: string s = A B C D
Let's say that I have classes like this: public class Parent { public int
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say I have following string: string test = False; I can't loop the
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.