471,354 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Best way to model object inheritance?

Howdy all!

I'm working with XML schemas for the first time, so please be gentle!

I am working on a graphics program (in Java if it matters), and I have
decided to use XML files to store the "objects" that make up a drawing.

A subset of the class hierarchy might look like:

+-------------------------+
| |
| skitch.objects.Object |
| |
+------------+------------+
|
+---------------+---------------+
| |
+------------+------------+ +------------+------------+
| | | |
| skitch.objects.Line | | skitch.objects.Square |
| | | |
+-------------------------+ +-------------------------+

I could create a schema that understands lines, squares, and all the
other shapes, but I want the program to be easily extensible and to use
a more dynamic "plug-in" mechanism.

My current solution uses two schemas for each object, a "generic"
schema (object.xsd):

<?xml version="1.0" encoding="US-ASCII"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">

<xs:include schemaLocation="basicTypes.xsd"/>

<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData"/>
</xs:sequence>
<xs:attribute name="oid" type="sk:oidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>

Once a file has been validated with this schema, the program can
discover the appropriate Java class to instantiate for this object (the
"class" attribute) and the class-specific schema (the "schema"
attribute).

Here's an example of a "line" object:

<?xml version="1.0" encoding="US-ASCII"?>

<sk:object xmlns:sk="xmlns://Skitch" oid="00000000"
class="skitch.objects.Line" schema="lineObject.xsd">

<sk:objectData>
<sk:start x="0.0" y="0.0"/>
<sk:end x="100" y="100"/>
<sk:color red="0" green="0" blue="0" alpha="255"/>
<sk:stroke width="1.0" cap="CAP_BUTT" join="JOIN_MITER"
miterLimit="10.0" dashPhase="0.0">
<sk:dash>
<sk:segment length="1.0"/>
<sk:segment length="1.0"/>
</sk:dash>
</sk:stroke>
</sk:objectData>

</sk:object>

And here's its class-specific schema:

<?xml version="1.0" encoding="US-ASCII"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sk="xmlns://Skitch"
targetNamespace="xmlns://Skitch"
elementFormDefault="qualified">

<xs:include schemaLocation="basicTypes.xsd"/>

<xs:element name="object">
<xs:complexType>
<xs:sequence>
<xs:element name="objectData" type="sk:lineObjectType"/>
</xs:sequence>
<xs:attribute name="oid" type="sk:oidType" use="required"/>
<xs:attribute name="class" type="xs:string" use="required"/>
<xs:attribute name="schema" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>

(basicTypes.xsd can be found at
http://home.comcast.net/~i.pilcher/x...basicTypes.xsd.)

Things appears to be working so far, but validating every object twice
just seem inelegant.

Is there a better way?

Thanks!

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Jul 23 '05 #1
0 987

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by D Witherspoon | last post: by
3 posts views Thread by Stephen S M WONG | last post: by
1 post views Thread by Jing You | last post: by
122 posts views Thread by Edward Diener No Spam | last post: by
9 posts views Thread by Marc De Schrijver | last post: by
23 posts views Thread by tonytech08 | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.