Mihail Stoynov's blog!
Surrender your ego
Thursday, February 21, 2008
Transformations between Domain Models, OOP fails, no such Design Pattern
The issue:
There are 3 (three) Domain Models (Object hierarchies, groups of objects). They are parts of different tiers of an application. Their hierarchy trees are nearly the same.
Why we have three DM is another issue, but is not in the focus of the following discussion.
The task is to convert objects from one Domain Model to another. As I said, the objects are nearly the same, data objects, no logic.
Until now there were static methods that do that. It's ugly - there's another class, inheritance is impossible - logic has to be extracted like this
public static M2Object convertM1toM2(M1Object) { /* logic */ }
becomes
public static M2Object convertM1toM2(M1Object m1) { convertM1toM2(m1, result); }
private static void convertM1toM2(M1Object m1, M2Object m2) { /* logic */ }
because the result is now an inheriting class and cannot reuse
M2Object convertM1toM2(M1Object)
, but can reuse
void convertM1toM2(M1Object m1, M2Object m2)
.
What I proposed was to put converters in one of the Domain Models, M2 was chosen.
so now M2 has
new M2Object(M1Object)
new M2Object(M3Object)
M1Object toM1()
M3Object toM3()
Inheritance with constructors is working smoothly. The problem comes from the
toXXX()
methods. Inheritance is difficult because the super
toXXX()
creates the result (base M1/M3 class), but the result in inheriting
toXXX()
method should be inherited M1/M3 class.
So now I'm thinking of (
M2InheritedObject
):
protected final void convertTo(M1InheritedObject m1) {
super.convertTo(m1); // convert base class properties (
reuse happens here
)
/* +extra logic */ // convert inherited class properties
}
public M1InheritedObject toM1() {
M1InheritedObject
result = new
M1InheritedObject
();
this.convertTo(result);
return result;
}
This method is gonna be reused, inheritance is weird - this is actually kinda unclean, but the best I can think of.
I'm thinking of some design pattern, (Adapter for example), but that good for objects, not object hierarchies.
If somebody thinks of something, I'd appreciate it.
Comments [3]
|
Trackback
Wednesday, February 27, 2008 2:57:08 PM UTC
Just test
Test
Wednesday, February 27, 2008 3:05:03 PM UTC
Test with Firefox
Test with Firefox
Wednesday, February 27, 2008 3:11:34 PM UTC
Works, hah?
mihail.stoynov
OpenID
Please login with either your
OpenID
above, or your details below.
Name
E-mail
(will show your
gravatar
icon)
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, blockquote@cite, strike
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
dasBlog theme by
Mads Kristensen
RSS feed
Search
Archives
December, 2008 (0)
November, 2008 (15)
October, 2008 (16)
September, 2008 (30)
August, 2008 (15)
July, 2008 (14)
June, 2008 (26)
May, 2008 (6)
April, 2008 (21)
March, 2008 (14)
February, 2008 (28)
November, 2007 (5)
October, 2007 (7)
September, 2007 (1)
August, 2007 (7)
July, 2007 (3)
June, 2007 (1)
Blog Stats
Total Posts: 203
This Year: 179
This Month: 0
This Week: 0
Comments: 82
Categories
Did you know
Java
rulez
Sucks
БГ
Blogroll
Michael Moore (no rss)
Links
BG-JUG
Copyright policy
No rights reserved.
(You are going to
copy stuff anyway :)
If you mention my
name, thank you.
2008, Mihail Stoynov