实现 : Defferent Heros attack Defferently. - 不同的英雄使用不用的招数

Strategy设计的思路:

基类A。更加小的基类B,新的继承类C:

1 从基类A中抽出一个更加小的基类B

2 利用这个更加小的基类B实现不同的效果

3 把这个更加小的基类B包括进基类A中去

4 新的继承类C仅仅须要和基类A打交道,设计不同行为,不须要理会更加小的基类B

#pragma once
#ifndef _STRATEGY_HEROS_H
#define _STRATEGY_HEROS_H #include <stdio.h> //Interface
class HeroAttack
{
public:
virtual void attack() = 0;
}; //Base method class
class Laser : public HeroAttack
{
public:
void attack()
{
puts("Super Eye-Laser");
}
}; class Smash : public HeroAttack
{
public:
void attack()
{
puts("I smash!");
}
}; class WebWebWeb : public HeroAttack
{
public:
void attack()
{
puts("Web, Web, Web!");
}
}; //Base Class
class Hero_From_Marvel
{
HeroAttack *heroAtt;
protected:
void setAttackWay(HeroAttack *hat)
{
if (heroAtt) delete heroAtt;
heroAtt = hat;
}
public:
Hero_From_Marvel() : heroAtt(nullptr)
{
} virtual ~Hero_From_Marvel()
{
if (heroAtt) delete heroAtt;
heroAtt = nullptr;
} void attack()
{
heroAtt->attack();
}
}; //derived class
class SuperMan : public Hero_From_Marvel
{
public:
SuperMan()
{
setAttackWay(new Laser);//这里不能使用malloc,否则内存冲突
}
}; class Hulk : public Hero_From_Marvel
{
public:
Hulk()
{
setAttackWay(new Smash);
}
}; class SpiderMan : public Hero_From_Marvel
{
public:
SpiderMan()
{
setAttackWay(new WebWebWeb);
}
}; int StrategyHerosRun()
{
const int HEROS = 3;
Hero_From_Marvel *hfm[HEROS] = {new SuperMan, new Hulk, new SpiderMan};
for (int i = 0; i < HEROS; i++)
{
hfm[i]->attack();
delete hfm[i];
}
return 0;
} #endif

Design Pattern 设计模式1 - Strategy 1的更多相关文章

  1. Composite Design Pattern 设计模式组合

    设计模式组合,它能够更类组合在一类,形成一个树状结构. #include <set> #include <iostream> #include <string> u ...

  2. Design Principle vs Design Pattern 设计原则 vs 设计模式

    Design Principle vs Design Pattern设计原则 vs 设计模式 来源:https://www.tutorialsteacher.com/articles/differen ...

  3. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  4. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

  5. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  6. 简单工厂设计模式(Simple Factory Design Pattern)

    [引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...

  7. 巧用代理设计模式(Proxy Design Pattern)改善前端图片加载体验

    这篇文章介绍一种使用代理设计模式(Proxy Design Pattern)的方法来改善您的前端应用里图片加载的体验. 假设我们的应用里需要显示一张尺寸很大的图片,位于远端服务器.我们用一些前端框架的 ...

  8. 设计模式-模板方法设计模式--Template Method design pattern

    /** * Abstract implementation of the {@link org.springframework.context.ApplicationContext} * interf ...

  9. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

随机推荐

  1. 【Luogu】P2015二叉苹果树(DP,DFS)

    题目链接 设f[i][j][k]表示给以i为根节点的子树分配j条可保留的树枝名额的时候,状态为k时能保留的最多苹果. k有三种情况. k=1:我只考虑子树的左叉,不考虑子树的右叉,此时子树能保留的最多 ...

  2. Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. 刷题总结——shortest(ssoi)

    题目: 题目背景 SOURCE:NOIP2015-SHY-3 题目描述 给定一张 n 个点的有向带权完全图,和一个数组 a[] ,请按顺序删除数组中的点,请求出在删除点 a[i] 以前,所有未删除点对 ...

  4. es6 箭头函数 map、find

    var  value = arr.map(function (x) {return x * x}); const arr = [1,2,3,4]; const value = arr.map(x =& ...

  5. Spoj-VISIBLEBOX Decreasing Number of Visible Box

    Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman ...

  6. zoj 3627#模拟#枚举

    Treasure Hunt II Time Limit: 2 Seconds                                     Memory Limit: 65536 KB    ...

  7. msp430入门学习41

    msp430的其他九 msp430入门学习

  8. Scrapy学习-4-Items类&Pipelines类

    items类使用 作用 能使得我们非常方便的操作字段名 在items.py中定制我们的类 class ArticleItem(scrapy.Item): title = scrapy.Field() ...

  9. POJ2752 NEXT[J]特性应用利用。

    题意:求一个字符串所有的前缀和后缀相同的情况,每个情况输出长度,如 ababcababababcabab :2 4 9 18 思路:next数组应用,利用j=nxet[i],i之前与开头相同的字符串长 ...

  10. bzoj 5091: [Lydsy0711月赛]摘苹果

    5091: [Lydsy0711月赛]摘苹果 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 148  Solved: 114[Submit][Statu ...