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 »

Moving on, I can start rapidly adding support for additional SQL where clause constructs. Like, for example, like:

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

public class LikeTest {
   @Test
   public void simple() {
      Like like = new Like(new StringColumn("name", 1), "Joe%");
      assertEquals("name like 'Joe%'", like.sql());
   }
}

// Like.java:
public class Like {
   private StringColumn column;
   private String value;

   public Like(StringColumn column, String value) {
      this.column = column;
      this.value = value;
   }

   public String sql() {
      return String.format("%s like %s",
         column.getName(), column.sqlValue(value));
   }

}

Right now, the And class supports joining only two Equals objects. I want to have And support Like objects as well, or any other conditional that I might dream up. I can extract a common interface that represents the ability of each to return an SQL representation:

public interface Criteria {
   String sqlString();
}

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

public class EqualsTest {
   @Test
   public void stringColumn() {
      Column column = new StringColumn("name", 10);
      Criteria criteria = new Equals(column, "joe");
      assertEquals("name = 'joe'", criteria.sqlString());
   }

   @Test
   public void numericColumn() {
      Column column = new NumericColumn("amount");
      Criteria criteria = new Equals(column, 5);
      assertEquals("amount = 5", criteria.sqlString());
   }
}


// Equals.java:
public class Equals implements Criteria {
   private Column column;
   private Object value;

   public Equals(Column column, Object value) {
      this.column = column;
      this.value = value;
   }

   public String sqlString() {
      return String.format("%s = %s",
         column.getName(), column.sqlValue(value));
   }
}

I can modify the Like class to implement the same Criteria interface. This requires me to do a bit of refactoring because I wasn't consistent with the method names.

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

public class LikeTest {
   @Test
   public void simple() {
      Criteria like = new Like(new StringColumn("name", 1), "Joe%");
      assertEquals("name like 'Joe%'", like.sqlString());
   }
}

// Like.java
public class Like implements Criteria {
   private StringColumn column;
   private String value;


   public Like(StringColumn column, String value) {
      this.column = column;
      this.value = value;
   }

   public String sqlString() {
      return String.format("%s like %s",
         column.getName(), column.sqlValue(value));
   }
}




Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel