I have been searching for a solution to show data of class with three assumptions:Class Employee is not responsible for showing itselfClass Employee does not expose its data with gettersClasses responsible for showing data are immutableSo I created Employee class with its View interface:public final class Employee {  private String name;  private String salary;  public interface View {    View addData(String name, String value);    void show();  }  public void showOn(View view) {    view.addData(Name, name.toString())        .addData(Salary, salary.toString())        .show();  }}And two classes to show it to user. HTML:final class HtmlPage implements Employee.View {  private final String content;  public HtmlPage() {    content = ;  }  public HtmlPage(String str) {    content = str;  }  @Override  public Employee.View addData(String name, String value) {    String newData = \t<tr><td>;    newData += name;    newData += </td><td>;    newData += value;    newData += </td></tr>\n;    HtmlPage updated = new HtmlPage(content + newData);    return updated;  }  @Override  public void show() {    System.out.println(<table>\n + content + </table>);  }}and GUI:import javax.swing.*;import java.awt.*;class OutputWindow implements Employee.View {  private final JComponent panel;  public OutputWindow() {    this.panel = new JPanel();  }  public OutputWindow(JComponent panel) {    this.panel = panel;  }  @Override  public Employee.View addData(String name, String value) {    panel.add(new JLabel(name));    panel.add(new JLabel(value));    return this;  }  @Override  public void show() {    JFrame outputFrame = new JFrame(Okienko do wypisania danych);    outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    outputFrame.getContentPane().add(this.getJComponent());    outputFrame.pack();    outputFrame.show();  }  JComponent getJComponent() {    panel.setLayout(new GridLayout(3, 2));    return panel;  }}Here is the usage:public static void main(String[] args) {    Employee Piotr = new Employee();    OutputWindow outputWindow = new OutputWindow();    Piotr.showOn(outputWindow);    Employee Ewelina = new Employee();    HtmlPage htmlPage = new HtmlPage();    Ewelina.showOn(htmlPage);}
Showing object's data on GUI and HTML without accessors and mutable objects
java;object oriented;gui