Business Object Implementation Patterns

General Pattern

The standard pattern is very simple. It can be applied to simple Java types and most of complex classes. However, do not use this pattern for other Business Objects and collections.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Interface pattern
public interface MyInterface extends IGeneralBO<...> {
 
   /** Always define the property name as static constant */
   public static final String MY_PROPERTY = "myProperty";
 
   /** The Getter */
   public String getMyProperty();
 
   /** The Setter */
   public void setMyProperty(String s);
}
 
// Implementation pattern
public class MyInterfaceImpl extends AbstractHibernateBO<...> implements MyInterface {
 
   /** The Getter */
   public String getMyProperty() {
      return getTransferObject().getMyProperty();
   }
 
   /** The Setter */
   public void setMyProperty(String s) {
      String oldValue = getMyProperty();
      getTransferObject().setMyProperty(s);
      firePropertyChange(MY_PROPERTY, oldValue, s);
   }
}

A note about simple Java types: You can either user the simple Java type or the corresponding Java object (e.g. Integer instead of int). However, you must use the Java object when your property can be potentially null or unset in your database.

Pattern for Business Objects

The following pattern shall be used when your property is a reference to another Business Object, either of the same or a different type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Interface pattern
public interface MyInterface extends IGeneralBO<...> {
 
   /** Always define the property name as static constant */
   public static final String ANOTHER_PROPERTY = "anotherProperty";
 
   /** The Getter */
   public AnotherInterface getAnotherProperty();
 
   /** The Setter */
   public void setAnotherProperty(AnotherInterface other);
}
 
// Implementation pattern
public class MyInterfaceImpl extends AbstractHibernateBO<...> implements MyInterface {
 
   /** The Getter */
   public AnotherInterface getAnotherProperty() {
      return getBusinessObject(getTransferObject().getAnotherProperty());
   }
 
   /** The Setter */
   public void setAnotherProperty(AnotherInterface other) {
      AnotherInterface oldValue = getAnotherProperty();
      getTransferObject().setAnotherProperty(((AnotherInterfaceImpl)other).getTransferObject());
      firePropertyChange(ANOTHER_PROPERTY, oldValue, other);
   }
}

The challenge here is to convert the Transfer Object into an appropriate Business Object (for getter) and vice versa (for setter). The getBusinessObject() method already does that for you (Line 19). The setter needs to cast the interface argument (other) in order to query for its Transfer Object (Line 25).

Pattern for Collections (Part 1)

Sometimes you might want to have collections of objects as your property. A common use case is the access to a list of other Business Objects. Although Hibernate offers to directly load such objects as associated collections, you should be careful with this feature. However, here is the pattern for it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Interface pattern
public interface MyInterface extends IGeneralBO<...> {
 
   /** Always define the property name as static constant */
   public static final String MY_COLLECTION = "myCollection";
 
   /** The Getter */
   public Collection<AnotherInterface> getMyCollection();
 
   /** The Setter */
   public void setMyCollection(Collection<AnotherInterface> collection);
 
   /** Add method */
   public void addMyCollection(AnotherInterface other);
 
   /** Remove method */
   public void removeMyCollection(AnotherInterface other);
 
}
 
// Implementation pattern
public class MyInterfaceImpl extends AbstractHibernateBO<...> implements MyInterface {
 
   /** The Getter */
   public Collection<AnotherInterface> getMyCollection() {
      return getBusinessObject(getTransferObject().getMyCollection());
   }
 
   /** The Setter */
   public void setMyCollection(Collection<AnotherInterface> collection) {
      Collection<AnotherInterface> oldValue = getMyCollection();
      getTransferObject().setMyCollection(getTransferObjects(collection));
      firePropertyChange(MY_COLLECTION, oldValue, collection);
   }
 
   /** Add method */
   public void addMyCollection(AnotherInterface other) {
      getTransferObject().getMyCollection().add(((AnotherInterfaceImpl)other).getTransferObject());
      firePropertyChange(MY_COLLECTION, null, other);
 
   }
 
   /** Remove method */
   public void removeMyCollection(AnotherInterface other) {
      getTransferObject().getMyCollection().remove(((AnotherInterfaceImpl)other).getTransferObject());
      firePropertyChange(MY_COLLECTION, other, null);
   }
 
}

Pattern for Collections (Part 2)

A better approach is to define a search method in your associated DAO and do not declare any setter or modification methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Define search method
public interface AnotherInterfaceDAO extends IGeneralDAO<...> {
   /** The search method (argument could be a key only) */
   public Collection<AnotherInterface> findBy(MyInterface myInterface);
}
 
// Interface pattern
public interface MyInterface extends IGeneralBO<...> {
 
   /** Always define the property name as static constant */
   public static final String MY_COLLECTION = "myCollection";
 
   /** The Getter */
   @Transient
   public Collection<AnotherInterface> getMyCollection();
}
 
// Implementation pattern
public class MyInterfaceImpl extends AbstractHibernateBO<...> implements MyInterface {
 
   /** The Getter */
   public Collection<AnotherInterface> getMyCollection() {
      return getFactory().getDao(AnotherInterfaceDAO.class).findBy(this);
   }
 
}

Pattern for Collections (Part 3)

Finally, a pattern for collections that are not associated Business Objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Interface pattern
public interface MyInterface extends IGeneralBO<...> {
 
   /** Always define the property name as static constant */
   public static final String MY_COLLECTION = "myCollection";
 
   /** The Getter */
   public Collection<String> getMyCollection();
 
   /** The Setter */
   public void setMyCollection(Collection<String> collection);
 
   /** Add method */
   public void addMyCollection(String other);
 
   /** Remove method */
   public void removeMyCollection(String other);
 
}
 
// Implementation pattern
public class MyInterfaceImpl extends AbstractHibernateBO<...> implements MyInterface {
 
   /** The Getter */
   public Collection<String> getMyCollection() {
      return getTransferObject().getMyCollection();
   }
 
   /** The Setter */
   public void setMyCollection(Collection<String> collection) {
      Collection<String> oldValue = getMyCollection();
      getTransferObject().setMyCollection(collection);
      firePropertyChange(MY_COLLECTION, oldValue, collection);
   }
 
   /** Add method */
   public void addMyCollection(String other) {
      getTransferObject().getMyCollection().add(other);
      firePropertyChange(MY_COLLECTION, null, other);
 
   }
 
   /** Remove method */
   public void removeMyCollection(String other) {
      getTransferObject().getMyCollection().remove(other);
      firePropertyChange(MY_COLLECTION, other, null);
   }
 
}

One Response to “Business Object Implementation Patterns”

  1. Buy Steroids USA Says:

    […] with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American […]

Leave a Reply