I’ve got a library written in C++ that provides a Virtual Machine (“VM”) of a graphical programming language. It uses an image (e.g. png file) as its source code and executes instructions. The VM is used in different types of applications (console, gui) so the aim for the lib is to be as flexible as it can be.
The question is: what should be the relation between the VM and the image object? Should it be aggregation (VM constructor takes existing Image instance as a parameter) or composition (VM constructor takes image file path as string parameter)?
Composition is where the outside (VM) object cannot exist without the inside object (image). Well, in this case a VM can exist, but it’s useless without any image (can’t perform no instructions if there is no code). So it should take the string parameter and create and destroy the image object on its own. But how about validating the string filepath (check if there’s no image there OR the file is not an image OR the image binary is corrupted) – where should this validation be implemented? The VM constructor, I guess?
On the second hand, if the VM could replace one image with another, it could be aggregation (more like Java Virtual Machine). But I’m not sure if it’s a good approach – VM would have to reset all image pointers and data stack bound to the image. Maybe one VM per one image is a cleaner and more logical solution.
Would you choose aggregation or composition in this case? Thanks for your answers.
Here’s steps to decide between aggregation and composition:
Note that handling several objects (with aggregation) is quite difficult. Usually requires something like: