Oop 模板法的任务/算法

Oop 模板法的任务/算法,oop,Oop,我想使用模板方法模式来绘制图形GUI。关于抽象类形状中模板方法的任务有什么建议吗?我的意思是,这种方法能产生什么? 谢谢。绘图方法。因为每个形状在渲染时都有不同的规格 您可以参考下面的示例,我在代码中做了注释。希望对你有帮助 CustomShape.java-您的抽象类 public abstract class CustomShape extends View { int shapeType = 0; int clr = Color.BLACK; int x=0;

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

绘图方法。因为每个形状在渲染时都有不同的规格

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

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;
    }

}
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;
    }


}
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);        

    }  
}
    <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>
CustomShape1.java-您的concreate类

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;
    }

}
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;
    }


}
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);        

    }  
}
    <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>
MainActivity.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;
    }

}
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;
    }


}
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);        

    }  
}
    <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>
活动\u main.xml

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;
    }

}
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;
    }


}
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);        

    }  
}
    <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>



谢谢您的回复。我认为只有一种方法可以做到这一点。我看不出绘图之前的步骤是什么。我只是想澄清一下,我的意思是,模板类(Shape)中有一个名为Draw的空/抽象方法,这个方法可以在实现中被重写。我认为如果我只有一个draw方法,它对于Shape的每个子类都是不同的,那么实现一个接口比实现一个抽象类更容易。我试图找到一个包含一些步骤的算法——对所有子类实现相同的方法,以及一些将被覆盖的方法。这是另一个选项。你可以选择抽象(模板)或界面,甚至两者都可以。从绘制形状开始。如果您有重复的代码,请稍后重构为某种模式。在遇到问题之前不要尝试实现模式。谢谢您的评论。我不会在这个任务中使用模板方法模式,但这是规范。然后我建议设置形状的起点和终点,并调用Draw方法,每个形状都会有所不同。谢谢你的建议。