title: Design pattern for bi directional data format conversion
body: <p>I work on a project that will deal with different data formats.</p>
  
  <p>for example, suppose i have the following data formats:</p>
  
  <pre><code>formatA, formatB, formatC, ...
  </code></pre>
  
  <p>with the following classes.</p>
  
  <p><strong>formatA</strong>:</p>
  
  <pre><code>public class dataFormatA {
  
      private String data;
  
      public dataFormatA(String data) {
          this.data = data;
          // convertData(this.data);
      }
  
      public dataFormatB convertToDataFormatB() {
          return new dataFormatB(data);
      }
  
      public dataFormatC convertToDataFormatC() {
          return new dataFormatC(data);
      }
  
  }
  </code></pre>
  
  <p><strong>formatB</strong>:</p>
  
  <pre><code>public class dataFormatB {
  
      private String data;
  
      public dataFormatB(String data) {
          this.data = data;
          // convertData(this.data);
      }
  
      public dataFormatA convertToDataFormatA() {
          return new dataFormatA(data);
      }
  
      public dataFormatC convertToDataFormatC() {
          return new dataFormatC(data);
      }
  
  }
  </code></pre>
  
  <p><strong>formatC</strong>:</p>
  
  <pre><code>public class dataFormatC {
  
      private String data;
  
      public dataFormatC(String data) {
          this.data = data;
          // convertData(this.data);
      }
  
      public dataFormatA convertToDataFormatA() {
          return new dataFormatA(data);
      }
  
      public dataFormatB convertToDataFormatB() {
          return new dataFormatB(data);
      }
  }
  </code></pre>
  
  <p>What is the best design pattern to make? currently, each class has a method to convert its data type to the rest of data formats classes. But this is not a good design as it violat the <strong>open close principle</strong> </p>