梳理你的思路(从OOP到架构设计)_设计模式Factory Method模式

目录

[1、Factory Method模式](#1、Factory Method模式)

[2、范例: Android + FM(工厂)模式](#2、范例: Android + FM(工厂)模式)

3、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模式的应用

相关推荐
J_liaty1 天前
23种设计模式一迭代器模式
设计模式·迭代器模式
驴儿响叮当20101 天前
设计模式之策略模式
设计模式·策略模式
驴儿响叮当20101 天前
设计模式之中介模式
设计模式
驴儿响叮当20101 天前
设计模式之命令模式
设计模式·命令模式
驴儿响叮当20101 天前
设计模式之适配器模式
设计模式·适配器模式
HEU_firejef1 天前
设计模式——代理模式
设计模式·代理模式
Coder_Boy_2 天前
从单体并发工具类到分布式并发:思想演进与最佳实践(二)
java·spring boot·分布式·微服务·设计模式
geovindu2 天前
python: Memento Pattern
开发语言·python·设计模式·备忘录模式
HEU_firejef2 天前
设计模式——单例模式
单例模式·设计模式
电子科技圈2 天前
SmartDV与Mirabilis Design宣布就SmartDV IP系统级模型达成战略合作
大数据·设计模式·软件工程