zoukankan      html  css  js  c++  java
  • 3 装饰模式(2)

    代码
    1 #include <iostream>
    2 #include <string>
    3
    4  using std::string;
    5 using std::cout;
    6 using std::endl;
    7
    8 class Person
    9 {
    10 public:
    11 Person()
    12 {
    13 }
    14 Person(string name)
    15 {
    16 this->name = name;
    17 }
    18 virtual void Show()
    19 {
    20 cout<<"装扮的"<<name<<endl;
    21 }
    22 private:
    23 string name;
    24 };
    25
    26 //服饰类
    27 class Finery : public Person
    28 {
    29 public:
    30 void Decorate(Person* component)
    31 {
    32 this->component = component;
    33 }
    34 void Show()
    35 {
    36 if(component != NULL)
    37 {
    38 component->Show();
    39 }
    40 }
    41 private:
    42 Person* component;
    43 };
    44
    45 //具体服饰类
    46 class TShirts : public Finery
    47 {
    48 public:
    49 void Show()
    50 {
    51 cout<<"大T恤 ";
    52 Finery::Show();
    53 }
    54 };
    55
    56 class BigTrouser : public Finery
    57 {
    58 public:
    59 void Show()
    60 {
    61 cout<<"垮裤 ";
    62 Finery::Show();
    63 }
    64 };
    65
    66
    67 class Sneakers:public Finery
    68 {
    69 public:
    70 virtual void Show( )
    71 {
    72 cout<< "破球鞋 ";
    73 Finery::Show();
    74 }
    75 };
    76
    77 class Suit:public Finery
    78 {
    79 public:
    80 virtual void Show( )
    81 {
    82 cout<< "西装 ";
    83 Finery::Show();
    84 }
    85 };
    86
    87 class Tie:public Finery
    88 {
    89 public:
    90 virtual void Show( )
    91 {
    92 cout<< "领带 ";
    93 Finery::Show();
    94 }
    95 };
    96
    97 class LeatherShoes:public Finery
    98 {
    99 public:
    100 virtual void Show( )
    101 {
    102 cout<< "皮靴 " ;
    103 Finery::Show();
    104 }
    105 };
    106
    107 int main()
    108 {
    109 Person* xc = new Person("小菜");
    110
    111 cout<<"第一种装扮:"<<endl;
    112 Sneakers *pqx = new Sneakers();
    113 BigTrouser *kk = new BigTrouser();
    114 TShirts *dtx = new TShirts();
    115
    116 //装饰过程
    117 pqx->Decorate( xc );
    118 kk->Decorate( pqx );
    119 dtx->Decorate( kk );
    120 dtx->Show();
    121
    122 cout<< "第二种装扮" <<endl;
    123 LeatherShoes *px = new LeatherShoes();
    124 Tie *ld = new Tie();
    125 Suit *xz = new Suit();
    126
    127 px->Decorate(xc);
    128 ld->Decorate(px);
    129 xz->Decorate(ld);
    130 xz->Show();
    131
    132
    133
    134 return 0;
    135 }
  • 相关阅读:
    SQL SERVER批量/单个修改表的所有者
    设置电脑右下角显示自己的大名
    javascript 事件查询 综合
    电脑命令大全值得珍藏
    仿flash超炫焦点图播放器 · javascript for jquery
    VirtualBox使用本地硬盘分区的权限问题
    同步时间并更新到计算机
    linux下电子词典里的扩展存储卡不能访问到的问题
    virtualbox 3.0.4终于可以共享剪贴板了
    批量更改mp3文件ID3编码的脚本
  • 原文地址:https://www.cnblogs.com/sifenkesi/p/1719635.html
Copyright © 2011-2022 走看看