目录
[1、Factory Method模式](#1、Factory Method模式)
[2、范例: Android + FM(工厂)模式](#2、范例: Android + FM(工厂)模式)
1、Factory Method模式
- 誰來創建<T>的對象呢?
- 例如, 剛才的Template Method模式內含一個EIT造形,那麼, 請問: 誰來創建該<T>的對象呢?
- Factory Method模式就是一種常用的方案。





2、范例: Android + FM(工厂)模式
在Android框架里,处处可见FactoryMethod模式。例如, Activity、 Service等抽象类别里都定义了onCreate()函数,它就是一个典型的FactoryMethod函数。


java
// GraphicView.java
// ........
public class GraphicView extends View{
private Paint paint= new Paint();
GraphicView(Context ctx) { super(ctx); }
@Override protected void onDraw(Canvas canvas) {
int line_x = 10; int line_y = 50;
canvas.drawColor(Color.WHITE);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+120, line_y, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(2);
canvas.drawText("這是繪圖區", line_x, line_y + 50, paint);
int pos = 70; paint.setColor(Color.RED);
canvas.drawRect(pos-5, line_y - 5, pos+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(pos-3, line_y - 3, pos+3, line_y + 3, paint);
}
}
// myActivity.java
// ........
public class ac01 extends Activity {
private GraphicView gv = null;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
gv = new GraphicView(this);
setContentView(gv);
}
}
myActivity类,通过new GraphicView(this); 来实现工厂EIT生成产品EIT的连接
3、Android里处处可见的FM模式的应用
