用于向现有对象添加新方法和代码的装饰器设计模式
decorator design pattern for adding new methods and codes to the existing object
我是设计模式的新手。我正在尝试使用装饰器设计模式向我现有的应用程序添加新代码和功能。
假设我有一个 class 的 App,它有两个方法 "Add" 和 "Multiply"。在某些时候(运行 时间),应用程序也需要计算平均值。
所以,我正在尝试使用装饰器设计模式来实现这一点。
到目前为止我有:
public class App implements Code{
public int a=2;
public int b=3;
@Override
public int Add(int a, int b) {
int add;
add = a+b;
return add;
}
@Override
public int Multiply(int a, int b) {
int mul;
mul= a*b;
return mul;
}
}
为了做到这一点,我定义了一个接口 "Code",如下所示:
public interface Code {
public int Add (int a, int b);
public int Multiply (int a, int b);
}
然后是装饰器抽象classCodeExtention
public abstract class CodeExtention implements Code{
protected Code extendedCode;
public CodeExtention(Code extendedCode) {
this.extendedCode = extendedCode;
}
@Override
public int Multiply(int a, int b){
return extendedCode.Multiply(a, b);
}
@Override
public int Add(int a, int b){
return extendedCode.Add(a, b);
}
}
现在我定义一个音乐会 class "AVG" 从我的摘要 class 扩展而来,如下所示:
public class AVG extends CodeExtention{
public AVG(Code extendedCode) {
super(extendedCode);
}
public int AVGcalculator (int a, int b){
return (a+b)/2;
}
@Override
public int Add(int a, int b) {
return super.Add(a, b);
}
@Override
public int Multiply(int a, int b) {
return super.Multiply(a, b);
}
}
现在我希望我的应用程序可以计算出平均值,我的主要内容是:
public static void main(String[] args) {
Code app = new App();
app = new AVG(app);
}
}
在这里我可以有:
System.out.println(app.Add(3, 4));
System.out.println(app.Multiply(3, 4));
我还是不能拥有:
System.out.println(app.AVGcalculator(3, 4));
我不知道哪里出了问题,甚至不知道我的场景是否可以使用这种设计模式!
该应用程序是代码类型,因为 AVGcalculator 不是代码接口的一部分,您不能调用它,如果您想调用 AVGcalculator,您可以这样做
System.out.println(((AVG)app).AVGcalculator(3, 4));
装饰器模式是一个非常糟糕的选择。
装饰器履行与装饰对象相同的契约——相同的API。你想要的是改变合同。因此,该模式不适用(阅读 this 以了解何时使用装饰器的良好示例)。
您可以使用 Command 模式:
interface BinaryIntOperation {
int execute(int a, int b);
}
class AddOperation implements BinaryIntOperation {
int execute(int a, int b) {
return a + b;
}
}
class MultiplyOperation implements BinaryIntOperation {
int execute(int a, int b) {
return a * b;
}
}
class AverageOperation implements BinaryIntOperation {
int execute(int a, int b) {
return (a + b)/2;
}
}
然后您可以从那里做很多事情:
BinaryIntOperation op = new AddOperation();
System.out.println(op.execute(3, 4));
op = new MultiplyOperation();
System.out.println(op.execute(4, 5));
你也可以这样写:
public int[] execute(int[] arr, BinaryIntOperation op, int a) {
for (int i = 0; i < arr.length; i++)
arr[i] = op.execute(a, arr[i]);
return arr;
}
命令模式是一种行为模式,看起来更像你想要的(改变行为)。
请注意,在 C# 中,您可以使用 扩展方法完全您想要的。但是 Java 没有这些。
我是设计模式的新手。我正在尝试使用装饰器设计模式向我现有的应用程序添加新代码和功能。
假设我有一个 class 的 App,它有两个方法 "Add" 和 "Multiply"。在某些时候(运行 时间),应用程序也需要计算平均值。 所以,我正在尝试使用装饰器设计模式来实现这一点。
到目前为止我有:
public class App implements Code{
public int a=2;
public int b=3;
@Override
public int Add(int a, int b) {
int add;
add = a+b;
return add;
}
@Override
public int Multiply(int a, int b) {
int mul;
mul= a*b;
return mul;
}
}
为了做到这一点,我定义了一个接口 "Code",如下所示:
public interface Code {
public int Add (int a, int b);
public int Multiply (int a, int b);
}
然后是装饰器抽象classCodeExtention
public abstract class CodeExtention implements Code{
protected Code extendedCode;
public CodeExtention(Code extendedCode) {
this.extendedCode = extendedCode;
}
@Override
public int Multiply(int a, int b){
return extendedCode.Multiply(a, b);
}
@Override
public int Add(int a, int b){
return extendedCode.Add(a, b);
}
}
现在我定义一个音乐会 class "AVG" 从我的摘要 class 扩展而来,如下所示:
public class AVG extends CodeExtention{
public AVG(Code extendedCode) {
super(extendedCode);
}
public int AVGcalculator (int a, int b){
return (a+b)/2;
}
@Override
public int Add(int a, int b) {
return super.Add(a, b);
}
@Override
public int Multiply(int a, int b) {
return super.Multiply(a, b);
}
}
现在我希望我的应用程序可以计算出平均值,我的主要内容是:
public static void main(String[] args) {
Code app = new App();
app = new AVG(app);
}
}
在这里我可以有:
System.out.println(app.Add(3, 4));
System.out.println(app.Multiply(3, 4));
我还是不能拥有:
System.out.println(app.AVGcalculator(3, 4));
我不知道哪里出了问题,甚至不知道我的场景是否可以使用这种设计模式!
该应用程序是代码类型,因为 AVGcalculator 不是代码接口的一部分,您不能调用它,如果您想调用 AVGcalculator,您可以这样做
System.out.println(((AVG)app).AVGcalculator(3, 4));
装饰器模式是一个非常糟糕的选择。
装饰器履行与装饰对象相同的契约——相同的API。你想要的是改变合同。因此,该模式不适用(阅读 this 以了解何时使用装饰器的良好示例)。
您可以使用 Command 模式:
interface BinaryIntOperation {
int execute(int a, int b);
}
class AddOperation implements BinaryIntOperation {
int execute(int a, int b) {
return a + b;
}
}
class MultiplyOperation implements BinaryIntOperation {
int execute(int a, int b) {
return a * b;
}
}
class AverageOperation implements BinaryIntOperation {
int execute(int a, int b) {
return (a + b)/2;
}
}
然后您可以从那里做很多事情:
BinaryIntOperation op = new AddOperation();
System.out.println(op.execute(3, 4));
op = new MultiplyOperation();
System.out.println(op.execute(4, 5));
你也可以这样写:
public int[] execute(int[] arr, BinaryIntOperation op, int a) {
for (int i = 0; i < arr.length; i++)
arr[i] = op.execute(a, arr[i]);
return arr;
}
命令模式是一种行为模式,看起来更像你想要的(改变行为)。
请注意,在 C# 中,您可以使用 扩展方法完全您想要的。但是 Java 没有这些。