October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Design Patterns: Composite

  • July 2, 2007
  • By Jeff Langr
  • Send Email »
  • More Articles »

Now that both Like and Equals implement the Criteria interface, I can drive the changes into the And class.

// AndTest.java
import static org.junit.Assert.*;
import org.junit.*;

public class AndTest {
   @Test
   public void and() {
      Criteria name = new Like(new StringColumn("name", 1), "Joe%");
      Criteria amount = new Equals(new NumericColumn("amount"), 5);
      And and = new And(name, amount);
      assertEquals("name like 'Joe%' and amount = 5", and.sql());
   }
}

// And.java
public class And {
   private Criteria left;
   private Criteria right;

   public And(Criteria name, Criteria amount) {
      this.left = name;
      this.right = amount;
   }

   public String sql() {
      return left.sqlString() + " and " + right.sqlString();
   }
}

So far, you haven't done anything that resembles a composite. Sometimes, the revelation is obvious; other times, it requires a bit more thinking outside the box. Here, if I think about SQL statements for just a few seconds, I realize that And is just another criteria that can be combined with other criteria. For example, I might want a where clause such as:

where (amount = 10 and name like 'Joe%') and department = 'Labor'

Here is the modified And class:

// AndTest.java:
import static org.junit.Assert.*;
import org.junit.*;

public class AndTest {
   @Test
   public void and() {
      Criteria name = new Like(new StringColumn("name", 1), "Joe%");
      Criteria amount = new Equals(new NumericColumn("amount"), 5);
      Criteria and = new And(name, amount);
      assertEquals("name like 'Joe%' and amount = 5", and.sqlString());
   }
}

// And.java:
public class And implements Criteria {
   private Criteria left;
   private Criteria right;

   public And(Criteria name, Criteria amount) {
      this.left = name;
      this.right = amount;
   }

   public String sqlString() {
      return left.sqlString() + " and " + right.sqlString();
   }
}

Now, I can combine like, equals, and and clauses at will.

The UML diagram shows how the implementation demonstrates use of the Composite design pattern. The key relationship is that the And class is a Criteria implementation, whereas at the same time objects of type And are composed of Criteria objects. Once client code has constructed an appropriate hierarchy of Criteria objects, it can ask the topmost object in the hierarchy for its sqlString in a single call.

Without using the composite design pattern, I can still code a solution, but it's one that would require either more method overloading or if statements, adding to the complexity of the system.


Figure 1: The composite pattern.

Reference

[Gamma] Gamma, E., et. al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional, 1995.

About the Author

Jeff Langr is a veteran software developer celebrating his 25th year of professional software development. He's authored two books and dozens of published articles on software development, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft.com.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel