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

目录

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

[2、范例: Android + TM模式](#2、范例: Android + TM模式)

[3、基于TM模式的扩充:以游戏的绘图循环(Game Loop)为例](#3、基于TM模式的扩充:以游戏的绘图循环(Game Loop)为例)

4、Android中处处可见TM模型的应用


1、Template Method模式

在前面各节里,我们介绍过,控制反转(IoC:Inversion of Control)是<基类/子类>结构里的重要机制。 Template Method模式是实现IoC的一种基本模式。

2、范例: Android + TM模式

Android的绘图是使用画布(Canvas)来把图显示于View的窗口里,并且从View类别而衍生子类别,提供更多功能来将图形或图片绘制于画布上。

在View类别里有个onDraw()函数, View类别体系里的每一个类别都必须覆写(Override) 这个onDraw()函数,来执行实际绘图的动作。

java 复制代码
// myView.java
//.......
public class myView extends View {
    private Paint paint= new Paint();
    private int line_x = 100, line_y = 100;
    private float count = 0;
    myView(Context ctx) { super(ctx); }
    
    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if( count > 12)
            count = 0;
        int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
        int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
        count++;canvas.drawColor(Color.WHITE);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
        paint.setStrokeWidth(2);
        paint.setColor(Color.RED);
        canvas.drawRect(line_x-5, line_y - 5,
        line_x+5, line_y + 5, paint);
        paint.setColor(Color.YELLOW);
        canvas.drawRect(line_x-3, line_y - 3, line_x+3,
        line_y + 3, paint);
    }
}

3、基于TM模式的扩充:以游戏的绘图循环(Game Loop)为例

游戏的基本动作就是不断的进行:绘图和刷新(Refresh)画面。其中, onDraw()函数实践画图,将图形绘制于View的画布(Canvas)上,并显示出来;而invalidate()函数则启动画面的刷新,重新呼叫一次onDraw()函数。

java 复制代码
// myView.java
// .......
public class myView extends View {
    private Paint paint= new Paint();
    private int line_x = 100, line_y = 100;
    myView(Context ctx) { super(ctx); }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //-----------------------------------------------------
        if( count > 12) count = 0;
        int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
        int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
        count++;
        //---------------------------------------------canvas.drawColor(Color.WHITE);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
        paint.setStrokeWidth(2);
        paint.setColor(Color.RED);
        canvas.drawRect(line_x-5, line_y - 5, line_x+5, line_y + 5, paint);
        paint.setColor(Color.YELLOW);
        canvas.drawRect(line_x-3, line_y - 3, line_x+3, line_y + 3, paint);

        try {
            Thread.sleep(1000);
        }catch (InterruptedException ie) {}
           
        invalidate();
    }
}

// myActivity.java
// ......
public class myActivity extends Activity implements OnClickListener {
    private myView mv = null;
    private Button ibtn;
    
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        public void show_layout_01(){
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        mv = new myView(this);
        LinearLayout.LayoutParams param =
            new LinearLayout.LayoutParams(200, 200);
        param.topMargin = 10; param.leftMargin = 10;
        layout.addView(mv, param);ibtn = new Button(this);
        ibtn.setOnClickListener(this);
        ibtn.setText("Exit");
        ibtn.setBackgroundResource(R.drawable.gray);
        LinearLayout.LayoutParams param1 =
            new LinearLayout.LayoutParams(200, 65);
        param1.topMargin = 10; param1.leftMargin = 10;
        layout.addView(ibtn, param1);
        //-----------------------------------------------
        setContentView(layout);
    }
    public void onClick(View v) {
        finish();
    }
}

4、Android中处处可见TM模型的应用

相关推荐
电子科技圈11 小时前
SiFive车规级RISC-V IP获IAR最新版嵌入式开发工具全面支持,加速汽车电子创新
嵌入式硬件·tcp/ip·设计模式·汽车·代码规范·risc-v·代码复审
七月丶13 小时前
Cloudflare 🌏 中国大陆网络访问优化 - 0元成本
人工智能·react.js·设计模式
筏.k13 小时前
C++ 设计模式系列:单例模式
c++·单例模式·设计模式
__万波__13 小时前
二十三种设计模式(十二)--代理模式
java·设计模式·代理模式
郝学胜-神的一滴14 小时前
Linux线程编程:从原理到实践
linux·服务器·开发语言·c++·程序人生·设计模式·软件工程
我爱学习_zwj14 小时前
前端设计模式:轻量级实战指南
设计模式·前端框架·状态模式
还是大剑师兰特14 小时前
前端设计模式:详解、应用场景与核心对比
前端·设计模式·大剑师
平凡之路无尽路2 天前
智能体设计模式:构建智能系统的实践指南
人工智能·设计模式·自然语言处理·nlp·aigc·vllm
冷崖2 天前
工厂模式-创建型
c++·设计模式
何中应2 天前
【面试题-5】设计模式
java·开发语言·后端·设计模式·面试题