Wednesday, September 30, 2009

Week 4 - Delegates and Design Patterns

Delegates:

To implement the M-V-C architecture, we need some means by which updates to the Model can be reflected to the View.  The Model, however, must direct the View, not vice-versa.  Additionally, a polling type of mechanism by the View is not correct, therefore events from the Model must direct the View.

A delegate is a sort of event listener (IObservable in Java, for example).  Created within a namespace, but outside of a class, they are, in essence, subscriptions that objects can subscribe to.

In the Model (the observable class), a queue is created of all the subscribers.  In the View (the observer class), they can subscribe to the event by indicating the method they desire to have called when the event happens. 


Factory Methods:

Factory Methods are: "simple decision making [method] that returns one of several possible subclasses of an abstract (emphasis added) base class, depending on the data provided."
- C# Design Patterns, Cooper, pg. 97, Addison-Wesley 2003

Examples from the HW include...  How do you create the Board class, or the BoardWithClear class?

Other reference for this include:
http://www.dofactory.com/patterns/patternfactory.aspx
http://www.c-sharpcorner.com/UploadFile/rmcochran/instantiationFactory06232007153242PM/instantiationFactory.aspx

Template Classes:

To compliment Factory Methods, there are also Template Methods... 

"The Template pattern formalizes the ides of defining an algorithm in a class but leaving some of the details to be implemented in subclasses." 

- C# Design Patterns, Cooper, pg. 345, Addison-Wesley 2003

To tie the two patterns (factory patten and template pattern) together, an algorithm is completed when the Factory Method selects the proper subclass of a template class, and instantiates it.

See also:
http://www.dofactory.com/patterns/patterntemplate.aspx
http://en.wikipedia.org/wiki/Template_method_pattern
http://www.c-sharpcorner.com/UploadFile/rmcochran/elegant_dal05212006130957PM/elegant_dal.aspx?ArticleID=b9f94d24-6933-48e7-9c16-879ba906d28b

No comments:

Post a Comment