Start learning free

LESSON 318 OF 343 · DESIGN PATTERNS

Prototype Pattern

Practice Creational

A game may need many enemies that start out alike. Building each one by hand repeats the same setup. The code that does it must also know how every kind is built.

The Prototype pattern keeps one ready-made example, the prototype. New objects are made by asking it to copy itself. Each copy can then change without touching the example.

Here you write an enemy spawner for a game, in three classes.

First write the interface Prototype. It has one method, clone(), which returns a copy. An interface lists the methods a class promises to have. A class that implements it must write each one.

Then write the class Enemy, which implements Prototype:

  • Enemy(kind, hp) makes an enemy of that kind, with hp hit points and no loot.
  • clone() returns a new Enemy with the same kind and hit points. It also gets its own copy of the loot list, with the same items.
  • hit(damage) takes damage off the hit points. They never go below 0.
  • addLoot(item) adds one item to the loot list. An item may come twice, and then it counts twice.
  • describe() returns the kind, the hit points and the number of loot items, as in "orc 40 2".

Last, write Spawner:

  • Spawner() starts with no prototypes.
  • register(name, prototype) keeps that very prototype under the name. A name used again replaces the old prototype.
  • spawn(name) returns clone() of the prototype kept under that name.

A clone is a new object, not a second name for the old one. Changing a clone never changes its prototype. Changing the prototype later never changes the clones made before.

spawn(name) returns a Prototype, so the spawner never needs to know which class it copies. The tests know it is an Enemy and use it as one.

The tests build every prototype themselves and keep their own reference to it. Every enemy they fight comes from spawn(name). Each line of the input is one command:

  • proto boss dragon 500 builds Enemy("dragon", 500) and registers it under the name boss.
  • ploot boss gold adds gold to the loot of the prototype boss.
  • spawn boss spawns an enemy from boss and writes its number. Spawned enemies are numbered 1, 2, 3 and so on.
  • hit 2 15 calls hit(15) on enemy 2. loot 2 sword calls addLoot("sword") on it.
  • show 2 writes the describe() of enemy 2. pshow boss writes the describe() of the prototype boss.

Each test case starts with a new spawner and no enemies.

 

Example 1:

Input:
1. proto orc orc 40
2. spawn orc
3. spawn orc
4. hit 1 15
5. hit 2 50
6. show 1
7. show 2
8. pshow orc
Output:
1
2
orc 25 0
orc 0 0
orc 40 0
Explanation:
Enemies 1 and 2 are two copies of the orc.
Enemy 1 loses 15, so it has 25 left.
Enemy 2 loses 50, but its hit points stop at 0.
The prototype still has 40.

Example 2:

Input:
1. proto boss dragon 500
2. ploot boss gold
3. spawn boss
4. loot 1 sword
5. loot 1 shield
6. ploot boss gem
7. spawn boss
8. show 1
9. show 2
10. pshow boss
Output:
1
2
dragon 500 3
dragon 500 2
dragon 500 2
Explanation:
Enemy 1 is a copy of the dragon, with its gold.
Then it gets a sword and a shield, so it has 3 items.
The gem goes to the prototype after enemy 1 was spawned.
So only enemy 2 has it. Its items are the gold and the gem.
The prototype has the same 2 items.

Example 3:

Input:
1. proto grunt goblin 30
2. ploot grunt coin
3. spawn grunt
4. proto grunt troll 90
5. spawn grunt
6. hit 1 5
7. show 1
8. show 2
9. pshow grunt
Output:
1
2
goblin 25 1
troll 90 0
troll 90 0
Explanation:
Command 4 replaces the prototype grunt with a troll.
Enemy 1 was spawned before that. It stays a goblin with its coin.
The hit leaves it 25 hit points.
Enemy 2 is a copy of the troll.

 

Constraints:

  • A test case has at most 100,000 commands.
  • It uses at most 1,000 prototype names.
  • A name, a kind or an item has 1 to 12 lowercase letters.
  • Hit points and damage are whole numbers from 1 to 1,000,000.
  • No enemy ever has more than 20 loot items.
  • A command names only a registered prototype, or an enemy already spawned.

Note:

Each method should run in O(1) time. An enemy has at most 20 loot items, so copying them takes O(1) time too.

STEP 1 OF 17
    Ctrl + Enter to check