简体   繁体   中英

Factory class design issue in java

I have two classes

public class PrepaidPackage {

    private String name;
    private String serviceClassID;
    private boolean isTranferable;

    public boolean isTranferable() {
        return isTranferable;
    }
    public void setTranferable(boolean isTranferable) {
        this.isTranferable = isTranferable;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getServiceClassID() {
        return serviceClassID;
    }
    public void setServiceClassID(String serviceClassID) {
        this.serviceClassID = serviceClassID;
    }
}

other class is

public class PostpaidPackage {
    private String name;
    private boolean isTranferable;
    public boolean isTranferable() {
        return isTranferable;
    }
    public void setTranferable(boolean isTranferable) {
        this.isTranferable = isTranferable;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

I want to create a factory class which on base of package type create relevant class. But if you look at above clasess they dont have same type of methods and variables. So please guide how create interface or abstract class for above class?

Now factory will return class name Package. Would i able to call methods which are not present in other class.

Updates

Please suggest if i break my package into two classes like

public abstract class MyPackage {
    public abstract PackageSpec getSpec();
    public abstract PackagePrepaidDetails getDetail();
}

Now common attributes will be in PackageSpec and prepaid stuff in packageDetails.

Its kind of abstract factory pattern.

public class PrepaidPackage extends MyPackage{
    PackageSpec spec;
    public Spec getSpec() {
      spec = new PackageSpec();
      spec.setTranferable(true)
      spec.setName("abc");
      return spec;
    }
    public PackagePrepaidDetails getDetails() {
      details = new PackagePrepaidDetails ();
      details.setServiceClassID(123)
      return details;
    }
}

public class PostpaidPackage extends MyPackage{
    PackageSpec spec;
    public Spec getSpec() {
        spec = new PackageSpec();
        spec.setTranferable(true)
        spec.setName("abc");
        return spec;
    }
}

A quick fix, not an ideal one is to have an interface that represents all the methods in the Prepaid class and leave them unimplemented in the Postpaid. That will solve the problem in the short term. I would suggest that you have a relook of the classes and the usages to avoid unimplemented methods in the code.

Well for an abstract super class you have to group everything common to both :

public abstract class MyPackage { // not sure you can call a class just "Package"
    private String name;
    private boolean isTranferable;

    public boolean isTranferable() {
        return isTranferable;
    }
    public void setTranferable(boolean isTranferable) {
        this.isTranferable = isTranferable;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

then both inherits from it (the first adds serviceClassID and the second nothing)

your factory function will return a MyPackage (or AbstractPackage, whatever), but to access the specific function you'll have to cast after an instanceof test.

I recomment you to have an interface if you don't have already. You do not neccessarily need it, but it is a good practice if they are so similar:

public interface Package {
    public boolean isTranferable();
    public void setTranferable(boolean isTranferable);
    public String getName();
    public void setName(String name);
}

Then in your calling code, you have a Package from your factory and:

Package p = myFactory.nextPackage();    // or something
if (p instanceof PrepaidPackage) {
    PrepaidPackage prepaid = (PrefpaidPackage)p;
    // and do the thing you want
} else if (p instanceof PostpaidPackage) {
    PostpaidPackage postpaid = (PostpaidPackage)p;
    // amd do the other things
}

Thing you are recommended to llok into is the instanceof operator and type casting .

Two possible design choices you can make:

  1. Have the prepaid package extend postpaid package and your factory then returns objects of type postpaid package, the code which calls the factory is then responsible for inspecting the type.

  2. Have a package interface which defines all of the methods and have postpaid package define the methods to throw an UnsupportedOperationException (ala the way collections defines some operations as optional.) or return some kind of sentinel value (ie null)

For either of the above you could add another method getType() which returns an enum of the various package types you wish to implement, and this could then be used in the code that accesses the factory objects to determine which methods are available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM