0

我想使用模板方法模式来绘制形状 GUI。关于抽象类 Shape 中模板方法的任务有什么建议?我的意思是,这种方法能产生什么?谢谢你。

4

2 回答 2

0

你可以参考下面的例子,我在代码中做了注释。希望对你有帮助...

CustomShape.java - 你的抽象类

public abstract class CustomShape extends View {
    int shapeType = 0;
    int clr = Color.BLACK;
    int x=0;
    int y=0;

    public CustomShape(Context context) {
        super(context);

    }

    // OnDraw can act as Template Method
    // This method holds the algorithm of shape creation    
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    @Override
    final public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
            // you can put here more method to make your shape different                 
            // for example setColor(); setStroke() ..... 

        createRectangle(canvas);                
    }

   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


    // Primitive operation sub classes must override
    abstract void setShapeType(int type);

    // Primitive operation sub classes must override
    abstract void setShapeColor(int color);

    // Primitive operation sub classes must override
    abstract void setXY(int x1,int y1);


    // Concreate Operation we dont want subclass to override  
    final void createRectangle(Canvas canvas) {

        if (shapeType == 0) {
            if (isColored()) {
                canvas.drawRect(x, y, x+100, y+100, getPaint(clr, 1));
            } else {
                canvas.drawRect(x, y, x+100, y+100, getPaint(Color.BLACK, 1));

            }
        } else {
            if (isColored()) {
                canvas.drawCircle(x, y, 80, getPaint(clr, 1));
            } else {
                canvas.drawCircle(x, y, 80, getPaint(clr, 1));
            }
        }
    }




    // Concreate Operation we dont want subclass to override  

    final Paint getPaint(int color, int Stroke) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(Stroke);
        return paint;
    }


    // HOOK - sub class can override but doesnt have to, 

    boolean isColored() {
        return true;
    }

}

CustomShape1.java - 你的创建类

public class CustomShape1 extends CustomShape {

    public CustomShape1(Context context) {
        super(context);

    }   

     boolean isColored(){
         return true;
     }


    @Override
    void setShapeType(int type) {
        shapeType= type;
    }

    @Override
    void setShapeColor(int color) {
        clr = color;        
    }

    @Override
    void setXY(int x1, int y1) {
        x = x1;
        y =y1;
    }


}

MainActivity.java

public class MainActivity extends Activity {

    LinearLayout ln1,ln2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ln1 = (LinearLayout)findViewById(R.id.ln1);
        ln2= (LinearLayout)findViewById(R.id.ln2);

        CustomShape1 cs1 = new CustomShape1(this);
        cs1.setShapeType(1);
        cs1.setShapeColor(Color.YELLOW);
        cs1.setXY(100, 100);

        CustomShape1 cs2 = new CustomShape1(this);
        cs2.setShapeType(0);
        cs2.setShapeColor(Color.RED);
        cs2.setXY(300, 300);


        ln2.addView(cs2);
        ln1.addView(cs1);        

    }  
}

activity_main.xml

    <LinearLayout
        android:id="@+id/ln1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ln2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    </LinearLayout>

</RelativeLayout>
于 2012-12-24T11:31:58.017 回答
0

画法。由于每个形状在渲染中都有不同的规格。

于 2012-12-24T09:09:43.327 回答