Tell me more ×
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 have a program where I want to save some data record. And I want the output type to be flexible such that I could save the data record to a text file, xml file, database, push to a webservice.

My take on it would be to create an interface such as DataStore with a Save() method, and the concrete subclasses such as TextFileDataStore, DatabaseDataStore, etc.

  1. What is the proper name/terminology for this type of pattern (I'm using the term "DataStore", log4net names things "appenders", .net they talk about "providers" and "persistence")? I want to come up with good class names (and method names) that fit with a convention if there is one.

  2. can you point me to a decent example, preferably in C#, C++, or java?

Update

Managed to find this stack overflow question, Object persistence terminology: 'repository' vs. 'store' vs. 'context' vs. 'retriever' vs. (…), which captures the terminology part of my question pretty well although there's not a decent answer yet.

share|improve this question
2  
+1 As you mentioned the term 'flexible' data type. It is important to mention JSON here. This is because of the increasingly ubiquitous nature of its host language, and the fact that it needs no custom parsing like xml. JSON stands for JavaScript Object Notation - it is, unless anyone else disagrees, the most flexible data-type on the web. Hope that helps. – ClintNash Jul 10 '12 at 2:40

closed as not a real question by Jarrod Roberson, ChrisF Jul 10 '12 at 8:23

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

One approach is to use a central "manager" as the receiver of messages. A config file can determine which classes receive these messages. The manager publishes messages out to these observers. In other words, the code publishes to a manager; the manager, based on registered observers, calls the method that they have registered to receive the string input. This way, you might have a list like [System.out.println, logger.debug, database_logger.debug], and the manager deals with calling each of those when provided with a string.

Appropriately enough, this is called the Observer Pattern.

share|improve this answer
Hadn't thought about outputting to multiple sources at once, good point! – User Jul 10 '12 at 1:50
You probably don't want to make the manager-class dependent on the concrete store/logger-classes. So the question remains unanswered how to abstract the concrete classes. – simoraman Jul 10 '12 at 6:43
@simoraman: The store/logger classes would all be observers/subscribers, so the manager doesn't need to know anything about the concrete classes. I thought that was pretty evident! – Baqueta Jul 10 '12 at 8:11

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