its often very difficult to apply extensions or patches to an existing application. consider the steps required. first, a software developer has to modify some source code. then a platform engineer rebuilds and repackages the system, creating a detailed installation guideline. finally, a deployment engineer redeploys the software at customer site. hopefully, it all works. is there any method to make these tasks simpler?
problem 【程序编程相关:Using the Strategy D】java.net">patchexpert is a simple tool to satisfy this requirement. it can insert software extensions or patches through some predefined extension points. 【推荐阅读:Java Tech: Acquire I】
testmain is a simple application used to print out an onscreen triangle of asterisk ("*") characters. it has a calculator class to calculate the level of triangle according to the given integer and asks triangleprint to print out the triangle. 【扩展信息:Principles, Patterns】
lets look at an example first to see how difficult it is to apply a patch to an existing application.
heres the source code for a basic implementation. calculator uses the singleton pattern, and is used to figure out how many asterisks to print out. this implementation simply doubles the given argument.
public class calculator { static calculator instance = new calculator(); public static calculator getinstance() { return instance; } protected calculator() { } public int calc(int num) { return num * 2; } }triangleprint prints out the triangle line by line, according to the given level.
public class triangleprint { int level; public triangleprint(int level) { this.level = level; } public void print() { for (int i = 1; i <= level; i++) system.out.println(getline(i)); } protected string getline(int num) { stringbuffer sb = new stringbuffer(); for(int i = 1; i <= num; i++) sb.append("*"); return sb.tostring(); } public int getlevel() { return level; } }finally, testmain exercises the code by getting a calculator instance, calling calc(), and sending the result on to triangleprint.
... 下一页