Suppose you want to display data contained in an array or collection in a table-like format, i.e., in rows and columns. You want to be able to choose which attributes to display. In addition, you want to only display a subset of the data that meet certain conditions, and you want to sort that data based on numerous sorting criteria and define a preference order for those criteria.
This is the type of functionality that SQL SELECT provides: In a SQL SELECT statement, you can define your datasource (the FROM clause); you define which attributes (columns) you'd like to see (the SELECT clause); you can add conditions (the WHERE clause); and you can define the order in which the data is presented (the ORDER BY clause).
This article describes a number of classes and interfaces that allow you to apply the same functionality to an array or a collection of objects of any type. To achieve this functionality, the mechanism described uses some design patterns.
The approach presented here has the following benefits:
SELECT clause separately, allowing you to produce neat, reusable, and extendable code
However, as you might guess, this mechanism employs generic interfaces that can be abused when misunderstood (more on that later).
The following list gives a short description of the classes and interfaces we need. Later you will see how they all work together to achieve the desired functionality. Each class and interface is quite simple, consisting of one or two methods.
Invoker defines only one method: public Object invoke(Object o). It allows you to wrap a method call in an interface and return the method's result. Later, you will see how it can be used.
Condition also defines one method: public boolean passes(Object o). It allows you to check any kind of object for any kind of condition and return either true or false. Again, you will soon
learn how to use this interface.
SelectStatement represents a complete SELECT statement containing a SELECT, FROM, WHERE, and ORDER BY clause.
SelectInstruction represents a single part of a SELECT clause, namely a column and a column name. An entire SELECT clause is represented by an array of SelectInstruction objects.
OrderInstruction represents a single part of an ORDER BY clause. An entire ORDER BY clause is represented by an array of OrderInstruction objects.Executor executes the SelectStatement, which contains the entire execution logic.
ResultTable represents the data returned by executing the SelectStatement. It contains the result data in the form of an Object[][], allowing you to easily traverse and display the data.
Now, let's see how all these classes and interfaces work together. This class diagram illustrates how they relate to each other.
| Subject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select email id from database and send email to those id's using yahooBy Anonymous on July 8, 2009, 10:12 pmI have written a program that can insert and remove an information about a person..but now i also want my program to become capable of selecting and adding specific...
Reply | Read entire comment
View all comments