In need of a method to control document activation logic, where activating a document involves adding it to a list of activeDocuments and setting a flag to true. Direct access to the isActive property should be prohibited.
class DocumentService {
private activeDocuments : Map<DocumentModel> = new Map<DocumentModel>();
// Activates the document and adds it to the list
activateDocument(document: DocumentModel) {
document.setActive();
activeDocuments.set(document.id, document);
}
}
class DocumentModel {
private isActive: boolean;
setActive() {
this.isActive = true;
}
}
class DocumentComponent {
documentSelected() {
// this.document.setActive() - SHOULD BE FORBIDDEN because the document is not added to the activedocument list !
this.documentService.activateDocument(this.document);
}
}
To address this issue, one possible solution is creating two interfaces: DocumentServiceInterface with a setActive() method and DocumentInterface without it. This setup prevents |DocumentComponent from activating the document while allowing the service to do so.
Are there any design patterns or strategies that can provide a solution?
Iterating through a list of documents to determine their active status is not feasible due to the complexity of the application structure and the potential for thousands of documents.