Friday, May 2, 2014

Prototype Collection

Objects in Object Oriented Programming can have arbitrary values. For instance, a string can contain any number of characters and a character can have any value.

However, not all values are relevant with regards to the problem space.

States

From Java, a classic example is the Planet class.

Planet data includes:

  • Average distance from the sun
  • Diameter at equator
  • Axis tilt
  • Rotation direction
  • Length of day
  • Length of year
  • Number of satellites
  • Type (Rocky, gaseous, etc.)

Obviously fields can have any value. There are an infinite possibility when it comes to planets. However, we only have 8 formally recognized planets in our solar system (the rest are planetoids, asteroids, etc.)

When we create an object of type Planet, we specify values for each property of the planet object. For instance, for Earth, we define LengthOfDay to be 86,400 seconds.

This is where prototypes come in.

Prototypes

A prototype is an object containing predefined values that has special significance to us.

This prototype is then copied and used wherever it is needed.

Using the Planet example, we can define prototypes for all the official solar system planets - Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.

(Pluto is in another list called Planetoids, which possibly inherits from Planet)

The concept of the prototype was so significant that the Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) included it in their book, Design Patterns: Elements of Reusable Object-Oriented Software (Copyright 1995).

Serialization

Serialization is a process of storing the state of an object in a portable format, such as a text string or XML document. This allows us to create a duplicate object at a later time.

Using serialization we can store object states that have significance for later use.

Prototype Collection

Since we have any number of prototypes for a specific object, we can collect these prototypes into a collection.

In java, this concept is encapsulated in its enumeration structure:

 

 

 

 

 

C# -
  1. public class Planet
  2. {
  3.     public static readonly Planet Mercury = new Planet("Mercury", 3.303e+23, 2.4397e6);
  4.     public static readonly Planet Venus = new Planet("Venus", 4.869e+24, 6.0518e6);
  5.     public static readonly Planet Earth = new Planet("Earth", 5.976e+24, 6.37814e6);
  6.     public static readonly Planet Mars = new Planet("Mars", 6.421e+23, 3.3972e6);
  7.     public static readonly Planet Jupiter = new Planet("Jupiter", 1.9e+27, 7.1492e7);
  8.     public static readonly Planet Saturn = new Planet("Saturn", 5.688e+26, 6.0268e7);
  9.     public static readonly Planet Uranus = new Planet("Uranus", 8.686e+25, 2.5559e7);
  10.     public static readonly Planet Neptune = new Planet("Neptune", 1.024e+26, 2.4746e7);
  11.     public static readonly Planet Pluto = new Planet("Pluto", 1.27e+22, 1.137e6);
  12.  
  13.     private Planet(string name, double mass, double radius)
  14.     {
  15.         this.name = name;
  16.         this.mass = mass;
  17.         this.radius = radius;
  18.     }
  19.  
  20.     public static IEnumerable<Planet> Values
  21.     {
  22.         get
  23.         {
  24.             yield return Mercury;
  25.             yield return Venus;
  26.             yield return Earth;
  27.             yield return Mars;
  28.             yield return Jupiter;
  29.             yield return Saturn;
  30.             yield return Uranus;
  31.             yield return Neptune;
  32.             yield return Pluto;
  33.         }
  34.     }
  35.  
  36.     public string Name { get { return name; } }
  37.     private readonly string name;
  38.  
  39.     public double Mass { get { return mass; } }
  40.     private readonly double mass;   // in kilograms
  41.  
  42.     public double Radius { get { return radius; } }
  43.     private readonly double radius; // in meters
  44. }

 

 

 

 

 

Please keep in mind that a prototype collection has no functionality. The only thing it can do is return a specified prototype and allows you to verify if two prototypes are equivalent.

As a result, many use the Java enumeration type incorrectly, by adding functionality.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

References

http://pdgowdy.wordpress.com/2012/07/27/c-implementation-of-a-java-like-enum/
http://stackoverflow.com/questions/469287/c-sharp-vs-java-enum-for-those-new-to-c
http://www.codeproject.com/Articles/38666/Enum-Pattern

 


 


 


 


 


IceRocket Tags: ,