[英]Java wrap a child class method such that the parent method gets called first and then calls child class method
I have a class that has a bunch of methods.我有一个 class 有很多方法。 This class is copied in several different places with slight variances in what each method does.
这个 class 被复制到几个不同的地方,每种方法的作用略有不同。 What I want to do is move the common code into a class with similar method signatures, but the parameter is a base class that the object in the parameter of the other class inherits.
What I want to do is move the common code into a class with similar method signatures, but the parameter is a base class that the object in the parameter of the other class inherits. When the method in the inherited class is called, I want it to redirect to the base class method, do that code, then somehow call back into the inherited class method.
当调用继承的 class 中的方法时,我希望它重定向到基本的 class 方法,执行该代码,然后以某种方式回调到继承的 class 方法。 I cannot provide the exact code, but I hope this example is sufficient.
我无法提供确切的代码,但我希望这个例子就足够了。
class BaseTableVersion{
Long getId();
void setId(Long id);
String getVersion();
void setVersion(String version);
TableType getTableType();
}
class TableVersion extends BaseTableVersion{
Long getVersionNumber();
void setVersionNumber(Long versionNumber);
}
//this class needs to exists in some capacity. I can't make it inherit a class as it is now because
//it already inherits another class (which I can't show or change) and java doesn't support multiple.
// inheritence. I can add members or change methods
class TableVersionRules{
void create(TableVersion tableVersion){
//do stuff
}
void versionChanged(TableVersion tableVersion){
//do stuff
}
}
//this class doesnt need to exists, its just to hopefully illustrate what i want.
class BaseTableVersionRules<B extends BaseTableVersion>{
void create(B tableVersion){
//do stuff
//call TableVersionRules.create(tableVersion) somehow
}
void versionChanged(B tableVersion){
//do stuff
//call TableVersionRules.versionChanged(tableVersion) somehow
}
}
So, conceptually what I want is to somehow link TableVersionRules and BaseTableVersionRules such that when TableVersionRules.versionChanged is called, it will call BaseTableVersionRules.versionChanged first, and then do what is in TableVersionRules.versionChanged.因此,从概念上讲,我想要以某种方式链接 TableVersionRules 和 BaseTableVersionRules,这样当调用 TableVersionRules.versionChanged 时,它将首先调用 BaseTableVersionRules.versionChanged,然后执行 TableVersionRules.versionChanged 中的操作。 What design pattern could I use for this, or does this just defy polymorphism?
我可以为此使用什么设计模式,或者这是否违反了多态性?
It looks like an inheritance with child class calling parent class using "super" keyword.它看起来像一个 inheritance 与子 class 调用父 class 使用“超级”关键字。 Something like that:
像这样的东西:
class TableVersionRules extends BaseTableVersionRules {
void versionChanged(...) {
super.versionChanged(...);
// Do some more stuff here.
}
}
"Super" will call the implementation from parent class, and then you can still add some more code here. “超级”将从父 class 调用实现,然后您仍然可以在此处添加更多代码。
Does it make sense?是否有意义?
UPDATE: If you can't use "extends" since class already extends something else, try using a local field:更新:如果您不能使用“扩展”,因为 class 已经扩展了其他内容,请尝试使用本地字段:
class TableVersionRules extends SomeOtherStuffThatNeedsToBeThere {
private final BaseTableVersionRules delegate;
void versionChanged(...) {
delegate.versionChanged(...);
// Do some more stuff here.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.