BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Is this the right approach (singleton question) ?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Mark Smith
Guest





PostPosted: Sat Nov 08, 2003 6:21 pm    Post subject: Is this the right approach (singleton question) ? Reply with quote



As I develop my first "fully-oo" app (wow, what a difference) I am finding
myself creating singleton after singleton called, for example:

TWebManager - handles dispatches to website and connects the app with web
resources
TListManager - handles "lists" (customers, users, etc).
TeEventManager - manager for internal event model
TeExceptionManager - handles exceptions gracefully
TLicenseManager - handles software licensing (what parts are enabled, etc)

This seems to make sense, but I want to make sure I'm not just
"manager-happy".

Like I said, this is my first OO project, and while I've discovered the
power of singletons, are there better patterns for this? Maybe I'm using
singletons just because they are all I know.

The "code smell" I see - and maybe there are others I don't see - is the
inclusion of manager after manager in the uses clauses of my units. Should
I put all managers in one unit? Or should I create a "managers.pas" file
that contains lots of {$I WebMgr.pas} type statements? Or is this not
really a "code smell"?

Thanks in advance for any comments.

Best Regards,
Mark Smith


Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sun Nov 09, 2003 2:08 am    Post subject: Re: Is this the right approach (singleton question) ? Reply with quote



Mark Smith wrote:
Quote:
As I develop my first "fully-oo" app (wow, what a difference) I am
finding myself creating singleton after singleton called, for example:

TWebManager - handles dispatches to website and connects the app with
web resources
TListManager - handles "lists" (customers, users, etc).
TeEventManager - manager for internal event model
TeExceptionManager - handles exceptions gracefully
TLicenseManager - handles software licensing (what parts are enabled,
etc)

Like I said, this is my first OO project, and while I've discovered
the power of singletons, are there better patterns for this? Maybe
I'm using singletons just because they are all I know.

Well, I expect you know your Customer, Users, etc will not be singletons.
<g>

You would have to provide more detail on each of the above before anyone
could really give advice. Off-hand, I'm not sure what the List manager
really does for you, and I'm not sure the Web Manager should be a singleton,
the web is a big place with lots of different things that can be done - what
is a single instance of a single class going to do?

Quote:
The "code smell" I see - and maybe there are others I don't see - is
the inclusion of manager after manager in the uses clauses of my
units.

That's alright because in future projects you may want some and not others.

Quote:
Should I put all managers in one unit?

Absolutely not, they are not related - that they all "manage" something is
not at all sufficient to group them.

Quote:
Or should I create a
"managers.pas" file that contains lots of {$I WebMgr.pas} type
statements?

That would be even worse.

Quote:
Or is this not really a "code smell"?

Can't smell anything from here at this level of detail. <g>

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson



Back to top
Mark Smith
Guest





PostPosted: Sun Nov 09, 2003 3:34 am    Post subject: Re: Is this the right approach (singleton question) ? Reply with quote



Wayne,

Thanks for taking the time to respond. I'll try to clarify.

Quote:
Off-hand, I'm not sure what the List
manager really does for you

As an InstantObjects user, I have access to the objects (Customers,Users)
via the framework. However, when loading 1000-3000 customers (avg for my
users) it is very, very slow (8 min. to iterate the entire list of 1200 in
one test I did). So the list manager bypasses the framework and loads the
"list-type" objects into memory tables at the start of the user's session,
then when the user asks for the list it performs a SQL query to find objects
with a "LastUpdated" time-stamp that is later than when the list was last
loaded/refreshed. It then retrieves just those items and presents a list
to the user.

This results in speed increases on the order of thousands-fold.

So in a nutshell, the list manager makes sure "list-type" objects are kept
in memory tables and kept fresh, outside the scope of the IO framework.

I hope that was clear.

Quote:
and I'm not sure the Web Manager should
be a singleton, the web is a big place with lots of different things
that can be done - what is a single instance of a single class going
to do?

I guess it should be called a WebsiteGateway rather than a WebManager since
it really provides a single point of access for my app to connect with my
website. Functions such as WebManager.SurfTo( aURL : string ) are present
etc. as well as constants for the URL parameter such as URL_HOMEPAGE,
URL_COMMUNITY, URL_FAQ, etc.

I think it was the writing of this "manager" more than any other that
prompted my posting, because it seemed as if I were moving global variables
and functions into a singleton, which in itself feels like one BIG global
variable. So now with my "managers" I feel as if I have 5-10 massive
globals instead of 50-100 little ones. Better, but still globals. Maybe
there's just no way around that.

In my last version of this app I had a file called "eCommon.pas" that
contained hundreds of types, consts, functions, etc that many places in the
code needed to access. While the singletons clean this up by grouping
related items into classes called "managers" I still think they are just
globals with another name.

Quote:
The "code smell" I see - and maybe there are others I don't see - is
the inclusion of manager after manager in the uses clauses of my
units.

That's alright because in future projects you may want some and not
others.

Good point. I haven't thought past the scope of this project at the moment.

Quote:
Should I put all managers in one unit?

Absolutely not, they are not related - that they all "manage"
something is not at all sufficient to group them.

Thank you, that settles that. <g>

Quote:

Or should I create a
"managers.pas" file that contains lots of {$I WebMgr.pas} type
statements?

That would be even worse.

I thought so too.

Quote:
Can't smell anything from here at this level of detail. <g

Maybe I'm too cerebral and everything's actually OK. I'm just afraid I'm
missing something because I thought the "OO-way" avoided global variables.
;)

Quote:
You would have to provide more detail on each of
the above before anyone could really give advice.

The WebManager and ListManager are covered above. Here are the others:

The TEventManager maintains a list of TEventDispatchers. TEvents can be
fired out via the manager to all the dispatchers, which ultimately deliver
them to a HandleEvent() function. So my business objects, forms, etc. need
only to declare a private TEventDispatcher (which self-registers as a
Subject with the TEventManager Observer) and wire it to a HandleEvent()
function to receive and process system wide notifications when things
happen. So if a customer is edited in one form, and the object is saved, a
TeObjectChangedEvent fires, the TListManager catches it, and updates the
Customer List so the window the user happens to have open is properly
refreshed with the new information.

The TLicenseManager is called by various parts of the program to see if they
are "allowed" to run based on purchased licensing. When the program
launches the license is established in the singleton and then anyone can
call it. As I said before, one big global variable.
The TeExceptionManager hooks into Application.OnException for global
exception handling, again, it's an object doing the work of what could just
as easily be a few global functions isolated in a unit.

Again, thanks for your reply, I realize this is very long but I thought a
complete reply was in order.

Best Regards,
Mark Smith



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.