Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am writing a class which consists mostly of data storage responsibilities. The context for the use of this class will, in general, be retrieving an array of objects of this type for display later. In light of that, I've created a static method, build_question_array(), which queries the database, instantiates the objects, and returns the array. Does this violate the "SOLID" principle?

share|improve this question
    
(1) will a constructor be sufficient? (2) can the array of such objects be stored in a standard array or collection? (3) does your static method talk directly to the database (issuing the queries directly)? (My opinion is that your static method is probably a "utility function", and as such it can be placed anywhere, not necessary inside your data object.) –  rwong Oct 2 '11 at 3:04

1 Answer 1

up vote 2 down vote accepted

Firstly, it does not obey the Open/Closed principle, because you won't be able to change anything at runtime in a static method.

I don't know whether it obeys the Single Responsibility principle, because I don't know whether it implements or delegates all of its work. If it implements it, it doesn't obey this principle.

EDIT: If that static method is inside the data structure's class, then it definitely does not obey SRP. The data class should not know or care about its various types of uses.

The Liskov Substitution principle isn't a factor since it's only one class, and it's a static method.

Interface Segregation also doesn't apply here.

Dependency Inversion is questionable here, because unless you can pass a delegate to this function or you're using a dynamic language, you're making every one of your clients depend on this sole implementation of this functionality.

So all in all, no, it doesn't.

share|improve this answer
4  
"you won't be able to change anything at runtime in a static method" - the OCP does not mandate runtime changes specifically. –  Péter Török Oct 2 '11 at 6:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.