Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is basically what i am now using a lot in my application for accessing objects that needed to be accessed from many classes:
Application::getInstance()->getComponentList()
Still think that this is not the good way how to access objects that need to be shared among many classes.
Question is if there is better approach to share objects in big application.
I have to agree with you and juanchopanza: accessing objects via a single singleton object throughout an entire project is a poor practice since modules are supposed to have few and well-stated dependencies among them (ideally: interfaces or means of communication).
Also: it's easy to violate the single responsibility principle.
There's no "do X instead of the singleton approach" solution, or no silver bullet: sometimes a singleton can really be useful but if possible: avoid it and design a clear interface instead.
Also: there isn't enough information in your question to state something more specific.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Feels like a question that has been asked before but I'm not exactly sure how to properly word it in a google search. Say I have a MainWindow object and in that object I define several objects PanelA, PanelB, PanelC and PanelD. What's a good way of connecting these objects to each other without having constructors that look like this
PanelA(PanelB* b, PanelC* c, PanelD* d)
Would you suggest to just pass them all in through the main class like so
PanelA(MainWindow* mw)
{
b=mw->b; c=mw->c; d=mw->d;
}
Or is there better ways to structure my classes. What's this problem known as in general, so that I can google this stuff myself.
What's this problem known as in general, so that I can google this stuff myself.
The design defect is known as strong coupling of types and use of hardcoded relations.
The general approach to refactor such code is to introduce interfaces and design patterns to solve the actual dependencies by means of functionality.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
A lot of WinAPI objects, such as handles, events or sockets, are resources. I found it safe and efficient to wrap them in RAII classes.
Are there any pitfalls in doing that? Is it common practice or, instead, a bright, misleading idea? Or, maybe, there are already libraries for that? (Although I couldn't find anything worthy.)
Mostly, I'm interested in wrappers for 'small' objects like files, sockets, events and threads, that are not related to GUI or COM. Libraries like MFC or ATL may do such job but don't seem to be lightweight and no-overhead what I'm asking about.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This link talks about allowing more restrictive access to a derived class method.
Q. What is the reason for allowing this in C++?
Languages such as Java & C# don't allow it. Is it useful in some cases? If so please help me understand.
It has more to do with it never being disallowed. And now it's too late: too much code would break. Do remember that C++ is a considerably older language than either Java or C#.
But the C++ philosophy inspires you to ask "why disallow it?". It could even be useful: some folk exploit it and make overridden methods private. The amount of documentation you should attach to a private method can be significantly less than a public method. This means you don't repeat yourself and are compelled to rely on the public / protected method in a base class for comments.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Many times I hear "Singleton is a bad practice, static methods are bad practice" all I can see for reason is "hard to test".
But I do think sometimes its really good if an operation can be done without instantiate a class.
EDIT: just because of testing, anyone can find out that "private methods are bad too, they cant be tested" for example
It is a question about semantics and expressing intent.
A static method is not inherently bad, apart from that it is hard to test. The bad part is confusing other programmers by using static methods just to avoid creating new instances.
If the method relates to the class itself and not to individual instances (Like a factory method for example), then by all means use a static one. But if the method semantically belongs to an individual instance, then use a non static method.
Static methods are generally frowned upon for much the same reason as global variables, in addition to testing issues:
Static methods do not relate to a specific class instance so will not always be thread safe.
Systems with lots of statics methods often do not scale well.
Confusion due to the mixture between calling static methods of a class and members of an instance of a class can lead to maintenance issues.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've heard systems described as a "clean object model", but a precise definition does not seem to be around. It seems to refer to the classes being complete or consistent in some way.
I'm just wondering if it's referring to a specific trait or just another favorable term like 'elegant'.
Quantlib is described as "written in C++ with a clean object model".1
It's not really a technical term. A "clean" object model is a well-designed one, by whichever standard of good design. Usually it involved orthogonal classes with a clear separation of concerns and an intuitive mapping to real-world concepts, i.e. a lot of fuzziness that you'll need to judge for yourself.