我想使用模板方法模式繪製形狀GUI。任何有關什麼可以成爲抽象類Shape中Template Method的任務的建議?我的意思是,這種方法能產生什麼? 謝謝。模板方法的任務/算法
回答
繪製方法。由於每個形狀在渲染中都有不同的規格。
謝謝你的回覆。我認爲這隻能用一種方法完成。我看不到繪圖前的步驟。 – user1926116
只是澄清,我的意思是,模板類(Shape)中有一個名爲Draw的空/抽象方法,並且此方法可以在實現中被覆蓋。 – Codethusiast
我認爲如果我只有一個繪製方法對Shape的每個子類都不同,那麼實現一個接口而不是抽象類會更容易。我試圖找到一個算法的一些步驟 - 方法相同的所有子類和一些將被覆蓋。 – user1926116
你可以參考下面的例子,我在代碼中做了評論。希望它可以幫助你...
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>
- 1. 非模板類的模板方法
- 2. jsrender模板方法
- 3. 模板類方法
- 4. STL算法函數模板
- 5. 模板匹配算法
- 6. 模板和佔位算法
- 7. 模板方法模式
- 8. 模板方法模式
- 9. C++不能調用方法(使用模板)從模板方法
- 10. UWP模板的CRUD方法
- 11. 控制方法的模板
- 12. dojo的模板方法
- 13. ReverseGeocodingTask任務的方法
- 14. 實現模板化模板方法
- 15. 模板雙模板類方法
- 16. C++模板方法的語法問題
- 17. C++ 11:模板類方法內聯聲明的任何更簡單的方法?
- 18. 進化算法的R模板?
- 19. 調用模板方法
- 20. C++模板(插入方法)
- 21. 休息模板(POST方法)
- 22. 重載方法與模板
- 23. dllexport'ing靜態模板方法
- 24. 添加方法模板特
- 25. Codeigniter模板庫,add_js()方法
- 26. 爲抽象模板方法
- 27. tolua和模板方法
- 28. 枚舉和模板方法
- 29. C#中的任務管理算法
- 30. 算法再平衡的循環任務
開始繪製形狀。稍後重構某種模式,如果您將有重複的代碼。在遇到問題之前不要嘗試實施模式。 –
謝謝你的評論。我不會爲此任務使用Template Method模式,但這是規範。 – user1926116
然後我建議設置形狀的起點和終點,並調用Draw方法,這對每個形狀都會有所不同。 –