联系
Knight's Tale » 技术

Strategy Design Pattern (C++)

2010-07-15 17:21

This is a C++ project that describe the usage of Strategy Pattern design The aim is to implement one task which is in the page 25 of ( Chinese version ).

creatd by Knight 2010.07.15 www.liaoqiqi.com/blog

Full Codes:

StrategyPattern.h

#pragma once

#include <iostream>
#include <string>
using namespace std;
namespace StrategyPatternDesign
{
    //////////////////////////////////////////////////////////////////////////
    //
    // algorithm class: weapon
    //
    class WeaponBehavior
    {
    public:
        virtual ~WeaponBehavior(){}
        virtual string UseWeapon()=0;
    };

    class NoBehavior:public WeaponBehavior
    {
    public:
        string UseWeapon(){ return  "None!";}
    };

    class KnifeBehavior:public WeaponBehavior
    {
    public:
        string UseWeapon(){return "using Knife!";}
    };

    class BowAndArrowBehavior:public WeaponBehavior
    {
    public:
        string UseWeapon(){return"using Bow and Arrow!";}
    };

    class AxeBehavior:public WeaponBehavior
    {
    public:
        string UseWeapon(){return "using Axe!";}
    };

    class SwordBehavior:public WeaponBehavior
    {
    public:
        string UseWeapon(){ return  "using Sword!";}
    };

    //////////////////////////////////////////////////////////////////////////
    //
    // The base and Concrete class:
    //
    class Character
    {
    public:
        Character();
        ~Character();
        virtual void Fight()=0;
        void SetWeapon(WeaponBehavior *rhs);
    protected:
        WeaponBehavior *weaponBehavior;
    };

    class King:public Character
    {
    public:
        void Fight(){ cout << "I'm King, " << weaponBehavior->UseWeapon() << std::endl;}
    };

    class Queen:public Character
    {
    public:
        void Fight(){ cout << "I'm Queen, " << weaponBehavior->UseWeapon() << std::endl;}
    };

    class Knight:public Character
    {
    public:
        void Fight(){ cout << "I'm Knight, " << weaponBehavior->UseWeapon() << std::endl;}
    };
}

StrategyPattern.cpp

#include "StrategyPattern.h"

namespace StrategyPatternDesign
{
    Character::~Character()
    {
        if(weaponBehavior)
            delete weaponBehavior;
    }

    void Character::SetWeapon(WeaponBehavior *rhs)
    {
        if(this->weaponBehavior == rhs)
            return;

        if(weaponBehavior)
            delete weaponBehavior;
        this->weaponBehavior = rhs;
    }

    Character::Character()
    {
        weaponBehavior = new NoBehavior();
    }

}

Test.cpp

#include <iostream>
#include "StrategyPattern.h"

using namespace StrategyPatternDesign;

void main()
{
    Character *king = new King();
    king->SetWeapon(new KnifeBehavior());
    king->Fight();
    king->SetWeapon(new SwordBehavior());
    king->Fight();

    Character *knight = new Knight();
    knight->Fight();
    knight->SetWeapon(new AxeBehavior());
    knight->Fight();

    delete king;
}

Results:

本文链接地址:Strategy Design Pattern (C++)