1
\$\begingroup\$

When I last did a lot of Java programming, I'd do this:

  • All classes in a package would be default (package) visibility.
  • Each package would have a public PackageNamePack factory class, e.g. BusinessPack.
  • And the package would have a number of interfaces.
  • The package factory would have methods like newBusiness(...) which would instantiate new BusinessImplWhatever objects, cast as a Business interface.

The factory class:

package io.eaternet.adapters.framework;

public class FrameworkPack {
    public static Business newBusiness(String name, String address, String city, String zipcode, String origKey) {
        return new BusinessImpl(name, address, city, zipcode, origKey);
    }

    public static Inspection newInspection(String origKey, String businessOrigKey, String score, String date) {
        return new InspectionImpl(origKey, businessOrigKey, score, date);
    }
}

An interface:

package io.eaternet.adapters.framework;

public interface Business {
    public String getName();
    public String getAddress();
    public String getCity();
    public String getZipcode();
    public String getOrigKey();
}

A package-visibility implementation class:

package io.eaternet.adapters.framework;

class BusinessImpl implements Business {
    String name, address, city, zipcode, origKey;

    BusinessImpl(String name, String address, String city, String zipcode, String origKey) {
        this.name = name;
        this.address = address;
        this.city = city;
        this.zipcode = zipcode;
        this.origKey = origKey;
    }

    public String getName() { return name; }
    public String getAddress() { return address; }
    public String getCity() { return city; }
    public String getZipcode() { return zipcode; }
    public String getOrigKey() { return origKey; }
}

I've started a new Java library which follows this pattern, and am wondering if it still make sense. This particular code is part of a CSV parser, which is why everything is a string.

\$\endgroup\$
2
  • \$\begingroup\$ you're expected to shows us some code. there's not much to review. you're asking if it makes senses. if it doesn't make sense to you, how would it make sense to anyone else? \$\endgroup\$ Commented Feb 1, 2015 at 19:50
  • \$\begingroup\$ Thanks. I've updated with working code from the project. \$\endgroup\$
    – Dogweather
    Commented Feb 2, 2015 at 3:37

1 Answer 1

2
\$\begingroup\$
  1. FrameworkPack is not a factory.
  2. Business declares methods that automatically are public, remove the word public.
  3. BusinessImpl looks like a ReadOnly-Class, declare the fields as final.
  4. If FrameworkPack shall never be instanicated, create a private Constructor and make the class final so noone is able to create a instance.

You can use the Abstract-Factory and Singleton- pattern by ease.

public interface Framework {
    Business newBusiness(String name, String address, String city, 
                         String zipcode, String origKey);
    Inspection newInspection(String origKey, String businessOrigKey, 
                             String score, String date);
}
public interface Business {
    String getName();                   
    String getAddress();                
    String getCity();                   
    String getZipcode();                
    String getOrigKey();                
}
public final class FrameworkPack implements Framework {
    private FrameworkPack(){};
    private static final FrameworkPack INSTANCE; 
    public static final FrameworkPack getInstance(){
        if (INSTANCE==null) {
           ensureExistingInstance();
        }
        return INSTANCE;
    }
    private synchronized void ensureExistingInstance(){
       if(INSTANCE==null){
         INSTANCE = new FrameworkPack();
       }
    }

    public BusinessImpl newBusiness(String name, String address, String city, 
                         String zipcode, String origKey){
      return new BusinessPack(name, address, city, zipcode, origKey);
    }
    ...
}
public final class BusinessPack {
    private final String name;
    private final String address;
    private final String city;
    private final String zipCode;
    private final String origKey;
    BusinessPack(String name, String address, String city, 
                         String zipcode, String origKey) {
      this.name = name;
      this.address = address;
      this.city = city;
      this.zipcode = zipcode;
      this.origKey = origKey;
    }
    ...
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.