.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Using factory pattern base on class name


Factory pattern helps you to centralize your object construction. Hence, you have an easy way to create object while developing. Otherwise, it also create a good infrastructure design for maintenance. I mentioned the simple way to create a factory design pattern before, so this is another way to use factory pattern. It maybe more automatic on creating multiple objects.

The mechanism of this method is the at one url location, the factory will recursively search for all class file with any predefined name at all the class paths of the sub-packages. Then it will create a new instance of each found java class file. So, prerequisites for this factory way is the way you name your classes which are required to be constructed.

In this sample code, i want to create instances of any class files named ‘Processor’. The ProcessFactory class in bellow image will find for Processor class at packages ‘data’ and ‘login’ and construct them.

/**
     * Initialize processor.
     */
    public static void initializeProcessor()
    {

        final List<String> lstPackages = new ArrayList<String>();
        getSubPackages(lstPackages, ProcessFactory.class);
        for (String subPackage : lstPackages)
        {
            try
            {
                Class.forName(
                    ProcessFactory.class.getPackage().getName() + "."
                            + subPackage + ".Processor").newInstance();
            }
            catch (SecurityException | ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            }
        }
    }

where getSubPackages is the function created in the last post and will be duplicated here:

public static void getSubPackages(
        final List<String> lstSubPackages,
        final Class<?> anyClass) throws NullPointerException
    {
        URL resource = anyClass.getResource("");
        if (resource != null)
        {
            File processDir = new File(resource.getPath());
            File[] lstFiles = processDir.listFiles();
            if (lstFiles != null)
            {
                for (File file : lstFiles)
                {
                    if (file.isDirectory())
                    {
                        lstSubPackages.add(file.getName());
                    }
                }
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: