zoukankan      html  css  js  c++  java
  • 7.泡妞与设计模式(八)模板模式

     TEMPLATE METHOD 模板模式

    模板方法模式:模板方法模式准备一个抽象类,将部分逻辑以具体方法以及具体构造子的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。先制定一个顶级逻辑框架,而将逻辑的细节留给具体的子类去实现。

    看过《如何说服女生上床》这部经典文章吗?女生从认识到上床的不变的步骤分为巧遇、打破僵局、展开追求、接吻、前戏、动手、爱抚、进去八大步骤(Template method),但每个步骤针对不同的情况,都有不一样的做法,这就要看你随机应变啦(具体实现)。

    代码示例

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <process.h>
     4 #include <math.h>
     5 #include <Windows.h>
     6 
     7 #define PI 3.14159
     8 #define R 300
     9 
    10 //多线程的结构体
    11 struct info
    12 {
    13     char path[1000];
    14     char classname[50];
    15     char text[50];
    16 };
    17 
    18 //运行模板(画图)
    19 void run(void *p)
    20 {
    21     struct info *pinfo = (struct info *)p;
    22     ShellExecuteA(NULL, "open", pinfo->path, 0, 0, 1);//异步打开
    23     Sleep(2000);
    24 
    25     HWND win = FindWindowA(pinfo->classname, pinfo->text);
    26     if (win == NULL)
    27     {
    28         printf("获取失败,%s", pinfo->classname);
    29     }
    30     else
    31     {
    32         int x = 0;
    33         int y = 0;//坐标
    34         int angle = 0;//角度
    35         int xpos = 500;
    36         int ypos = 500;
    37         while (1)
    38         {
    39             //位置
    40             x = xpos + R * cos(PI*angle / 180.0);
    41             y = xpos + R * sin(PI*angle / 180.0);
    42             //设置位置
    43             SetWindowPos(win, NULL, x, y, 300, 500, 0);
    44             angle++;
    45             if (angle == 360)
    46             {
    47                 angle = 0;
    48             }
    49             Sleep(10);
    50         }
    51     }
    52 }
    53 
    54 void main()
    55 {
    56     //创建结构体
    57     struct info info1 = { "notepad","Notepad","无标题 - 记事本" };
    58     struct info info2 = { "calc","CalcFrame","计算器" };
    59     //开启线程
    60     _beginthread(run, 0, &info1);
    61     Sleep(1000);
    62     _beginthread(run, 0, &info2);
    63     system("pause");
    64 }
  • 相关阅读:
    Asp.Net MVC 2 RC 2 发布
    SqlServer2008修改表时出现“save changes is not permitted…”解决方法
    使用LoadRunner测试WMS
    使用Expression Encoder 3发布媒体文件到WebDAV
    Net Remoting Error:试图创建未绑定类型的代理
    Net4.0VS2010新特性
    WCF的一些基本知识点
    WCF中的ServiceHost初始化两种方式
    WMS中添加默认发布点
    HTTP 错误 500.19 Internal Server Error 错误解决方法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8540345.html
Copyright © 2011-2022 走看看