Skip to Main Content
Java Enterprise Best Practices
book

Java Enterprise Best Practices

by O'Reilly Java Authors
December 2002
Intermediate to advanced content levelIntermediate to advanced
288 pages
9h 46m
English
O'Reilly Media, Inc.
Content preview from Java Enterprise Best Practices

Use Flat Hierarchies When Designing Value Objects

The first rule of thumb when designing a suite of value objects is this: avoid inheritance. There are two basic reasons for this. The first is efficiency. When you send an instance of a class over the wire, you also send information about the class hierarchy. If you run Example 6-1, you’ll see that in Java Development Kit (JDK) 1.4 the cost of one extra level of inheritance is 44 bytes (regardless of whether you use serialization or externalization).

Example 6-1. FlatHierarchies.java
public class FlatHierarchies { public static void main(String[] args) { try { System.out.println("An instance of A takes " + getSize(new A( ))); System.out.println("An instance of B takes " + getSize(new B( ))); System.out.println("An instance of C takes " + getSize(new C( ))); System.out.println("An instance of D takes " + getSize(new D( ))); } catch(Exception e) { e.printStackTrace( ); } } private static int getSize(Object arg) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( ); ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream); oos.writeObject(arg); oos.close( ); byte[] bytes = byteArrayOutputStream.toByteArray( ); return bytes.length; } protected static class A implements Serializable { } protected static class B extends A { } protected static class C implements Externalizable { public void readExternal(ObjectInput oi) {} public void writeExternal(ObjectOutput oo) {} } protected ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Moving to Java 9: Better Design and Simpler Code

Moving to Java 9: Better Design and Simpler Code

Trisha Gee
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau

Publisher Resources

ISBN: 0596003846Supplemental ContentErrata Page