Re: Question concerning object-oriented programming



spinoza1111 wrote:
Whereas I believe that all non-stateless objects should have a Usable
property and an inspect() method. If at the end of the constructor the
inspect() of the state fails to verify key assertions, actual objects
should mark themselves Unusable and thereafter all procedures should
take an early exit, and properties and methods returning values should
return defaults such as zero, a null string, or null.

As one other person has said, one way of handling this is by throwing
exceptions. You can throw an exception from a constructor, in which
case the language will ensure that the object does not even exist
anymore (in which case, methods can't be called on it). But you
can also throw exceptions from methods. Exceptions have a way of
making the caller stop what it's erroneously doing, especially if
the caller doesn't handle them. :-) And even if it does, it has
the knowledge that something is wrong, so if it persists afterwards,
it's just being recalcitrant.

Theory. Any Object should have a toString() and a palindromic
fromString() which restores the object state from the toString, which
would, in this scenario, create a readable but complete state, perhaps
in XML.

This depends on what you think the purpose of toString() is.
Namely, is it designed to save the state of the object, or is
it designed to provide information to the user? I usually
assume it's the latter. For example, if I have an object
representing a network session and it disconnects prematurely,
I might write a message to the log describing that event, and
I'll probably including the output of the network session's
toString() in there. My aim is just to allow the operator
to know who disconnected, not to ever be able to reconstruct
anything. As such, my toString() may not contain all the
information about the session. It might just contain an IP
address, even though the session holds some other state
information that is an implementation detail. In other words,
my toString() is lossy. And to me, that's OK. It's useful
to have the most relevant information handy in the logs, and
if there were a requirement to put all the information in
there, it would be a burden and it would create clutter
that makes the logs harder to read.

However, some OO languages do support serialization. Java,
for example, allows a class to implement the interface called
java.io.Serializable, and if desired it can also implement
some methods called writeObject(...) and readObject(...).
These don't work with strings; instead, they work with
streams, but the effect is the same, especially since in
Java (and in a lot of languages now) you can create a
stream that is secretly backed by a string, so that anything
which knows how to read/write a stream can be "tricked"
into reading/writing a string.

- Logan
.



Relevant Pages

  • Re: System.currentTimeMillis not returning GMT/UTC (J2me)
    ... You said the "constructor returns", twice, I'm sorry, I had assumed that meant ... Date.toString() may return a local String or it may not, ... like all objects it happens to have a toString() method. ... Such don't exist in the MIDP edition. ...
    (comp.lang.java.programmer)
  • Re: VB.NET Overloading problem/question
    ... It seems you are trying to make ToString do too much. ... Public Overridable Property Subject As String ... BodyMessage & SubjectBodyMessage would then simply need to add ... Public Overrides Readonly Property HasBody As Boolean ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Control = StrConv([Control], 3)
    ... Scott ... >> Fred- I think a list of exceptions would be best, I can only see a handful ... > Function ConvExceptionsInField(StrIn As String) As String ... > Dim strWord As String ...
    (microsoft.public.access.formscoding)
  • Re: Java 7 features
    ... indeed, get rid of checked exceptions, by making ... String foo = whatever; ... The example above explicitly initializes the String foo. ...
    (comp.lang.java.programmer)
  • Cant understand why these exceptions occur...
    ... If the command is "buffer", ... but I get exceptions if the buffer file does ... procedure LoadCache(Const cacheName: String); forward; ... if (idx < Length(files)) then ...
    (alt.comp.lang.borland-delphi)