Is there a pattern or best practice for passing a reference type to multiple classes vs a static class?

Posted by Dave on Programmers See other posts from Programmers or by Dave
Published on 2014-08-20T09:59:40Z Indexed on 2014/08/22 4:27 UTC
Read the original article Hit count: 394

My .NET application creates HTML files, and as such, the structure looks like

variable myData
BuildHomePage()
variable graph = new BuildGraphPage(myData)
variable table = BuildTablePage(myData)

BuildGraphPage and BuildTablePage both require access data, the myData object.

In the above example, I've passed the myData object to 2 constructors. This is what I'm doing now, in my current project.

The myData object, and it's properties are all readonly.

The problem is, the number of pages which will require this object has grown. In the real project, there are currently 4, but the new spec is to have about 20. Passing this object to the constructor of each new object and assigning it to a field is a little time consuming, but not a hardship!

This poses the question whether it's better practice to continue as I have, or to refactor and create a new static class for myData which can be referenced from any where in my project.

I guess my abilities to use Google are poor, because I did try and find an appropriate pattern as I am sure this type of design must be common place but my results returned nothing.

Is there a pattern which is suited, or do best practices lean towards one implementation over another.

© Programmers or respective owner

Related posts about design-patterns

Related posts about programming-practices