473,378 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Design Pattern Problem

The Top Level Design:

The class Base is a factory class with a twist. It uses the Assembly/Type
classes to extract all types that inherit from it and add them to the list
of types that inherit from it. During run time, using a static method, the
class creates an instance of the derived class using the Activator class and
returns it. This design pattern is very similar to the design pattern
applied by the Assembly class.

The twist is that all inheritors of Base must specify a small amount of
metadata. The metadata will be hard-coded into the derived classes and a
lack of the said metadata must be discovered at compile time.

Design Constraints:

a.. Base is not a component.
b.. Derived classes may be inherited as well.
c.. All derived classes must have unique metadata.

There are several ways to approach this problem:

Design Approach #1:

Base will expose an abstract property that will, when called, return the
value. This, in turn, will require a derived class to override the property
and thus exposing the metadata.

When the factory would be filled, Base would approach all classes that
derive from it and are not abstract and extract the metadata by calling the
property.

Advantages:

There will be compiler intervention in case of failure to override.

Problem #1:

If Base is derived by the non-abstract class A (A is Base) and A is derived
by the non-abstract class B (B is A, B is Base), there is a high probability
for the third constraint to be violated, since there is no compiler error
when not overriding the property in class B (since it has already been
overridden in class A).

Problem #2:

Calling this method from the Base class will require creation of an instance
of the derived class, which is both costly and redundant.

Design Approach #2:

Any implementation of a derivative of Base will require programmers to write
a static property that will return the metadata.

When the factory would be filled, Base would approach each derived class and
extract the metadata by calling the static property.

Advantages:

A static property will not require the construction of an instance of the
derived class.

Problem:

Programmers may and will forget to implement the said property, since there
is no compiler error when it is written and there is no mechanism in the
language that requires a derived class to implement a static
property/method.

Design Approach #3:

Any implementation of a derivative of Base will require programmers to have
a custom attribute set on their derived class and that attribute will be
read at run time.

When the factory would be filled, Base would approach each derived class and
extract the metadata by getting the custom attribute.

Advantages:

This design approach seems the most logical, since attributes are a language
element that was created to describe metadata for classes.

Problem:

Programmers may and will forget to implement said property, since there is
not compiler error when it is written and there is no mechanism in the
framework that requires an attribute to be placed on a class.

Design Approach #4:

When the factory would be filled, Base would explicitly call the static
constructors using the RunTimeHelpers.RunClassConstructor of all derived
classes that, in turn, would register themselves with the base class.

Advantages:

This design pattern works with current compiler / language abilities.

Problem #1:

Programmers may and will forget to implement an explicit type initializer,
since there is no compiler error when it is written and there is no
mechanism in the language that requires a derived class to implement an
explicit type initializer.

Problem #2:

This method places the responsibility of registration in the hands of the
programmer of the derived class.

Problem #3:

This is too much of a Hack-ish approach to be a design approach.

My Conclusion:

With the current tools in the hands of the programmer today, there is no
ability to implement this kind of special factory design pattern.

Does anyone have an idea as to how to approach this situation?
Jul 21 '05 #1
3 3106
Omer van Kloeten <gv******@spaz.ice.org> wrote:

<snip>
My Conclusion:

With the current tools in the hands of the programmer today, there is no
ability to implement this kind of special factory design pattern.

Does anyone have an idea as to how to approach this situation?


I would suggest the second approach, combined with a tool which can
very easily be run in the "post build" phase which checks that all
classes which should have the appropriate attribute do indeed have it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Jon,
I believe you mean the third approach.
I have thought of using an external tool, but just like I don't "trust"
people to write the attribute themselves, I don't "trust" them to use that
external tool in their post-build events, as I will have no control over the
customers once the product will ship.
And besides, having to create an external tool reduces the strength and
wholeness of this design pattern.

Omer

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Omer van Kloeten <gv******@spaz.ice.org> wrote:

<snip>
My Conclusion:

With the current tools in the hands of the programmer today, there is no
ability to implement this kind of special factory design pattern.

Does anyone have an idea as to how to approach this situation?


I would suggest the second approach, combined with a tool which can
very easily be run in the "post build" phase which checks that all
classes which should have the appropriate attribute do indeed have it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
Omer van Kloeten <gv******@spaz.ice.org> wrote:
I believe you mean the third approach.
Yes, sorry.
I have thought of using an external tool, but just like I don't "trust"
people to write the attribute themselves, I don't "trust" them to use that
external tool in their post-build events, as I will have no control over the
customers once the product will ship.
Yup, that's fair.
And besides, having to create an external tool reduces the strength and
wholeness of this design pattern.


Absolutely - it's just an unfortunate fact of life at the moment :(
Certainly it's not ideal - it would be nice if there could be some way
of writing attributes that affect compilation in a custom way, although
I don't know what the API to such a facility would look like.

I don't know exactly what your situation is, but you could also do the
verification when the assembly is loaded, and write out warnings if the
relevant information isn't present. I would at least hope that your
customers would test their plug-in implementations once before trying
to use them in a production environment! It's not a *great* solution,
but it may well be the best you can do at the moment.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mike Sutton | last post by:
I had first tried a less active PHP group, but let me try here. I searched for this, but didn't find anything that appeared related. Possibly I didn't know what to search for. I am designing a...
2
by: qazmlp | last post by:
Builder pattern seem to suit my requirements perfectly except in one case as described below. I have used the same example diagram that is used in GOF Design pattern book, to explain the problem...
3
by: Merlin | last post by:
Design Problem =============== Would appreciate any help or suggestion on this design issue. I have spent a great deal of time and effort for an elegant solution but it seems I am not getting...
1
by: Takayasu Kenduma | last post by:
hi everybody. i am quite new about design patterns and i would like improve my experience in c# applications design. so i choose a common problem as a draft to talk about in the group to find the...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
16
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I...
2
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.