O'Reilly logo

C# 3.0 Design Patterns by Judith Bishop

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Prototype Pattern Example Code—Photo Archive

See the following code for an example of the Prototype Pattern:

Here is an using System; using System.Collections.Generic; using System.IO; using PrototypePattern; namespace CompositePattern { // The Composite Pattern namespace // including the Share operations // The Interface public interface IComponent <T> { void Add(IComponent <T> c); IComponent <T> Remove(T s); string Display(int depth); IComponent <T> Find(T s); IComponent <T> Share (T s,IComponent <T> home); string Name {get; set;} } // The Composite [Serializable( )] public class Composite <T> : IPrototype <IComponent <T>>, IComponent <T> { List <IComponent <T>> list; public string Name {get; set;} public Composite (string name) { Name = name; list = new List <IComponent <T>> ( ); } public void Add(IComponent <T> c) { list.Add(c); } // Finds the item from a particular point in the structure // and returns the composite from which it was removed // If not found, return the point as given public IComponent <T> Remove(T s) { holder = this; IComponent <T> p = holder.Find(s); if (holder!=null) { (holder as Composite<T>).list.Remove(p); return holder; } else return this; } IComponent <T> holder=null; // Recursively looks for an item // Returns its reference or else null public IComponent <T> Find (T s) { holder = this; if (Name.Equals(s)) return this; IComponent <T> found=null; foreach (IComponent <T> c in list) { found = c.Find(s); if (found!=null) break; } return found; } public IComponent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required