Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Question about "data hiding" OOP paradigm issue
- From: Cedric.Beust@xxxxxxx (Cedric Beust)
- Subject: Question about "data hiding" OOP paradigm issue
- Date: Thu, 30 Mar 2000 09:45:49 -0800
> From: Sordini Emmanuele [mailto:Sordini.Emmanuele@xxxxxxxxxxxxxx] > 1) When I call mc.doSomething() the state of mc is changed, and so is > the state of theObject. Or am I wrong? Feeding the setter with a reference > to mc is quite performance-effective, but clearly breaks OOP visibility > rules. > 2) If, on the other hand, I passed the setter a copy of mc, that would > ensure respecting the rule but would generate some memory and performance > overhead, which is not advisable given Java's generally poor scores. > > Anybody have any opinions / hints? It's impossible to give a general recommendation, it really depends on the semantics of your class. You correctly identified the trade-offs : - pass by reference : efficient, but possibility for the object to be modified with side-effects - pass by value (clone) : no side-effects, but probably unnecessarily slow The typical way to deal with this problem is the Immutable idiom. Break your class in two parts, one being read-only and which contains exclusively "const member functions" to use C++ terminology : class ConstMyClass { public Object getValue() { ...} }; class MyClass extends ConstMyClass { public setValue(Object o) { ... } } Then, just pick the appropriate type in your method's signatures. Does this answer your question ? -- Cedric http://beust.com/cedric --- To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx
- References:
- Question about "data hiding" OOP paradigm issue
- From: Sordini Emmanuele
- Question about "data hiding" OOP paradigm issue
- Prev by Date: JDBC-ODBC bridge and prepared statements
- Next by Date: [Fwd: A Peculiar Problem with the HTTP Get method :- JSP]
- Previous by thread: Question about "data hiding" OOP paradigm issue
- Next by thread: Question about "data hiding" OOP paradigm issue
- Index(es):