Saving code at the same level when working with static methods

This may be a little subjective, but I would like to receive your contribution to my current situation. I have a class that will be used to serialize / deserialize an object.

public class MyClass
{
    public static string ToXmlString( MyClass c ) { /*...*/ }
    public static MyClass FromXmlString( string xml ) { /*...*/ }
}

I only like this approach because it supports two functions at the same level. However, my goal is to avoid using static methods (whenever possible). It also looks like I can be vilolating SRP, but the main purpose of this object is that it can be serialized / deserialized from an xml string.

Any thoughts on using static methods in this situation? Should I just do ToXmlStringnon-static, but leave the statics FromXmlString? Should I create a new class that will only handle MyClass serialization?

EDIT:

The class that I am discussing here is a simple transfer object. It is used to save / restore values ​​from the thrid party tool.

Thank!

+3
source share
6 answers

FWIW I think serialization is a problem that needs to be separated from the rest of your class, especially if your class is a business type.

, .

, ?

(SaveToDB, LoadFromDB, ToBinaryStream, FromBinaryStream...), , (, ).

+2

# Java , To__ , From__ ( ). : ToString() - .

+1

(XML ), serialize/deserialize .

MyClass "writeObject" "readObject", . Sun .

" ", . .

PS: , WML, XStream API.

0

Benoit, , ( ):

// : c12:SerialCtl.java
// Controlling serialization by adding your own
// writeObject() and readObject() methods.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerialCtl implements Serializable {
  private String a;

  private transient String b;

  public SerialCtl(String aa, String bb) {
    a = "Not Transient: " + aa;
    b = "Transient: " + bb;
  }

  public String toString() {
    return a + "\n" + b;
  }

  private void writeObject(ObjectOutputStream stream) throws IOException {
    stream.defaultWriteObject();
    stream.writeObject(b);
  }

  private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    b = (String) stream.readObject();
  }

  public static void main(String[] args) throws IOException,
      ClassNotFoundException {
    SerialCtl sc = new SerialCtl("Test1", "Test2");
    System.out.println("Before:\n" + sc);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(buf);
    o.writeObject(sc);
    // Now get it back:
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
        buf.toByteArray()));
    SerialCtl sc2 = (SerialCtl) in.readObject();
    System.out.println("After:\n" + sc2);
  }
}

, .

0

, , vs. , Framework (String.Split/Join, ).

, , , - . , , - , . , , , .

, , , . , , , , , . .

ToXmlString , , . , , . , .

0
source

You can define a constructor that accepts an XMLReader (or a string if you really insist). The main advantage of this is that it allows you to have stronger invariants in your class and to be explicit with respect to any constant members with readonly.

0
source

Source: https://habr.com/ru/post/1759920/


All Articles