我们正在开发核心组件,对于少数业务场景,我们为每个方案开发了自定义代码。 我们希望在以可配置的方式执行核心业务逻辑之后调用这些自定义代码业务逻辑,而无需在核心组件中进行代码修改。
这种情况是否有可用的设计模式?
我们正在使用Spring Framework,有没有可用的功能init?
示例代码:
核心模块:
Core.java
public String save (Map<String,String> map){
// my business logic here to insert into to core table
}
自定义模块:
Entity1Custom.java
public String customSave(Map<String,String> map){
// my custom save logic only for Entity1
}
Entity2Custom.java
public String customSave(Map<String,String> map){
// my custom save logic only for Entity2
}
如何在以可配置的方式执行我的核心保存方法之后调用自定义Entity1和Entity2方法,以便将来如果再添加任何实体,那么我不应该更改我的代码。
答案 0 :(得分:0)
您可以使用的简单事例示例(使用默认方法的Java 8示例,但您可以轻松调整):
public interface BusinessProcessor {
default void doBefore() {
}
void doCoreBusiness();
default void doAfter() {
}
}
public class MyCoreBusiness implements BusinessProcessor {
@Override
public void doCoreBusiness() {
// Do core business there
}
}
public class MyCustomExtendedBusiness extends MyCoreBusiness {
@Override
public void doBefore() {
// Do something before
}
}
现在,正如您的问题评论中所解释的那样,不需要特定的设计模式来实现这一点,因此需要使用对象概念。保持简单。
答案 1 :(得分:0)
这是我的变体,类似于@kjj的变体,但避免了界面上的默认实现。
public class CoreLogic {
protected void doCoreLogic() {
// Core logic here
}
protected void doSpecificLogic {
// Leave this implementation empty in case
// some subclass doesn't need it. Alternatively,
// make it abstract (and the the class abstract too)
// to force subclasses to provide an implementation.
}
public void doAction {
doCoreLogic();
doSpecificLogic(); // Works even if this method is abstract
}
}
public class SpecificLogic1 extends CoreLogic {
protected void doSpecificLogic() {
// Specific logic here.
}
}
请注意,正如其他人也提到的那样,这不仅仅是一种“设计模式”,而只是工作中的基本OO原则。
答案 2 :(得分:0)
我计划按照以下方法实施上述方案。请分享是否有任何其他现有方法/设计模式支持我的方案。
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author pandian
*
*/
public class Core {
public static void main(String[] arg) {
Core core = new Core();
Map<Object, Object> obj = new HashMap<Object, Object>();
core.create(obj);
}
public Map create(Map<Object, Object> obj) {
// TODO implement core business logic
System.out.println("Core Business Logic Completed");
try {
obj = invokeAfter(obj);
} catch (SecurityException | IllegalArgumentException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
public Map invokeAfter(Map<Object, Object> obj) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
Class params[] = {};
// TODO below list will be injected using spring configuration in
// configurable approach
List<String> afterClassNameList = new ArrayList<String>();
afterClassNameList.add("Custom1");
afterClassNameList.add("Custom2");
if (!afterClassNameList.isEmpty()) {
for (String className : afterClassNameList) {
System.out.println("Class Name::" + className);
Class thisClass = Class.forName(className);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("create", params);
obj = (Map<Object, Object>) thisMethod.invoke(iClass, obj);
}
}
return obj;
}
}
class Custom1 {
public Map create(Map<Object, Object> obj) {
// TODO implement custom business logic
return obj;
}
}
class Custom2 {
public Map create(Map<Object, Object> obj) {
// TODO implement custom business logic
return obj;
}
}