java中工厂类设计问题

发布于 2024-10-19 22:45:34 字数 2142 浏览 1 评论 0原文

我有两个类,

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;
    }
}

其他类是

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;
    }
}

我想创建一个工厂类,它基于包类型创建相关类。但是如果你看看上面的类,它们没有相同类型的方法和变量。那么请指导如何为上述类创建接口或抽象类?

现在工厂将返回类名Package。我是否能够调用其他类中不存在的方法。

更新

请建议我是否将我的包分成两个类,例如

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

现在通用属性将在 PackageSpec 中,预付费内容将在 packageDetails 中。

它是一种抽象工厂模式。

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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-10-26 22:45:34

如果您还没有界面,我建议您拥有一个界面。您不一定需要它,但如果它们非常相似,那么这是一个很好的做法:

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

然后在您的调用代码中,您有一个来自工厂的包,并且:

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
}

建议您进入的是 instanceof 运算符和类型转换

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.

夏有森光若流苏 2024-10-26 22:45:34

一种快速解决方案(但不是理想的解决方案)是拥有一个代表 Prepaid 类中所有方法的接口,并让它们在 Postpaid 中未实现。这将在短期内解决问题。我建议您重新查看类和用法,以避免代码中未实现的方法。

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.

无远思近则忧 2024-10-26 22:45:34

对于抽象超类,您必须将两者共有的所有内容分组:

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;
    }
}

然后两者都继承自它(第一个添加 serviceClassID ,第二个不添加)

您的工厂函数将返回 MyPackage (或 AbstractPackage ,无论如何) ,但要访问特定函数,您必须在 instanceof 测试后进行强制转换。

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.

染火枫林 2024-10-26 22:45:34

您可以做出两种可能的设计选择:

  1. 延长预付费套餐的期限
    后付费套餐和您的工厂
    然后返回类型的对象
    后付费套餐,代码如下
    然后打电话给工厂
    负责检查类型。

  2. 有一个包接口
    定义了所有的方法并且有
    后付费套餐定义方法
    扔一个
    UnsupportedOperationException(阿拉
    集合定义某些内容的方式
    操作为可选。)或返回
    某种哨兵值(即 null)

对于上述任何一种,您都可以添加另一个方法 getType() ,它返回您希望实现的各种包类型的枚举,然后可以在访问工厂对象的代码中使用它以确定哪些方法可用。

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 (i.e. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文