Based on some questions I asked (1, 2,3), I am frequently suggested to separate data from functions (because of changes in function, single responsibility, separation of concerns, adopting interfaces, immutability, ...).
Then my question is: In which situation functions should be encapsulated with data? what type of functions are they? Is representing an entity or a concept a single responsibility? if yes then I may conclude no function should be introduced in a class which represents an entity! Did I miss something?
According to the following approaches, what happens if I always follow the second approach! Please note in the second approach class Entity
has no private state variable as it has no function, then its fields are accessible in class EntityOp1
and EntityOp23
class Entity
{
public data;
private s1, s2, s3;
operation1;
operation2;
operation3;
}
vs.
class Entity
{
public data;
}
class EntityOp1
{
Entity e;
private s1;
operation1;
}
class EntityOp23
{
Entity e;
private s2, s3;
operation2;
operation3;
}