Design Question

Z

Zach Dennis

I am having a small dillema. In a program I and a friend are writing I am
proposing to have a Singleton Error/Status object that is updated with
error/status information. For example (this code is nont tested and it is
just a conceptual look):


class StatusSingleton
def setStatus( returnCode , message , obj=nil )
@returnCode = returnCode
@message = message
@obj = obj
end

def getLastReturnCode
return @returnCode
end

def getLastMessage
return @message
end
end

class MyObj
def setTrue
#A Big Error Occurs
StatusSingleton.setStatus( "0x0080" , "Could not set true" );
end
end

m = MyObj.new();
if( !m.setTrue() ) then
MyTkTextFieldWidget.setText( StatusSingleton.getLastMessage() )
end



Is my StatusSinglton overkill? I am trying to consolidate error information
into one class. What do you think?

Thanks,

Zach
 
D

David Alan Black

Hello --

Zach Dennis said:
I am having a small dillema. In a program I and a friend are writing I am
proposing to have a Singleton Error/Status object that is updated with
error/status information. For example (this code is nont tested and it is
just a conceptual look):


class StatusSingleton
def setStatus( returnCode , message , obj=nil )
@returnCode = returnCode
@message = message
@obj = obj
end

def getLastReturnCode
return @returnCode
end

A quick hint: rather than use methods that start with "get" and "set"
and just return instance variables, use the attr_* family of methods
instead.
def getLastMessage
return @message
end
end

class MyObj
def setTrue
#A Big Error Occurs
StatusSingleton.setStatus( "0x0080" , "Could not set true" );
end
end

m = MyObj.new();
if( !m.setTrue() ) then
MyTkTextFieldWidget.setText( StatusSingleton.getLastMessage() )
end



Is my StatusSinglton overkill? I am trying to consolidate error information
into one class. What do you think?

Another idea might be just to create your own Exception class:

class SomeException < Exception; end

and then raise and rescue accordingly:

begin
m = MyObject.new
# do stuff that raises a SomeException on error
rescue SomeException => e
MyTkTextFieldWidget.set_text(e.message)
end

But there may be something that this doesn't give you that I'm not
taking into account.


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top