Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用JavaSwing实现责任链_Java_Swing_Chain Of Responsibility - Fatal编程技术网

如何用JavaSwing实现责任链

如何用JavaSwing实现责任链,java,swing,chain-of-responsibility,Java,Swing,Chain Of Responsibility,我的任务是创建一个简单的绘图应用程序,其中可以使用JavaSwing和MVC绘制基本形状(椭圆、直线、矩形),并选择边框和填充颜色 模型的形状部分使用复合模式实现。要实现的功能是绘图(这已经由shapes类自己处理)、调整大小、移动和删除形状,我应该使用责任链(chainofresponsibility,CoR)模式来实现这一点 CoR在理论上对我来说是有意义的,但我很难理解如何在实践中应用它来实现功能。我知道,当我点击绘图面板时,程序应该能识别所选的形状,然后我可以实现调整大小、移动和删除的方

我的任务是创建一个简单的绘图应用程序,其中可以使用JavaSwing和MVC绘制基本形状(椭圆、直线、矩形),并选择边框和填充颜色

模型的形状部分使用复合模式实现。要实现的功能是绘图(这已经由shapes类自己处理)、调整大小、移动和删除形状,我应该使用责任链(chainofresponsibility,CoR)模式来实现这一点

CoR在理论上对我来说是有意义的,但我很难理解如何在实践中应用它来实现功能。我知道,当我点击绘图面板时,程序应该能识别所选的形状,然后我可以实现调整大小、移动和删除的方法

因此,我需要的建议是:

1) 如何在这里实际实现CoR模式

2) 实现调整大小、移动和删除功能的好方法是什么?在自己的具体处理程序类中,作为shapes类中的方法,其他


非常感谢你的帮助

这里是CoR的一个提议的基本实现。
为了便于使用,以下代码是一个文件:可以将整个代码复制粘贴到
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;
    }
}

您确定不需要(抽象)工厂或任何适合对象创建的创建模式吗?责任链是一种行为模式,使用ATM可能是一个很好的用例。我同意使用工厂是有意义的,但对于任务,我被指示使用模型部件的复合模式。我不确定CoR,但引用了一些相关示例
GraphPanel
是最简单的。谢谢,这看起来很有用,我将尝试相应地实现。在这个答案中没有类似于责任链设计模式的东西。