简体   繁体   中英

Java XML design boolean flag and values

Here is what my current XML file looks like.

<book>
    <isWritable>true</isWritable>
    <writeDelay>4.0</writeDelay>

    <isReadable>true</isReadable>
    <readDelay>3.2</readDelay>

    <isDestroyed>false</isDestroyed>
</book>

I have :

3 boolean flags - isWritable, isReadable, isDestroyed.

2 values writeDelay, readDelay.

Conditions :

isWritable require writeDelay

isReadable require readDelay

You can be isDestroyed but not isWritable or isReadable

You can be isWriteable and isReadable

You can be isWritable or isReadable

Current implementaion:

Book.class has all of these has required fields. I'm looking to simplifying this.

If my book is readable but not writable. The XML file should only be:

<book>
    <isReadable>true</isReadable>
    <readDelay>3.2</readDelay>
</book>

Likewise if the book is destroyed. The XML should only be:

<book>
    <isDestroyed>true</isDestroyed>
</book>

Is there any existing design pattern that applies to my situation? I have looked at a few and am leaning towards polymorphism but wanted to reach out to see if there are any other patterns I have not yet considered.

Polymorphism : Here is my idea but to me, it seems a lot for just a boolean and value.

Class called bookDescription.

subclass: writeDescription, readDescription, writeAndReadDescription (subclass of writeDescription), destroyedDescription.

Any comments and suggestions will be appreciated. Thank you!

I would advise XML attributes. The issue is pretty common when examining Java vs XML, because elements tend to be much more concrete compared to attributes .

Here's a good read on the purpose of attributes: https://stackoverflow.com/a/420197/2958086

We're describing something about this book's properties, rather than the contents of the book itself.

In this case, you could possibly do:

<book isWritable="true" isReadable="true" writeDelay="4.0" readDelay="3.2" isDestroyed="false"/>

Then you can pull the attributes out real quick, and assume no attributes means false.

However, you've actually provided 5 pieces of data, but 2 pieces of data are dependent on 2 other pieces of data:

isWritable : writeDelay

isReadable : readDelay

Because you indicate writeDelay and readDelay are required based on the other fields, you can make this even more efficient:

<book writeDelay="4.0" readDelay="3.2" isDestroyed="false"/>

A read-only book:

<book readDelay="3.2"/>

A destroyed book:

<book isDestroyed="true"/>

You can then use the writeDelay and readDelay vars to handle the booleans. If they're not provided, you can assume that their associated value is false.

This allows your XML to be SAX-friendly as well, as all the vital attributes are provided in-line with the element itself, rather than necessarily be further down in parsing. You can easily validate from the singular node, rather than the node and its children, based on the properties.

The Java object as you've described it, however, is fine. You should keep all 5 of those values in there for clarity and actual programmatic use. XML is designed specifically in this case for storing the data, while your application interprets the stored data.

For this particular use case you could create a separate version of the Book class that is only used for the object/XML conversion. This class will have object data type properties (ie Boolean instead of boolean ) so that null values are not converted to XML. Then you can encapsulate all your logic into a converter class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM