473,322 Members | 1,781 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,322 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 3103
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.