使用IntelljIDEA生成接口的类继承图及装饰器模式
类图生成方法###
以一个装饰器模式实现数学运算的例子为例。
-
安装 Intellj Ultimate , lience server: http://xdouble.cn:8888/
-
在类上右键点击 class diagram :
-
在得到的类的框框上 “双指单击”或右键 , 选择 show Implementations :
-
得到的实现类列表上, Ctrl + A 全选
-
Enter 得到类图结果,上面有 导出图片功能。
-
可以查看接口及实现类的覆写方法
-
调整布局
-
添加额外的类
如果发现还有点单独的接口有关联但是不在上述继承体系里, 可以添加额外的 class diagram 并按上如法炮制。 -
导出图片保存
装饰器代码###
Function.java 函数接口, sources 是被装饰的内层函数运算。
package zzz.study.patterns.decorator.func;
public abstract class Function {
protected Function[] sources;
public Function(Function[] sources) {
this.sources = sources;
}
public Function(Function f) {
this(new Function[] {f});
}
public abstract double f(double t);
public String toString() {
String name = this.getClass().toString();
StringBuffer buf = new StringBuffer(name);
if (sources.length > 0) {
buf.append('(');
for (int i=0; i < sources.length; i++) {
if (i > 0)
buf.append(",");
buf.append(sources[i]);
}
buf.append(')');
}
return buf.toString();
}
}
Constant.java :常量函数
package zzz.study.patterns.decorator.func;
public class Constant extends Function {
private double constant;
public Constant() {
super(new Function[] {});
}
public Constant(double constant) {
super(new Function[]{});
this.constant = constant;
}
public double f(double t) {
return constant;
}
public String toString() {
return Double.toString(constant);
}
}
T.java : 线性函数
package zzz.study.patterns.decorator.func;
public class T extends Function {
public T() {
super(new Function[] {});
}
public double f(double t) {
return t;
}
public String toString() {
return "t";
}
}
Square.java :平方函数
package zzz.study.patterns.decorator.func;
public class Square extends Function {
public Square() {
super(new Function[] {});
}
public Square(Function f) {
super(new Function[] {f});
}
public double f(double t) {
return Math.pow(sources[0].f(t),2);
}
public String toString() {
StringBuffer buf = new StringBuffer("");
if (sources.length > 0) {
buf.append('(');
buf.append(sources[0]);
buf.append('^');
buf.append(2);
buf.append(')');
}
return buf.toString();
}
}
ExpDouble.java :指数函数
package zzz.study.patterns.decorator.func;
public class ExpDouble extends Function {
private double expDouble; // 指数的底数
public ExpDouble() {
super(new Function[] {});
}
public ExpDouble(double expDouble, Function f) {
super(new Function[] {f});
this.expDouble = expDouble;
}
public double f(double t) {
return Math.pow(expDouble, sources[0].f(t));
}
public String toString() {
StringBuffer buf = new StringBuffer("");
if (sources.length > 0) {
buf.append('(');
buf.append('(');
buf.append(expDouble);
buf.append(')');
buf.append('^');
buf.append(sources[0]);
buf.append(')');
}
return buf.toString();
}
}
Pow.java :幂函数
package zzz.study.patterns.decorator.func;
public class Pow extends Function {
private double pow; // 幂函数的指数
public Pow() {
super(new Function[] {});
}
public Pow(Function f, double pow) {
super(new Function[] {f});
this.pow = pow;
}
public double f(double t) {
return Math.pow(sources[0].f(t), pow);
}
public String toString() {
StringBuffer buf = new StringBuffer("");
if (sources.length > 0) {
buf.append('(');
buf.append(sources[0]);
buf.append('^');
buf.append('(');
buf.append(pow);
buf.append(')');
buf.append(')');
}
return buf.toString();
}
}
Arithmetic.java :四则运算
package zzz.study.patterns.decorator.func;
public class Arithmetic extends Function {
protected char op;
public Arithmetic(char op, Function f1, Function f2) {
super(new Function[] {f1, f2});
this.op = op;
}
public double f(double t) {
switch(op) {
case '+':
return sources[0].f(t) + sources[1].f(t);
case '-':
return sources[0].f(t) - sources[1].f(t);
case '*':
return sources[0].f(t) * sources[1].f(t);
case '/':
return sources[0].f(t) / sources[1].f(t);
default:
return 0;
}
}
public String toString() {
StringBuffer buf = new StringBuffer("");
if (sources.length > 0) {
buf.append('(');
buf.append(sources[0]);
buf.append(Character.toString(op));
buf.append(sources[1]);
buf.append(')');
}
return buf.toString();
}
}
Sin.java , Cos.java 请读者自行完成。
测试:
package zzz.study.patterns.decorator;
import zzz.study.patterns.decorator.func.Arithmetic;
import zzz.study.patterns.decorator.func.Cos;
import zzz.study.patterns.decorator.func.Function;
import zzz.study.patterns.decorator.func.Sin;
import zzz.study.patterns.decorator.func.Square;
import zzz.study.patterns.decorator.func.T;
public class ShowFunction {
public static void main(String[] args) {
Function complexFunc = new Arithmetic('+', new Square(new Sin(new T())), new Square(new Cos(new T())));
System.out.println(complexFunc + " = " + complexFunc.f(100.0));
}
}
在 《Java函数接口实现函数组合及装饰器模式》 一文中,使用 Function 接口有更简洁的装饰器模式实现。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2016-07-09 Python实现在给定整数序列中找到和为100的所有数字组合