No nonsense C # design pattern five: Prototype

Source: Internet
Author: User
Tags abstract bool

Intention

Use the prototype instance to specify the type of object to create and create new objects by copying them.

Scene

There are many similar enemies in the game scene, their skills are the same, but as the enemy appears in different positions, these people's ability is not the same. Let's say we now need to make a team of three infantry, including an elite infantry, with a particularly high capacity. Well, you might be able to create an enemy abstract class, and then create different subclasses for the infantry with different abilities. Then, use design patterns such as factory methods to let callers rely on enemy abstract classes.

The problem is, if there are countless different types of infantry, do you need to create countless subclasses? Also, the initialization of the infantry model is time-consuming, and creating so many infantry objects can be a waste of time. Is it possible to create a single infantry prototype and then replicate multiple infantry with the same touch? After copying, you just need to adjust the location of the objects on the map, or adjust their capabilities. The prototype model is used to solve this problem.

Sample code

using System;





using System.Threading;





using System.Collections.Generic;





using System.IO;





using System.Runtime.Serialization.Formatters.Binary;





using System.Diagnostics;





namespace Prototypeexample





{





Class Program





    {





static void Main (string[] args)





        {





Stopwatch SW = new Stopwatch ();





SW. Start ();





enemy Enemyprototype = new Footman (5, 4, New Location (100, 200));





gamescene GS = new Gamescene ();





list<enemy> enemygroup = Gs. Createenemygroup (Enemyprototype);





foreach (footman ft in Enemygroup)





            {





ft. Showinfo ();





ft. Footmanattack ();





            }





Console.WriteLine (SW. Elapsedmilliseconds);





        }





    }





class Gamescene





    {





Public list<enemy> Createenemygroup (enemy Enemyprototype)





        {





list<enemy> enemygroup = new list<enemy> ();





Enemy e1 = Enemyprototype.clone (TRUE);





E1. location.x = enemyprototype.location.x-10;





enemy E2 = Enemyprototype.clone (true);





E2. location.x = enemyprototype.location.x + 10;





Enemy Elite = Enemyprototype.clone (TRUE);





Elite. Power = Enemyprototype.power * 2;





Elite. Speed = Enemyprototype.speed * 2;





Elite. location.x = enemyprototype.location.x;





Elite. LOCATION.Y = enemyprototype.location.y + 10;





Enemygroup.add (E1);





Enemygroup.add (E2);





Enemygroup.add (Elite);





return enemygroup;





        }





    }





[Serializable]





class Location





    {





public int x;





public int y;





public Location (int x, int y)





        {





this.x = x;





this.y = y;





        }





    }





[Serializable]





abstract class Enemy





    {





protected Location Location;





Public Location Location





        {





get {return location;}





set {location = value;}





        }





protected int power;





public int Power





        {





get {return power;}





set {power = value;}





        }





protected int speed;





public int Speed





        {





get {return speed;}





set {speed = value;}





        }





public abstract Enemy Clone (bool isdeepcopy);





public abstract void Showinfo ();





Public Enemy (int power, int speed, Location Location)





        {





Thread.Sleep (1000); Construct is assumed to be a high calc work.





this.power = power;





this.speed = speed;





this.location = location;





        }





    }





[Serializable]





class Footman:enemy





    {





private string model;





public footman (int power, int speed, Location Location)





: Base (power, speed, location)





        {





model = "footman";





        }





public override void Showinfo ()





        {





Console.WriteLine ("Model: {0} power:{1} speed:{2} location: ({3},{4})", model, power, speed, location.x, LOCATION.Y);





        }





public override enemy Clone (bool isdeepcopy)





        {





footman footman;





if (isdeepcopy)





            {





MemoryStream MemoryStream = new MemoryStream ();





BinaryFormatter formatter = new BinaryFormatter ();





Formatter. Serialize (MemoryStream, this);





memorystream.position = 0;





footman = (footman) formatter. Deserialize (MemoryStream);





            }





Else





Footman = (footman) this. MemberwiseClone ();





return footman;





        }





public void Footmanattack ()





        {





Console.WriteLine ("Footmanattack");





        }





    }





}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.