dimanche 5 juillet 2020

Design pattern for building data model through different levels

I have a data model that has several attributes

class MyData {
    public string attr1;
    public string attr2;
    public string attr3;
}

These attributes values are not assigned at a common place, instead, this data model is passed around in several functions where values are assigned. For example,

public void method1(final MyData data) {
    ...
    data.attr1 = "val1";
    ...
    method2(data);
}

public void method2(final MyData data) {
    ...
    data.attr2 = "val2";
    ...
    method3(data);
}

public void method3(final MyData data) {
    ...
    data.attr3 = "val3";
    ...
    finalProcess(data);
}

My question is, what is the best practice of building data model in this case? People say data immutable guarantees thread safety, if the attributes can be only set once, would that serve the purpose? Is there any non-hacky way to do this?

Aucun commentaire:

Enregistrer un commentaire