473,883 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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. RunClassConstru ctor 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 3154
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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.co m>
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
2045
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 web application using PHP, to which I am relatively new. I have seen some sites use the model whereby a single index page is created that handles authentication and receives option parameters telling the index what to load in the body of the...
2
3493
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 that I am facing. aClient aDirector aConcreteBuilder =============================================================================
3
2011
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 anywhere... I have simplified my design problem to the following: Consider the following classes:
1
4267
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 right approach and the different point of views of different programmers. one common problem is a tree structure where a node is a son of another one and nodes can be of different types. i can think about some tables in a database (like access or...
10
2142
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 shouldn't be here). I have two design questions: 1. what is the correct (or best) way to include database queries into the code if you plan on
3
264
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 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...
16
2346
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 always dream to achieve a refined and reusable design and implementation. I am very interested with code reuse.
2
2304
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 ChildB) derived from class Base. Now i want to create an object, but i don't want to know which class to instantiate.
22
4758
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. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
0
9938
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9791
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10414
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3232
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.