zoukankan      html  css  js  c++  java
  • 0.泡妞与设计模式(一)工厂模式

    工厂模式:

    客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。(简洁来说就是对同一种要求使用不同的处理手段)

    例如,追MM的时候少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了。麦当劳和肯德基就是生产鸡翅的Factory。

    代码示例

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <locale.h>
     4 #include <Windows.h>
     5 //语音说出调用COM组件的头文件
     6 #include <atlstr.h>
     7 #include <sphelper.h>
     8 #include <sapi.h>
     9 #include <compstui.h>
    10 #include <string.h>
    11 //语音函数
    12 #pragma comment(lib,"sapi.lib")
    13 #pragma comment(lib,"comsupp.lib")
    14 #pragma comment(lib,"ole32.lib")
    15 
    16 //函数指针
    17 void(*p)(wchar_t *str) = NULL;
    18 
    19 //语音说出来
    20 void speak(wchar_t *str)
    21 {
    22     ISpVoice *pVoice = NULL;
    23     ::CoInitialize(NULL);
    24     //获取ISpVoice接口
    25     long hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    26     if (FAILED(hr))
    27     {
    28         MessageBox(NULL, "CoCreateInstance失败!", "错误", 0x10010);
    29         return;
    30     }
    31     hr = pVoice->Speak(str, 0, NULL);
    32     pVoice->Release();
    33     pVoice = NULL;
    34     //千万不要忘记
    35     ::CoUninitialize();
    36 }
    37 
    38 //输出到屏幕
    39 void print(wchar_t *str)
    40 {
    41     wprintf(L"%ls", str);
    42 }
    43 
    44 //对话框显示
    45 void show(wchar_t *str)
    46 {
    47     MessageBoxW(NULL, L"i love you", str, NULL);
    48 }
    49 
    50 //接口固定,对一串文字执行不同的操作
    51 void go(void(*pfun)(wchar_t *str),wchar_t *pstr)
    52 {
    53     p = pfun;
    54     p(pstr);
    55 }
    56 
    57 void main()
    58 {
    59     setlocale(LC_ALL, "zh-CN");
    60 
    61     while (1)
    62     {
    63         go(show, L"老板来四个鸡腿");
    64         system("pause");
    65     }
    66 
    67     system("pause");
    68 }
  • 相关阅读:
    Memcached存储溢出
    Memcached windows安装
    Memcached简介
    HTTP请求中怎样选择Get和Post方式
    【cogs 597】【dp】交错匹配
    &lt;LeetCode OJ&gt; 58. Length of Last Word
    codevs1051
    最重要的 Java EE 最佳实践
    android-xBuild apk差分与合成,zip差分与合成,lua打包,apk打包,png/jpg图片压缩
    [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8531292.html
Copyright © 2011-2022 走看看