top of page
Writer's pictureDibyojyoti Sanyal

What design & build increment means in OO design Principle ? How to apply it in code ?

Updated: Aug 29, 2021

This is one of the Object Oriented Design Principles that helps developers keep the code readable and understandable.


When you try to make the whole design and start implementation that will not work because either you will find your previous assumptions were incorrect or the requirement changed. Therefore, it is better to design in a way that the design can be extended and the same basic design building blocks can be reused in other parts of the code. While designing we also need to adhere to homogeneity, which means similar things should be designed and implemented in a similar way and orientation of similar parts of code should look like the same. This is how we can design and build incrementally, once small pieces at a time.


Example, when you need to implement an HTTP connection and a JDBC connection to a database. The actual code will be very different but you should design it in a way that the same design patterns can be used in both places, the high level method names and the class hierarchy and relation between objects should be similar in both implementations. Though implementation details and the objects itself will be different but the interaction and responsibilities divided in different classes and methods in high level should look similar. It makes the code easy to understand also, as human nature is such that it recognizes the known patterns easily.


A concrete example will clearly show the idea. Here an interface connectionConfig helps us create two specific connection configuration classes needed to create connection configuration for HTTP and JDBC. The interface provides guidance on how a configuration class has to be created. However the implementation for both of them could be totally different and can be implemented at different points of time.

public interface connectionConfig {
  ConnectionConfig setHost(String host);
  ConnectionConfig setPort(String port);
  ConnectionConfig setProtocol(String protocol);
  String build();
}
 
public class HttpConnectionConfig implements connectionConfig {
}
public class JdbcConnectionConfig implements connectionConfig {
}

We can take help of builder classes to create a HTTP and JDBC connection, each builder class dedicated for each connection type. This way we make sure that in both cases we use the same design pattern. It also makes sure that the code to create a HTTP connection and a JDBC connection looks similar.


The code to create HTTP connection can be like this

HttpClientBuilder  httpClientBuilder =  HttpClientBuilder.create();
    final  String  connectionConfig = HttpConnectionConfig.setHost(host)
.setPost(port)
.setProtocol(“HTTP”)
.build();
    httpClientBuilder.setDefaultConnectionString(connectionConfig);

The code to create JDBC connection can be like this

JdbcClientBuilder  jdbcClientBuilder =  JdbcClientBuilder.create();
    final  String  connectionConfig = JdbcConnectionConfig.setHost(host)
.setPost(port)
.setProtocol(“JDBC”)
.build();
    jdbcClientBuilder.setDefaultConnectionString(connectionConfig);

This example shows how we can apply the Object Oriented Design Principle “design and build incrementally” in Object Oriented Programming.


Komentarze


bottom of page