I am currently working on a project that generates videos from templates (a template being a collection of JSON files and assets).
It seems natural to have a Template
class that contains all the info of how this specific template is structured (e.g. which parts are personaliseable, what assets are required etc...).
The issue I have been hung up on is how to model a job. A job takes a particular Template
, allows assigning values to the personaliseable fields and renders a video using these values. To me, it makes sense that a Job would be an instance of an instance of Template
.
A solution I am imagining is something like this (in pseudo-Typescript):
class Template {
// elements in groups can be subclasses of the generic TemplateField - I'd like to preserve that and their grouping in the DynamicallyCreatedTemplateClass that is generated below
templateFields : Group<TemplateField>[];
constructor( sourcePath : string) {
// build shape of templateFields from a file
}
// This should return a constructor for a dynamically created class that inherits from Template as well as a Job mixin but already has a concrete structure in this.templateFields
// in general, the resulting class should resemble the Template instance this is called on (i.e. properties that have values on the instance should have the same values in every instance of the generated class)
getInstanceClass() : () => DynamicallyCreatedTemplateClass { /* ??? */ }
}
const someTemplate = new Template('/path/to/some/file.json');
const someJob = new someTemplate.getInstanceClass();
someJob.templateFields[0].get(0).value = 'something';
if(
someJob.templateFields[0].get(0).type == someTemplate.templateFields[0].get(0).type &&
typeof someTemplate.templateFields[0].get(0).value === 'undefined'
)
console.log('success!');
Is there a pattern to describe this relationship or am I going about this all wrong?
instance
to mean two very different things. However, you are talking aboutTemplate
is if it were a class, and aJob
as if it were an object. The fact thatTemplate
is itself an instance of an object isn't relevant to helping understand the concept.