2012-12-24 33 views
0

我想使用模板方法模式繪製形狀GUI。任何有關什麼可以成爲抽象類Shape中Template Method的任務的建議?我的意思是,這種方法能產生什麼? 謝謝。模板方法的任務/算法

+0

開始繪製形狀。稍後重構某種模式,如果您將有重複的代碼。在遇到問題之前不要嘗試實施模式。 –

+0

謝謝你的評論。我不會爲此任務使用Template Method模式,但這是規範。 – user1926116

+0

然後我建議設置形狀的起點和終點,並調用Draw方法,這對每個形狀都會有所不同。 –

回答

0

繪製方法。由於每個形狀在渲染中都有不同的規格。

+0

謝謝你的回覆。我認爲這隻能用一種方法完成。我看不到繪圖前的步驟。 – user1926116

+0

只是澄清,我的意思是,模板類(Shape)中有一個名爲Draw的空/抽象方法,並且此方法可以在實現中被覆蓋。 – Codethusiast

+0

我認爲如果我只有一個繪製方法對Shape的每個子類都不同,那麼實現一個接口而不是抽象類會更容易。我試圖找到一個算法的一些步驟 - 方法相同的所有子類和一些將被覆蓋。 – user1926116

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 - 你concreate類

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>