简体   繁体   English

如何使用 Java Swing 实现责任链

[英]How to implement chain of responsibility with Java Swing

I have the task of creating a simple drawing app, where one can draw basic shapes (ovals,lines,rectangles) with border and fill colors of choice, using Java Swing and MVC.我的任务是创建一个简单的绘图应用程序,在其中可以使用 Java Swing 和 MVC 绘制带有选择的边框和填充颜色的基本形状(椭圆形、线条、矩形)。

The shapes part of the model is implemented using the composite pattern.模型的形状部分是使用复合模式实现的。 The functionality to be implementing is drawing (this is already handled by the shapes classes themselves), resizing, moving and deleting shapes and I am supposed to use chain of responsibility (CoR) pattern to accomplish this.要实现的功能是绘制(这已经由形状类本身处理)、调整大小、移动和删除形状,我应该使用责任链 (CoR) 模式来实现这一点。

CoR makes sense to me in theory, but I have difficulty grasping how I can apply it for implementing the functionality in practice. CoR 在理论上对我来说很有意义,但我很难理解如何在实践中应用它来实现功能。 I understand that when I click on the drawing panel the program should recognize which shape is selected and then I can implement the methods for resizing,moving,deleting.我知道当我点击绘图面板时,程序应该识别出选择了哪个形状,然后我可以实现调整大小、移动、删除的方法。

So what I need advice on is:所以我需要的建议是:

1) how to practically implement CoR pattern here? 1) 如何在这里实际实现 CoR 模式?

2) what is a good way to implement the functionality for resizing, moving, deleting? 2)什么是实现调整大小,移动,删除功能的好方法? In own concrete handler classes, as methods in the shapes classes, other?在自己的具体处理程序类中,作为形状类中的方法,其他?

Many thanks for help.非常感谢您的帮助。

Here is a proposed basic implementation of CoR.这是一个提议的 CoR 基本实现。
For ease of use the following code is a one-file mre : the entire code can be copy-pasted into ShapeManipulator.java and run:为了便于使用,以下代码是一个文件mre :整个代码可以复制粘贴到ShapeManipulator.java并运行:

public class ShapeManipulator {

    public static void main(String[] args) {
        Shape s1 = new Shape();
        Shape s2 = new Shape();
        Shape s3 = new Shape();

        ShapeManipulationBase move = new MoveHandler();
        ShapeManipulationBase resize = new ResizeHandler();
        ShapeManipulationBase delete = new DeleteHandler();

        move.setXparam(50).setYparam(25).handle(s1);
        resize.setXparam(100).setYparam(250).handle(s1);
        resize.setXparam(200).setYparam(20).handle(s2);
        delete.handle(s3);
    }
}

//CoR basic interface 
interface ShapeManipulationHandler {
     void handle(Shape shape);
}

//base class allows swtting of optional x, y parameters 
abstract class ShapeManipulationBase implements ShapeManipulationHandler {

    protected int Xparam, Yparam; 

    //setters return this to allow chaining of setters 
    ShapeManipulationBase setXparam(int xparam) {
        Xparam = xparam;
        return this;
    }

    ShapeManipulationBase setYparam(int yparam) {
        Yparam = yparam;
        return this;
    }

    @Override
    public abstract void handle(Shape shape) ;
}

class MoveHandler extends ShapeManipulationBase {

    @Override
    public void handle(Shape shape) {
        System.out.println("Moving "+ shape + " by X="+ Xparam + " and Y="+ Yparam);
    }
}

class ResizeHandler extends ShapeManipulationBase {

    @Override
    public void handle(Shape shape) {
        System.out.println("Resizing "+ shape + " by X="+ Xparam + " and Y="+ Yparam);
    }
}

class DeleteHandler extends ShapeManipulationBase {

    @Override
    public void handle(Shape shape) {
        System.out.println("Deleting "+ shape);
    }
}

class Shape{
    private static int shapeCouner = 0;
    private final int shapeNumber;

    Shape() {
        shapeNumber = ++shapeCouner;
    }

    @Override
    public String toString() {
        return "Shape # "+shapeNumber;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM