Design Patterns #2

designp

Template method pattern

The Template Method pattern provides an outline of a series of steps for an algorithm. Objects that implement these steps retain the original structure of the algorithm but have the option to redefine or adjust certain steps. This pattern is designed to offer extensibility to the client developer.

Template Methods are frequently used in general purpose frameworks or libraries that will be used by other developer An example is an object that fires a sequence of events in response to an action, for example a process request. The object generates a ‘preprocess’ event, a ‘process’ event and a ‘postprocess’ event. The developer has the option to adjust the response to immediately before the processing, during the processing and immediately after the processing.

An easy way to think of Template Method is that of an algorithm with holes. It is up to the developer to fill these holes with appropriate functionality for each step.

Template method dofactory – link
Class code: Template method

Reflection in C#

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them

… full article

Class code: reflection demo #1
Class code: reflection demo #2

Template method + SQL + Reflection

Let’s try to build a generic mssql query using template method and reflection

Class code: MSSQL select
Class code: MSSQL select + generics + reflection

The next step will be to implement the Generic query into the template method pattern …

Links:

Leave a comment