[Tutor] Another OO modeling question

Alan Gauld alan.gauld at btinternet.com
Tue Dec 22 19:31:26 EST 2015


On 22/12/15 23:47, jamie hu wrote:
> Thanks for the reply Alan. If init code calls launch method then I
> would have problems in looking up objects afterwards.

In that case use the init to do the config then explicitly call the launch.

> For example:
> 1. First or initial calls to VirtualMachine(memory, vcpu, network,
> base_image) will try to launch a VM and return a vmid string.
> 2. Now if I need to terminate all VMs with a specific base_image then
> I will use VirtualMachine.find_by_baseimage() method. This needs to
> return VirtualMachine objects with matching base image.

This is the same as your previous question, so I'd store the VMs in a
dictionary keyed by vmid. I'd put the dictionary in the class (not the
instance)
as an attribute. And i'd make the find method a class method.

> 3. If this data was stored in a database then, before returning this
> data I will need to call init method again.

Yes, but would you really be running so many virtual machines that you'd
store it in a database? Maybe if you run a very large data center you would
but that seems an unlikely use for a database. But if you wanted to and
didn't want to start an identical VM then you definitely need to keep
the init
for config and have a separate launch() method.

See previous mails for how to instantiate an existing object from the
database.

> As init is calling launch method, wouldn't this trigger VM launch again?
> Or am I missing something here?

No, it's just that you've now included several extra requirements that
weren't in the original spec.  The requirements largely determine the
design.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list