In this context, design symmetry means that if one method causes an effect, the logically opposite method should cause the opposite effect. This can be a subtle point, but the results may be very significant.
For instance, take the following case. You have a type, ConversionService, that implements the popular life cycle pattern.
public interface LifeCycle {
void init();
void start();
void stop();
void destroy();
}
... public class ConversionService implements LifeCycle {
private Connection conn;
...
public void init() {
conn = new Connection(getServerConfig());
conn.open();
}
public void start() { ... }
public void stop() { ... }
public void destroy() {
conn.close();
conn = null;
} }
In the init() method of ConversionService, a Connection object is created and opened. During the life of the service, the Connection object is used over and over again. Then, when the service is being permanently shut down, the destroy() method is called.
Since the destroy() method is logically opposite of the init() method, this is where the Connection should be destroyed. Another equally valid option is to create the Connection in the start() method and destroy it in the stop() method. You should choose the option you want to use based on the desired effect. The wrong choice would be to create the Connection in the init() method and dispose of it in the stop() method.
By composing your classes so that the diametric methods have diametric effects, you'll make your classes easier to use and prevent unexpected behaviour. The more mundane your objects are, the better.




Leave a comment