天天看点

智取设计模式之简单工厂模式

在游戏中,每个玩家或怪物都会有各自的状态,本文中将会分别使用UML,C#,C++实现简单工厂模式给玩家或怪物添加状态。 一、简介 简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一,而在大话设计模式中则作为开头篇介绍。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现,也是作为学习工厂方法模式的一个引导。实际编程过程中会经常使用,思路简单易懂。 通俗的讲:简单工厂类就是为负责生产对象的一个类,而 被创建的类通常都具有共同的父类或接口。 二、优缺点 最大的优点在于简单工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体类的依赖。但却违背了设计模式六大原则中的开放-封闭原则。 三、UML类图

智取设计模式之简单工厂模式

在图1中包含的角色及相应的功能如下: 1.PlayerStateBase代表简单工厂类角色,其中包含了必要的逻辑判断及操作,是简单工厂模式的核心。同时也是具体类角色的父类。 2.而它的派生类则是具体类,由简单工厂所要创建的具体实例对象。 也可以如图2将图1中的简单工厂类角色分为工厂角色和具体类角色的抽象类。但本文代码则以图1的方式进行演示。两者区别不大。

智取设计模式之简单工厂模式

四、C#代码演示 1.首先定义好State类型

public enum StateType
    {
        State_Poisoning = 0,
        State_Silnet,
        State_Invincible,
    }
           

2.PlayerStateBase类

public class PlayerStateBase
    {
        public static PlayerStateBase AddState(StateType mType)
        {
            switch (mType)
            {
                case StateType.State_Poisoning:
                    return new StatePoisoning();
                case StateType.State_Silnet:
                    return new StateSilnet();
                case StateType.State_Invincible:
                    return new StateInvincible();
                default:
                    break;
            }
            return null;
        }

        public void Start()
        {
            OnAdd();
        }

        public void Destroy()
        {
            OnDestroy();
        }

        protected virtual void OnAdd()
        {

        }

        protected virtual void OnDestroy()
        {

        }
        
    }
           

3.每个状态类

public class StatePoisoning : PlayerStateBase
    {
        protected override void OnAdd()
        {
            Console.WriteLine("-----AddStatePoisoning----");
        }

        protected override void OnDestroy()
        {
            Console.WriteLine("-----DeleStatePoisoning----");
        }
    }
           
public class StateSilnet : PlayerStateBase
    {
        protected override void OnAdd()
        {
            Console.WriteLine("-----AddStateSilnet----");
        }

        protected override void OnDestroy()
        {
            Console.WriteLine("-----DeleStateSilnet----");
        }
    }
           
public class StateInvincible : PlayerStateBase
    {
        protected override void OnAdd()
        {
            Console.WriteLine("-----AddStateInvincible----");
        }

        protected override void OnDestroy()
        {
            Console.WriteLine("-----DeleStateInvincible----");
        }
    }
           

4.Main方法

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("--------------------华丽的分隔线---------------------");
            PlayerStateBase _playerState = null;
            _playerState = PlayerStateBase.AddState(StateType.State_Invincible);
            _playerState.Start();
            _playerState.Destroy();
            Console.WriteLine("--------------------华丽的分隔线---------------------");
            _playerState = PlayerStateBase.AddState(StateType.State_Poisoning);
            _playerState.Start();
            _playerState.Destroy();
            Console.WriteLine("--------------------华丽的分隔线---------------------");
            _playerState = PlayerStateBase.AddState(StateType.State_Silnet);
            _playerState.Start();
            _playerState.Destroy();
        }
    }
           

5.结果

智取设计模式之简单工厂模式

五、C++代码演示

1.PlayerStateBase.h 头文件

#ifndef CPLAYERSTATEBASE_H
#define CPLAYERSTATEBASE_H
#include <iostream>
using namespace std;
enum EStateType
{
	EStatePoisoning = 0,
	EStateSilent,
	EStateInvincible,

};

class CPlayerStateBase
{
public:
	static CPlayerStateBase * AddState(EStateType & mType);
	void Start();
	void Destroy();
protected:
	virtual void OnAdd();
	virtual void OnDestroy();
};
#endif
           

2. PlayerStateBase.cpp

#include "StatePoisoning.h"
#include "StateSilent.h"
#include "StateInvincible.h"
CPlayerStateBase * CPlayerStateBase::AddState(EStateType & mType)
{
	switch(mType)
	{
	case EStatePoisoning :
		return new CStatePoisoning();
	case EStateSilent :
		return new CStateSilent();
	case EStateInvincible :
		return new CStateInvincible();
	}
}
void CPlayerStateBase::Start()
{
	OnAdd();
}
void CPlayerStateBase::Destroy()
{
	OnDestroy();
}
void CPlayerStateBase::OnAdd()
{
}

void CPlayerStateBase::OnDestroy()
{
}
           

3.每个状态类 (1)State--Invincible

#include "CPlayerStateBase.h"

class CStateInvincible : public CPlayerStateBase
{
protected:
	virtual void OnAdd();
	virtual void OnDestroy();
};
           
#include "StateInvincible.h"
void CStateInvincible ::OnAdd()
{
	cout<<"AddStateInvincible"<<endl;
}

void CStateInvincible ::OnDestroy()
{
	cout<<"DeleStateInvincible"<<endl;
}
           

(2)State--Poisoning

#include "CPlayerStateBase.h"

class CStatePoisoning : public CPlayerStateBase
{
protected :
	virtual void OnAdd(); 
	virtual void OnDestroy();
};
           
#include "StatePoisoning.h"
void CStatePoisoning ::OnAdd()
{
	cout<<"AddStatePoisoning"<<endl;
}

void CStatePoisoning ::OnDestroy()
{
	cout<<"DeleStatePoisoning"<<endl;
}
           

(3)State--Silent

#include "CPlayerStateBase.h"

class CStateSilent : public CPlayerStateBase
{
protected:
	virtual void OnAdd();
	virtual void OnDestroy();
};
           
#include "StateSilent.h"
void CStateSilent ::OnAdd()
{
	cout<<"AddStateSilent"<<endl;
}

void CStateSilent ::OnDestroy()
{
	cout<<"DeleStateSilent"<<endl;
}
           

4.Main方法

#include "CPlayerStateBase.h"
using namespace std;
void main()
{
	cout<<"--------------------华丽的分隔线---------------------"<<endl;
	EStateType _type = EStatePoisoning;
	CPlayerStateBase * _state = NULL;
	_state = CPlayerStateBase::AddState(_type);
	_state->Start();
	_state->Destroy();
	cout<<"--------------------华丽的分隔线---------------------"<<endl;
	_type = EStateSilent;
	_state = CPlayerStateBase::AddState(_type);
	_state->Start();
	_state->Destroy();
	cout<<"--------------------华丽的分隔线---------------------"<<endl;
	_type = EStateInvincible;
	_state = CPlayerStateBase::AddState(_type);
	_state->Start();
	_state->Destroy();
	cout<<"--------------------华丽的分隔线---------------------"<<endl;

	delete _state;
}
           

5.结果

智取设计模式之简单工厂模式

继续阅读