gof- prototype model

Prototype Mode: design based on class will eventually emerge 

  And process-oriented compared to object-oriented is characteristic of the state (data) and behavior (logic) bound, class-based OOP design will be some uncomfortable design

Reference scenario: avoid code redundancy, simplified operation behavior

example:

<- symbol indicates inheritance

class Monster
{
  //...
};
class Ghost : public Monster {};
class Demon : public Monster {};
class Sorcerer : public Monster {};
class Spawner
{
public:
  virtual ~Spawner() {}
  virtual Monster* spawnMonster() = 0;
};
class GhostSpawner : public Spawner
{
public:
  virtual Monster* spawnMonster()
  {
    return new Ghost();
  }
};
class DemonSpawner : public Spawner
{
public:
  virtual Monster* spawnMonster()
  {
    return new Demon();
  } 
}; 
// you know

A builder:

typedef Monster* (*SpawnCallback)();
class Spawner
{
public:
  Spawner(SpawnCallback spawn) : spawn_(spawn){}
  Monster* spawnMonster()
  {
    return spawn_();
  }
private:
  SpawnCallback spawn_;
};

This example is too vague feeling that most people would never be so foolish as to create an object using the above way

Copy a game from the book of original programming mode to expand about the feelings of the prototype:

 

Guess you like

Origin www.cnblogs.com/chenggg/p/11332965.html