游戏中的设计模式之抽象工厂模式
2012-07-05 01:00
239 查看
游戏中需要用到抽象工厂的地方很多,比如:你想得到一个怪物monster,而我们在游戏中可能会出现几十种怪物,而且每种怪物都有自己特有技能,如:大蛇可以发射毒液使你减速,熊会有重击技能可以将你打晕,老虎会有暴击技能等等。而怎样将这些怪初始化并能够与游戏中的人物联系起来,这地方使用抽象工厂可以解决这些问题。
在抽象工厂中需要我们多用对象组合,少用继承,针对抽象编程,不针对实现编程,对象通过工厂暴露的方法创建。
下面我们举例解释抽象工厂在游戏中的运用:
1.
我们建立一个所有怪物的父类,我们叫他兽王(也许兽王应该是所有怪物的boss的,但是我感觉还是先有兽王才会产生其他怪兽小弟的),给它个英文名BaseMonster,好了,我们的兽王有了,兽王都有什么呢?名字,技能,血量等等,我们先用这三个吧。
Class Abstract BaseMonster{
protected String name;
protected int HP;
protected String skill;
}
2.
所有的怪物需要做一些共同的操作,我们可以用接口来实现这些操作,这里我们只需要一个方法print,输出当前的怪物的名字。
public interface IFMonster{
void printMessage();
}
3.
接下来我们就要实现每个种类的怪物的父类了。
所有蛇的父类snake
public class Snake extends BaseMonster implements IFMonster{
void printMessage(){
System.out.println(“-”+this.name+”蛇”+”-会用技能”+this.skill);
}
}
所以熊的父类Bear
public class Bear extends BaseMonster implements IFMonster{
void printMesssage(){
System.out.println(“-”+this.name+”熊”+”-会用技能”+this.skill);
}
}
所有老虎的父类Tiger
public class Tiger extends BaseMonster implements IFMonster{
void printMessage(){
System.out.println(“”+this.name+”老虎”+”-会用技能”+this.skill);
}
}
4.
接下来我们就需要使用工厂类能够产生各种怪,MonsterFactory
public abstract class MonsterFactory{
public Snake createSnake(int hp);
Public Bear createBear(int hp);
Public Tiger createTiger(int hp);
}
5.
有了抽象工厂,那么我们接下来就需要具体的怪了,比如眼镜蛇,北极熊,东北虎
public class YJsnake extends Snake{
Public YJsnake(int hp){
this.name=”眼睛”;
this.HP=hp;
this.skill=“十倍毒液“;
}
}
public class BJbear extends Bear{
public BJbear(int hp){
this.name=”北极”
this.HP=hp;
this.skill=”重击”
}
}
public class DBtiger extends Tiger{
public DBtiger(int hp){
this.name=”东北”
this.HP=hp
this.skill=”暴击”
}
}
6.
有了这些具体的怪,我们就可以实现抽象怪工厂,产生具体工厂MyFactory
public class MyFactory extends MonsterFactory{
public Snake createSnake(int hp){
return new YJsnake(hp);
}
pulic bear createBear(int hp){
return new BJbear(hp);
}
public tiger createTiger(int hp){
return new DBtiger(hp);
}
}
7.
我们需要一个类来使用我们每个实现的类,Hero
public class Hero{
public MonsterFactory monsterFactory;
public Hero(MonsterFactory facotry){
this.monsterFactory=factory;
}
public int hitSnake(int hp){
Snake snake=monsterFactory.createSnake(hp);
snake.printMessage();
return snake.hp-50;
}
public int hitBear(int hp){
Bear bear=monsterFactory.createBear(hp);
bear.printMessage();
return bear.hp-50;
}
public int hitTiger(int hp){
Tiger tiger=monsterFactory.createTiger(hp);
tiger.printMessage();
return tiger.hp-50;
}
}
8.最后需要在Main里面调用Hero就可以了
void Main(){
MonsterFacotry monsterFactory=new MyFactory();
Hero hero=new Hero(monsterFactory);
int snakeBlood= hero.hitSnake(100);
int bearBlood= hero.hitBear(200);
int tigerBlood= hero.hitTiger(300);
//输出所有怪掉的血量
。。。。。
}
以上代码仅供参考。
在抽象工厂中需要我们多用对象组合,少用继承,针对抽象编程,不针对实现编程,对象通过工厂暴露的方法创建。
下面我们举例解释抽象工厂在游戏中的运用:
1.
我们建立一个所有怪物的父类,我们叫他兽王(也许兽王应该是所有怪物的boss的,但是我感觉还是先有兽王才会产生其他怪兽小弟的),给它个英文名BaseMonster,好了,我们的兽王有了,兽王都有什么呢?名字,技能,血量等等,我们先用这三个吧。
Class Abstract BaseMonster{
protected String name;
protected int HP;
protected String skill;
}
2.
所有的怪物需要做一些共同的操作,我们可以用接口来实现这些操作,这里我们只需要一个方法print,输出当前的怪物的名字。
public interface IFMonster{
void printMessage();
}
3.
接下来我们就要实现每个种类的怪物的父类了。
所有蛇的父类snake
public class Snake extends BaseMonster implements IFMonster{
void printMessage(){
System.out.println(“-”+this.name+”蛇”+”-会用技能”+this.skill);
}
}
所以熊的父类Bear
public class Bear extends BaseMonster implements IFMonster{
void printMesssage(){
System.out.println(“-”+this.name+”熊”+”-会用技能”+this.skill);
}
}
所有老虎的父类Tiger
public class Tiger extends BaseMonster implements IFMonster{
void printMessage(){
System.out.println(“”+this.name+”老虎”+”-会用技能”+this.skill);
}
}
4.
接下来我们就需要使用工厂类能够产生各种怪,MonsterFactory
public abstract class MonsterFactory{
public Snake createSnake(int hp);
Public Bear createBear(int hp);
Public Tiger createTiger(int hp);
}
5.
有了抽象工厂,那么我们接下来就需要具体的怪了,比如眼镜蛇,北极熊,东北虎
public class YJsnake extends Snake{
Public YJsnake(int hp){
this.name=”眼睛”;
this.HP=hp;
this.skill=“十倍毒液“;
}
}
public class BJbear extends Bear{
public BJbear(int hp){
this.name=”北极”
this.HP=hp;
this.skill=”重击”
}
}
public class DBtiger extends Tiger{
public DBtiger(int hp){
this.name=”东北”
this.HP=hp
this.skill=”暴击”
}
}
6.
有了这些具体的怪,我们就可以实现抽象怪工厂,产生具体工厂MyFactory
public class MyFactory extends MonsterFactory{
public Snake createSnake(int hp){
return new YJsnake(hp);
}
pulic bear createBear(int hp){
return new BJbear(hp);
}
public tiger createTiger(int hp){
return new DBtiger(hp);
}
}
7.
我们需要一个类来使用我们每个实现的类,Hero
public class Hero{
public MonsterFactory monsterFactory;
public Hero(MonsterFactory facotry){
this.monsterFactory=factory;
}
public int hitSnake(int hp){
Snake snake=monsterFactory.createSnake(hp);
snake.printMessage();
return snake.hp-50;
}
public int hitBear(int hp){
Bear bear=monsterFactory.createBear(hp);
bear.printMessage();
return bear.hp-50;
}
public int hitTiger(int hp){
Tiger tiger=monsterFactory.createTiger(hp);
tiger.printMessage();
return tiger.hp-50;
}
}
8.最后需要在Main里面调用Hero就可以了
void Main(){
MonsterFacotry monsterFactory=new MyFactory();
Hero hero=new Hero(monsterFactory);
int snakeBlood= hero.hitSnake(100);
int bearBlood= hero.hitBear(200);
int tigerBlood= hero.hitTiger(300);
//输出所有怪掉的血量
。。。。。
}
以上代码仅供参考。
相关文章推荐
- c++ 设计模式9 (Abstract Factory 抽象工厂模式)
- java设计模式之抽象工厂模式
- 设计模式(一)简单工厂模式工厂模式 抽象工厂模式详解
- PHP实现设计模式中的抽象工厂模式详解
- 游戏开发设计模式之对象池模式(unity3d 示例实现)
- 设计模式之抽象工厂模式
- 设计模式->创建型模式->抽象工厂模式
- 设计模式之抽象工厂模式
- 游戏开发入门(二)游戏中的设计模式
- 23种经典设计模式的java实现_1_抽象工厂模式
- 设计模式复习笔记 (4)创建型模式:抽象工厂模式
- 设计模式――抽象工厂模式(Abstract Factory Pattern)
- 设计模式 抽象工厂模式(Abstract Factory)
- 初学设计模式【6】抽象工厂模式——AbstractFactory
- 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)
- 设计模式--【抽象工厂模式】
- C# 设计模式----抽象工厂模式
- 设计模式--2: Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
- C#设计模式 抽象工厂模式
- 设计模式总结之Abstruct Factory Pattern(抽象工厂模式)