title: How should I separate the view of a domain from the domain model itself in java?
body: <p>How can I decouple how (or even if) a domain model is viewed from the implementation of the view itself. As an example consider the following code:</p>
  
  <pre><code>public class Student
  {
      private Set&lt;Course&gt; enrollment;
      public void enroll(Course c){
          // domain implementation
      }
      public void unenroll(Course c) {
         // domain implementation
      }
  
  }
  </code></pre>
  
  <p>This is a pretty simple example, but it's fine for what I'm asking. What would be a good way of ensuring that the view of this object is decoupled from the actual domain functionality? For example this student should be able to be represented in XML, JSON, or even some sort of game, or not have a view of any kind. </p>
  
  <p>Some of the approaches I've thought of could be something like:</p>
  
  <pre><code>@XmlRootElement(name="student")
  public class StudentXmlView implements XmlView&lt;Student&gt; {
  
      @XmlElement(name="enrollment")  
      private XmlVeiw&lt;Set&lt;Course&gt;&gt; enrollmentView;
  }
  </code></pre>
  
  <p>Would this sort of approach be suitable, or is there some best practice that is typically used in java for separating domain from it's view?</p>