シナリオのために最も適したデザインパターンを特定する方法

エンターテインメント:

私は、GUIベースの描画プログラムを実装したいです。円、三角形や四角形を描画することができるはずです。そして、将来的には、システム内の図形の数を拡張することができるはずです。

このプログラムは、このような「動き」への機能など、これらの形状に一定の能力」を与える「拡大」、「回転」、そしてもちろん(GUIで描かれた形状を得るために)「描画」します。これらの方法の各々は、それらが描画されるためには、パラメータとして渡されたように「グラフィックスコンテキスト」を必要とします。

私は方法を、私は戦略パターンが有用であろうと考え整形した形状と異なるが存在しますので、このプログラムのためのデザインパターンを使用して欲しいです。

私は、このパターンは大丈夫です使用している方法を知ってほしいです。

interface Shape{}

interface Behavioral{
public void move(Graphics g){}
public void expand(Graphics g){}
public void draw(Graphics g){}
}

(There is an aggregation between shape and behavioral)

interface Rotatable{
public void rotate(Graphics g)
} 

interface RotatableToLeft extends Rotatable(){
public void rotate(Graphics g)
}

interface RotatableToRight extends Rotatable(){
public void rotate(Graphics g)
}

class circle implements Shape{
public void move(Shape g){--}
public void expand(Shape g){--}
public void draw(Shape g){--}

}

class Square implements Shape implements Rotatable{
public void move(Shape g){--}
public void expand(Shape g){--}
public void draw(Shape g){--}
public void rotatefree(RotatableToLeft g){--}
}

class Triangle implements Shape implements Rotatable{
public void move(Graphics g){--}
public void expand(Graphics g){--}
public void draw(Graphics g){--}
public void rotateLeft(RotatableToLeft g){--}
public void rotateRight(RotatableToRight g){--}

}
マイクNakis:

Shapeおそらく、共通の抽象基底クラスではなくインタフェースでなければならず、非常に少なくとも、draw()のメンバーである必要がありますShapeすべての形状は他のすべてのメソッドを実装することが考えられますのでBehavioral、あなたは取り除くことができますBehavioralし、そのメソッドのすべてを動かしますShape

RotatableToLeftそしてRotatableToRightどんな意味がありません。唯一の方法を含む、1つのインタフェースだけを使用し、回転させる方向を渡します。

あなたは、おそらくいくつかの「tryAs」を必要とメソッド「として」「」であります。例えば:

class Shape
{
    public Rotatable tryAsRotatable() { return null; }

    public final boolean isRotatable()
    {
        return tryAsRotatable() != null;
    }

    public final Rotatable asRotatable() 
    {
        Rotatable result = tryAsRotatable();
        assert result != null;
        return result;
    }
}

class Circle extends Shape implements Rotatable
{
    @Override
    public Circle tryAsRotatable() { return this; }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=297031&siteId=1