java中工厂类设计问题
我有两个类,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您还没有界面,我建议您拥有一个界面。您不一定需要它,但如果它们非常相似,那么这是一个很好的做法:
然后在您的调用代码中,您有一个来自工厂的包,并且:
建议您进入的是 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:
Then in your calling code, you have a Package from your factory and:
Thing you are recommended to llok into is the instanceof operator and type casting.
一种快速解决方案(但不是理想的解决方案)是拥有一个代表 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.
对于抽象超类,您必须将两者共有的所有内容分组:
然后两者都继承自它(第一个添加
serviceClassID
,第二个不添加)您的工厂函数将返回 MyPackage (或 AbstractPackage ,无论如何) ,但要访问特定函数,您必须在
instanceof
测试后进行强制转换。Well for an abstract super class you have to group everything common to both :
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.您可以做出两种可能的设计选择:
延长预付费套餐的期限
后付费套餐和您的工厂
然后返回类型的对象
后付费套餐,代码如下
然后打电话给工厂
负责检查类型。
有一个包接口
定义了所有的方法并且有
后付费套餐定义方法
扔一个
UnsupportedOperationException(阿拉
集合定义某些内容的方式
操作为可选。)或返回
某种哨兵值(即 null)
对于上述任何一种,您都可以添加另一个方法 getType() ,它返回您希望实现的各种包类型的枚举,然后可以在访问工厂对象的代码中使用它以确定哪些方法可用。
Two possible design choices you can make:
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.
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.