My understanding of object oriented programming and modular project structures in JS is that it's best practice to decouple objects/modules as much as possible. For example say I have the following two decoupled modules:
// person.js
module.exports = function() {
this.firstName: '';
this.lastName: '';
};
// occasion.js
module.exports = function() {
this.title: '';
this.date: '';
};
When instantiated by these modules, objects will hold relevant data to that type. However lets say I have another module that uses data from both types:
// invitation.js
module.exports = function() {
this.eventTitle: '';
this.eventDate: '';
this.sendTo: '';
}
Instances of this module need to take the title
and date
from Occasion
objects and also take and concatenate the firstName
and lastName
from Person
objects.
Obviously to be able to take information from two particular object types and combine them into another type I am going to have to have a function or something that is heavily coupled i.e. something that understands the structure of all the modules involved.
Considering best practices for OOP and modulated code management methodologies how should these transformers, stitchers, controllers whatever you want to call them, be implement in a project?
1) Do I create an module for each possible interaction that exports an object with a single inbuilt method? e.g.
//person-and-occasion-to-invitation.js
module.exports = function() {
this.getInvitation = function(person, occasion) {
var invitation = new Invitation();
invitation.eventTitle = occasion.title;
invitation.eventDate = occasion.date;
invitation.sendTo = person.firstName + ' ' + person.lastName;
return invitation;
};
};
This seems silly to instantiate a single object just to perform a single task and my modules folder would get full very fast.
2) Do I create an module for each possible interaction that exports an a single method? e.g.
//person-and-occasion-to-invitation.js
module.exports = function(person, occasion) {
var invitation = new Invitation();
invitation.eventTitle = occasion.title;
invitation.eventDate = occasion.date;
invitation.sendTo = person.firstName + ' ' + person.lastName;
return invitation;
};
Once again my modules folder would get full very fast and also in OOP shouldn't everything be contained in an object?
3) Do I create an module that contains all possible interactions? e.g.
//interactions.js
module.exports = function() {
this.personAndOccasionToInvitation = function(person, occasion) {
var invitation = new Invitation();
invitation.eventTitle = occasion.title;
invitation.eventDate = occasion.date;
invitation.sendTo = person.firstName + ' ' + person.lastName;
return invitation;
};
};
This will keep my modules folder a lot cleaner but it could get very long and I feel like it is creating a God object.
What is best/common practice when dealing with decoupled modules? Can somebody explain the methodology and provide examples?