encapsulation

What methods are there to modularize C code?

孤人 提交于 2019-12-20 12:15:56
问题 What methods, practices and conventions do you know of to modularize C code as a project grows in size? 回答1: Create header files which contain ONLY what is necessary to use a module. In the corresponding .c file(s), make anything not meant to be visible outside (e.g. helper functions) static. Use prefixes on the names of everything externally visible to help avoid namespace collisions. (If a module spans multiple files, things become harder., as you may need to expose internal things and not

Dependency Inversion Principle (SOLID) vs Encapsulation (Pillars of OOP)

家住魔仙堡 提交于 2019-12-20 08:07:03
问题 I was recently having a debate about the Dependency Inversion Principle , Inversion of Control and Dependency Injection . In relation to this topic we were debating whether these principles violate one of the pillars of OOP, namely Encapsulation . My understanding of these things is: The Dependency Inversion Principle implies that objects should depend upon abstractions, not concretions - this is the fundamental principle upon which the Inversion of Control pattern and Dependency Injection

Cannot access protected member of another instance from derived type's scope

橙三吉。 提交于 2019-12-20 02:12:41
问题 In this answer to the question " Why can't my object access protected members of another object defined in common base class? ", one can read: You can only access protected members from your own base class instance. Either I don't get it correctly or the following MCVE (live on coliru) proves it wrong: struct Base { void f(); protected: int prot; }; struct Derived : Base { void g(); private: int priv; }; void Base::f() { Base b; b.prot = prot; (void) b; } void Derived::g() { { Derived d;

Why not use 'protected' or 'private' in PHP?

折月煮酒 提交于 2019-12-19 17:42:31
问题 I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore " _ " in front of the method name), but they do not explicitly declare any methods public , private , or protected . Why is this? Does it have to do with portability? Are the public , private , or protected keywords not available in older versions of PHP? 回答1: public, private and protected are PHP5 keywords. unfortunately, PHP4 still has a

Why not use 'protected' or 'private' in PHP?

Deadly 提交于 2019-12-19 17:42:01
问题 I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore " _ " in front of the method name), but they do not explicitly declare any methods public , private , or protected . Why is this? Does it have to do with portability? Are the public , private , or protected keywords not available in older versions of PHP? 回答1: public, private and protected are PHP5 keywords. unfortunately, PHP4 still has a

How to use Dependency Injection without breaking encapsulation?

懵懂的女人 提交于 2019-12-18 10:22:30
问题 How can i perform dependency injection without breaking encapsulation? Using a Dependency Injection example from Wikipedia: public Car { public float getSpeed(); } Note: Other methods and properties (e.g. PushBrake(), PushGas(), SetWheelPosition() ) omitted for clarity This works well; you don't know how my object implements getSpeed - it is " encapsulated ". In reality my object implements getSpeed as: public Car { private m_speed; public float getSpeed( return m_speed; ); } And all is well.

Encapsulation and Getters

♀尐吖头ヾ 提交于 2019-12-18 07:08:23
问题 I was reading this article on why getter and setters are evil. The article doesn't say not to use them ever, but, it's telling you to think in a way that limits the use of those methods, or to quote the article: Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. what happens when you need to display data in a GUI, but don't have getter methods? The article covers this briefly, but not fully. It mentions passing a JComponent

Declaring private member variables

ぃ、小莉子 提交于 2019-12-18 04:46:15
问题 I've started learning Objective-C a few weeks ago and I still don't understand how to manage the encapsulation of a class correctly. What is the best way to declare a private member variable in a class? It seems that setting the right getter/setter for your member variable with "@property" is the right way to go, more than just declaring it "@private" in the interface. But it seems to me that this still gives other classes an access to these variables. Even if you declare the property

Private Methods Over Public Methods

心不动则不痛 提交于 2019-12-17 23:44:32
问题 I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of the principles of OOD is to make as much as you can private and hide all of the implementation details. I'm not sure I completely understand the logic behind this though. I understand that it's important to make fields private to prevent invalid

encapsulation in javascript module pattern

和自甴很熟 提交于 2019-12-17 20:49:17
问题 I was reading this link http://addyosmani.com/largescalejavascript/#modpattern And saw the following example. var basketModule = (function() { var basket = []; //private return { //exposed to public addItem: function(values) { basket.push(values); }, getItemCount: function() { return basket.length; }, getTotal: function(){ var q = this.getItemCount(),p=0; while(q--){ p+= basket[q].price; } return p; } } }()); basketModule.addItem({item:'bread',price:0.5}); basketModule.addItem({item:'butter'