Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: "cleaning out" object data

by brian_d_foy (Abbot)
on May 21, 2005 at 02:07 UTC ( #459179=note: print w/replies, xml ) Need Help??

in reply to "cleaning out" object data

I often set up objects with an init() or reset() methods. I use the new() simply to create the object, and then let it call init() where everything gets populated, assigned, and so on. In that case, I would just call init() again to get the object back to its pristine state. You might have to include some code to make sure it only has the stuff you want (and nothing more).

In your example, I would think you would want a new Guard object for each new guard on duty. You can still store the current Guard in the same variable in the main script, but once each Guard instance has its own object you can do things with the Guard coming off shift and the Guard going on shift. You can freeze the off-duty Guard object and thaw it out when it's time for him to work again. You could even keep an array of Guards around and rotate through them.

Instead of exposing a Guard object to the main script though, what you really want is a Security object. Like you said, the Celebrity does not care about the Guard, only the Security. The Security object can handle shift changes and so on. As you rotate through the Guards, you can ask the Security object things like "Who's on duty?", but also, "Who was last on duty?" and "What did he remember"? You can't do that sort of thing if you wipe out the Guard's memory at every shift change.

brian d foy <>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://459179]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-10-22 16:03 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!