Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Five Great Patterns for Dealing with References : Page 3

In the old days, when applications primarily consisted of a number of windows and a database, using reference data was still easy. You would create a reference table and refer to it from your main tables. Nowadays, in object-oriented environments, where your business logic is key, there are more alternatives for dealing with references.


advertisement
Descriptor Pattern
The third pattern, the descriptor pattern, mixes best practices from the previous two patterns. Basically, a descriptor is a collection of instances of the class itself. Using this pattern, the CourseLevels class can be implemented as follows.

public class CourseLevels { public static CourseLevels Beginner = new CourseLevels(); public static CourseLevels Advanced = new CourseLevels(); public static CourseLevels VeryHigh = new CourseLevels(); public static CourseLevels Expert = new CourseLevels(); }

Because the instances are again static, descriptors are type safe, both in simple checks as in operation signatures, just like enumerations, as the following example shows.

if (MyCourse.Level == CourseLevels.Beginner) { … }

Methods on the Course class can also be specified elegantly.


public bool HasLevel(CourseLevels level) { return (Level == level); }

Also descriptors can be extended, like constant collections. In the simple implementation above however, it is not possible to use display values other than the defined names of the instances. Luckily a straightforward extension to this implementation solves these issues, if necessary.

public class CourseLevels { public string DisplayValue; public CourseLevels(string displayvalue) { DisplayValue = display; } public static CourseLevels Beginner = new CourseLevels("Beginner"); public static CourseLevels Advanced = new CourseLevels("Advanced"); public static CourseLevels VeryHigh = new CourseLevels("Very high"); public static CourseLevels Expert = new CourseLevels("Expert"); }

Still, descriptors have a fixed number of elements, just like enumerations and constant collections. And again, like the latter, retrieving the set of possible values requires reflection. In C#, in both patterns, an operation like the following takes care of this job.

public static CourseLevels[] GetValues() { ArrayList list = new ArrayList(); foreach (FieldInfo pi in type.GetFields()) { list.Add((CourseLevels) pi.GetValue(type)); } return (CourseLevels[]) list.ToArray(typeof(CourseLevels)); }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap