Advertisement
Slash18

Prototype Pattern

Jun 29th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1.  
  2. //Full tutorial on https://indiedevart.wordpress.com/
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace PrototypePattern
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Enemy enemy = new Enemy();
  17.             enemy.Id = 1;
  18.  
  19.             Console.WriteLine("Enemy Copy ID: " + enemy.Id.ToString());
  20.  
  21.             // We enter the cheat code here and we have a new
  22.             // player object that we can save on the disk asyncronously.
  23.             Enemy clonedEnemy = enemy.Clone() as Enemy;
  24.  
  25.             Console.WriteLine("Enemy Copy ID: " + clonedEnemy.Id.ToString());
  26.             Console.ReadLine();
  27.                
  28.         }
  29.     }
  30.     public abstract class AbstractEnemy
  31.     {
  32.         int _id;
  33.  
  34.         public int Id
  35.         {
  36.             get { return _id; }
  37.             set { _id = value; }
  38.         }
  39.         public abstract AbstractEnemy Clone();
  40.     }
  41.     class Enemy : AbstractEnemy
  42.     {
  43.         public override AbstractEnemy Clone()
  44.         {
  45.             Console.WriteLine("cloning enemy");
  46.             return this.MemberwiseClone() as AbstractEnemy;
  47.         }
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement