As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
When I'm implementing design patterns, should I keep terms like "strategy", "visitor", "facade" or could I fit these names to the context of my application ? What is the best practice ?
You should fit these names to the context of the application. It will make it easier for the people reading your code. You can add the patterns in your documentation.
I think you should always keep some reference to the pattern in your naming where it makes it meaningful and descriptive.
Patterns are a means of communication. If I come across code that is an XyzVisitor, I know that the visitor pattern has been used. With nothing else, the name has conveyed a whole stack of information on how the code works (or should work).
That said, sometimes it would just be a bit odd. Eg. DatabaseSingleton. Whereas AccountRefreshCommand fits quite nicely.
Depends on which pattern you're using, Some pattern names may be mixed with class names, e.g. I use
class LogFactory
class StudentsAdapter
for factory and adapter patterns, but
Engine.Instance
for singleton.
Depends on whether you are happy renaming the class in the event of using a different pattern, for me it would smack too much of hungarian notation difficulties.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Would I be correct in thinking that an interface has 3 main benefits.
A blueprint to what must be created (I have also heard others refer to it as a contract).
Polymorphism.
Unlike inheritance (which IMO has many similarities) you can have many interfaces
Are there any other plus or minus points and does anyone not agree with my 3 points?
The "blueprint" metaphor works better for classes than for interfaces, but the "contract" metaphor is pretty accurate. An interface specifies what other classes can expect in terms of public methods, without saying anything about the underlying implementation. Where inheritance between classes tends to consist of is-a relationships, interfaces can be thought of as works-as-a relationships, though I don't think the latter term is in common use.
I would add that the use of interfaces goes some way to creating self-documenting code. For example, the interfaces that your class implements describes the functionality the class supports. So, you end up with code that looks like:
if (someClass is ISearchable)
{
someClass.Search();
}
Two objects with the same Interface do not need to be otherwise related.
So you could have
- Flower
- Truck
- Dinosaur
all having the same interface
- IColor
even though they are completely different objects.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I seem to be coming across a lot of variable (boolean) for some Options that control whether something will be done, like:
GiveWarningEnabled
FeedbackEnabled (will provide feedback)
These will be used a lot and I"m trying to think of a good pre/suf fix that will indicted it's Boolean. My best thought so far was Enabled.
Perhaps: Will?
WillGiveWarning (or WillWarn)
WillGiveFeedback?
often times "is" will be used, such as isPlaying or isWifiEnabled.
Will, should, can, and does are also good ones to use because they express conditions. A verb such as "give", like the one you listed, seems to better fit a function declaration since it is performing an action.
I usally prefer flag.
Example:
warningFlag,
feedbackFlag.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
The use of design patterns in programming is wide spread across many programming languages. A number of examples are the factory, or singleton design pattern. Many of these patterns use object orientation to create abstraction and encapsulation in the code, they aim at make code re-usable and structured. Many of these design patterns could also be used in R, maybe by using the proto library, or standard R object orientation?
My questions are:
What base code (S3, S4) / packages (proto, R.oo) can I use to reproduce design patterns as for example mentioned in the book by Gamma et al?
Are there examples of design patterns implemented in R, both in base R, or in packages?
Some examples of design patterns:
The system.time() function seems to behave much like a decorator pattern. However, almost exclusively decorators are mentioned in the context of object oriented programming. But still, it has the feel of a decorator, it extends (or decorates) an existing piece of code (in OOP always an object) with additional functionality without any need to change the piece of code. Here system.time() is shown in action:
system.time(bla <- Sys.sleep(1000))
#jverzani posted an example of singleton pattern on github.
An example of a Strategy Design Pattern is the apply family of functions. The functionality of looping over the given object is generic, the function that is applied (the strategy) is chosen when the user supplies the function.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
My question is language transcendent, I've often found prototypes of "copy" functions define parameters in the order: argument1:"destination" then argument2:"source".
It is the case of memcpy for example in C. But it is NOT the case of file copy on bash ! You say, e.g.: "$ cp file file2" where file2 is the new file.
Which makes much more sense to me, we always say "copy that text here please" and not "copy here that text" which is Yoda-esque.
So the true question is: a good API should use what form (order) ? and maybe another subsidiary question: what form is everybody expecting, if there is any ?
I expect source to come first, and destination later.
If you can disambiguate in the language, it would be better. For example, in a OO language:
source.copyTo(destination);
In a language with named parameters:
copy(source: s, destination: d);
The important thing is to make clear what's going on for people reading the code. Code is more often read than it's written.
I've always preferred source-destination (I'm pushing from here to here), but it probably also depends on the call. If it's only 'copy' you're referring to, I think this works. I'm sure there's there are other pull oriented calls that dest-source would apply better to.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I think it is not necessary for me to explain what is the good thing about OOP. But I would like to know discuss is the static method violate the OOP design? or a more OOP way to do is making a singleton to do such static method?
I would like to know what harm is done by static methods and what purpose is served by adhering to object oriented purity.
This question and its answer makes as much sense as any other argument about object-oriented purity and where a particular language falls on the continuum.
C# and Java both support the notion of methods and attributes associated with classes rather than a specific instance.
The benefit or harm of singletons in design have been explored in detail elsewhere.
Static methods wouldn't exist if they were considered to be bad practice in the OOP paradigm. Static methods are absolutely necessary at times, if you know how to use them. If you have a method that does not make use of, or change, any member of a class object, then it is static by nature.