Hello Tom,
Sorry for the delay in responding. I was offline due to a family emergency
for a few weeks.
Tom Krueger said:
Thanks Nick for the response.
If you or anyone wants to take on this challenge the sample that I have
given can be considered a problem to be solved. Forget about the business
aspects of it. Nick I know you asked for more business information,
however,
I'm not asking this question to get alternative solutions, I am interested
in
the specific solution to solve this simple (but may not be possible)
problem.
If you mean "does anyone want to write the code for a custom serializer?"
then that would only be a fairly minor challenge. If I had time, I'd take
you up on it, but alas, I do not.
I have an alternative solution developed and in place which will work
fine,
however, I believe the solution that I am looking for exists and I would
like
to know for the next time this situation occurs. If a simple solution
does
not exist, then it should be brought to the Microsoft Product teams
attention.
A simple solution exists. To write a custom serializer and use it to
serialize your objects. Not that difficult. Very patterns based.
Interestingly what I am more interested in now is our discussion as to
whether this is a strategy pattern or not.
The serialization mechanism in .net framework employs a strategy pattern.
This is what allows you to write a custom serializer. The framework will
call your serializer (concrete strategy) instead of the one supplied with
the framework The strategy pattern allows the developer to write their
code where the developer is not aware (and does not need to be aware) of all
the potential plug-in algorithms for a particular use. As long as the
algorithm fits the interface, it can be called.
Again, I'm not certain that it is
however, I don't see yet how it is not. You say that that an algorithm is
needed and you say that Car and MotorCycle are not algorithms. I guess it
depends on how you define algorithm.
no offense, but a Car is not an algorithm. It is an thing, an object. What
you are encapsulating in your example object heirarchy is that you can have
a generic vehicle type that hides the possibility that the concrete object
may be either a car or a motorcycle. Note that it does a poor job of hiding
this detail, since there are members defined on both car and motorcycle that
are specific to the type. This means that the calling code needs to know
that it is calling a car, and not a vehicle, to use that member. This
creates tight coupling. Unless the calling code is specific to the 'car'
type (as it could be if you create a set of related object types like those
created by the abstract factory), then the calling code is coupled to the
car type and you get NOTHING through your inheritance heirarchy.
Object orientation does not provide any benefit if the designer simply looks
for obvious generalizations to place on an object. OO only benefits you if
you intentionally decide that it is in your best interest to hide something
under a generalization. Inheritance for its own sake creates unmaintainable
code.
My thought is that those creating the
patterns need a generic term to define the structure so I believe
algorithm
is being used loosely.
No. It is not. You have clearly not read the Gang-of-Four book or one of
the many books that do a good job of explaining it. Erich Gamma was writing
his Ph.D. thesis, which he published as that book. His words are specific,
scientifically chosen, and boring as h_ll. There is nothing loose about
design patterns.
Let me ask this, what if we added the method
Accelerate() to Vechical which both Car and MotorCycle them implement.
Would
they then be considered algorithms?
The heirarchy that you define is that of a vehicle with specialized bits for
'car' and 'motorcycle'. So, no, you don't magically change a noun into a
verb by adding a method to the class. If on the other hand, you add an
object called 'TankFiller' to the base class, and in your factory, and
'TankFiller' is a base class with three concrete children called
'RobotFiller,' 'HumanFiller,' and 'CoyoteFiller' and your factory would
decide, when creating the motorcycle object, which filler class to associate
with it, and a method in either the base 'Vehicle' class or the concrete
'Motorcycle' class simply called 'TankFiller.DoYourStuff()' in order to
increase the fuel supply in the vehicle, then I'd say that you have created
and consumed a strategy pattern.
You stated "Patterns represent 'good' or 'best' practices. Not all
practices are good. Therefore, not all heirarchies can be illustrated as
a
pattern". While I agree this is probably a true statement, I don't see
how
it applies here.
I meant to say that you illustrated a heirarchy and, indirectly, asked 'what
pattern is this?' My answer: it is not a pattern because your design does
not illustrate a practice that I would encourage in anyone. If, in fact,
you were to present that class design to me in an architectural review, I'd
be fairly curious about what you were trying to accomplish. If you couldn't
answer there any better than you have answered here, I'd suggest alternative
heirarchies for the solution. In other words, I'd ask you not to write that
code.
Are you saying that it is bad practice to call a MotorCycle
a Vehicle.
No. If you want to encapsulate vehicles, it is perfectly appropriate to
create a vehicle base class and create child classes for 'automobile' and
'motorcycle'. However, you then proceed to add methods to each of the child
classes that are mutually exclusive. That indicates that the calling code
would need to be tightly coupled to the concrete type rather than to the
abstraction, in order to use the methods. That is bad practice.
I don't see how you can say such a thing, without knowing the
solution that it is being used for. The example given is a very common
"problem"
The example is a code snippet. If you have a common problem that you wish
to be answered by a pattern, try to frame your problem in the following
terms: "I have a situation where X is in common and Y varies. I want to
hide the variation from code that needs only know about X. How do I do it?"
If your 'X' is a data-only object, you have implemented a DTO pattern (Data
Transport Object). If your X is an algorithm that can be applied by the
calling code (good example: bubble sort vs. QuickSort) without knowing the
details of the algorithm, then you have a strategy pattern. If your X is a
sequence of related conditions that need to be evaluated in order, then you
may have a decorator or a chain of responsibility pattern.
The key to framing your problem in terms of commonality and variability is
that you are performing a very basic and extremely powerful OOA technique
called, surprisingly, Commonality-Variability Analysis or CVA. See this
article for more details on this powerful technique. This article is a
chapter in a book that I highly recommend called Design Patterns Explained
by Shalloway and Trott:
http://www.netobjectives.com/ezines/ez0407NetObj_Commonality_and_Variability_Analysis.pdf
so there definitely is a name for this pattern,
I am not as familiar with the names of anti-patterns as I am with patterns,
so if there is a name, I cannot recall it off the top of my head. ;-)
I'm doing my best to be respectful of you and your question. I know that
you mean well. However, as far as I can tell, and I've taught a few courses
on OO Design Patterns over the years, the snippet that you presented does
not represent a good practice, and therefore does not appear in the canon of
design patterns. If I am wrong, I invite anyone to submit the name and
description of a design pattern (or link to one) that is adequately
described by the OP's code snippet in this thread.
Tom, perhaps I can offer some more detailed information.
The following sites are good for introducing code-specific bits of design
patterns
http://www.dofactory.com/Patterns/Patterns.aspx
http://home.earthlink.net/~huston2/dp/patterns.html
In addition to the book mentioned above, another very good book on design
patterns is Head First Design Patterns
http://www.oreilly.com/catalog/hfdesignpat/
I hope that this begins a journey for you. I mean that in the nicest and
most generous way possible. Honest. Reading about, then learning, then
studying, and then teaching design patterns changed my career completely. I
hope that you find design patterns to be as powerful as I have.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--