user814435
user814435

Reputation: 3

Is there a design pattern for this

I have the following domain, a dossier with documents:

-Dossier(Name,Documents[])
-Document(Name)

Now there is a new requeriment for a few dossiers which have subjects and each subject has documents, so the new domain would be like this:

-Dossier(Name,Subjects[])
-Subject(Number,Documents[])
-Document(Name)

For these few dossiers (7%) , I have to change a simple design for a more complex design. My question is: There is a design pattern or any idea which I can use for support the subjects but avoiding to change the initial domain.

Upvotes: 0

Views: 253

Answers (3)

Marcin
Marcin

Reputation: 917

There are many things to consider when choosing a pattern

If the design changes dynamically acording to a context which is external to Dossier consoder Delegation pattern (wiki).

If this should be set during a "construction" or "initialization" stage you can consider a pimpl (www.codeproject.com) or Decorator (wiki) pattern

Or maybye you need something different...

The choosen solution should depend on the exact problem that you need to solve. Two simple rules are:

  1. Favor composition in place of inheritance
  2. Encapsulate the variability

1) Means delegate to work to a components. Each cpmponent should do a "one thing" but do it good.

2) Means if there is a part in your system that is subject to change - encapsulete it in a seperate entity(ies)

For an introduction material in design patterns I could recomend tyou the "gang of four" book" Design Patterns: Elements of Reusable Object-Oriented Software

Upvotes: 0

Adam Eberlin
Adam Eberlin

Reputation: 14205

I think you are looking for the Decorator Pattern.

You may also find the Composite Pattern or the Delegation pattern useful here.

Upvotes: 1

Piotr Sobczyk
Piotr Sobczyk

Reputation: 6583

You can just keep references/links from Dossier to both collection of Subjects and collection of Documents (so it would be -Dossier(Name,Documents[],Subjects[]) ) and implement some validation rules on DB/Programming language level like: when collection of Subjects is not empty in a Dossier, this dossier cannot have any Documents (colleciton of Documents must be empty). This way you change your model only additively without actually modyfing or removing anything existing.

Alternatively you can create a base class Dossier which has name and two subclasses: DossierWithSubject (with collection of subjects) and DossierWithoutSubject (with collection of Documents) where the latter is exactly what you had so far. So just make / treat your current Dossiers as DossiersWithoutSubject.

Hope anything of this was helpful.

Upvotes: 1

Related Questions