encapsulation

“public” or “private” attribute in Python ? What is the best way?

…衆ロ難τιáo~ 提交于 2019-11-27 10:27:05
问题 In Python, I have the following example class : class Foo: self._attr = 0 @property def attr(self): return self._attr @attr.setter def attr(self, value): self._attr = value @attr.deleter def attr(self): del self._attr As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it's not respecting the "KISS" philosophy to declare all attributes like that. So, why not declare all my

Using a strategy pattern and a command pattern

夙愿已清 提交于 2019-11-27 09:58:50
Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern takes in parameters for execution, while the Command pattern doesn't. It seems to me that the command pattern requires all information for execution to be available when it is created, and it is able to delay its calling (perhaps as part of a script). What determinations guide whether to use one pattern or the other? I'm including an encapsulation hierarchy table of several of the GoF design patterns to help explain the

Globally defined AngularJS controllers and encapsulation

旧城冷巷雨未停 提交于 2019-11-27 07:35:50
According to AngularJS's tutorial, a controller function just sits within the global scope. http://docs.angularjs.org/tutorial/step_04 Do the controller functions themselves automatically get parsed into an encapsulated scope, or do they dwell within the global scope? I know that they are passed a reference to their own $scope, but it appears that the function themselves are just sitting in the global scope. Obviously this can cause problems down the road, and I have learned through experience and education to encapsulate Further more, if they do dwell within the global scope, would it not be

Why encapsulation is an important feature of OOP languages? [closed]

喜你入骨 提交于 2019-11-27 07:27:34
I came across different interview where question was asked to me why encapsulation is used? Whose requirement actually is encapsulation? Is it for users of program? Or is it for co-workers? Or is it to protect code from hackers? BartoszKP Encapsulation helps in isolating implementation details from the behavior exposed to clients of a class (other classes/functions that are using this class), and gives you more control over coupling in your code. Consider this example, similar to the one in Robert Martin's book Clean Code : public class Car { //... public float GetFuelPercentage() { /* ... */

SQL Server: How to permission schemas?

笑着哭i 提交于 2019-11-27 07:26:54
Inspired by various schema related questions I've seen... Ownership chaining allows me to GRANT EXECUTE on a stored procedure without explicit permissions on tables I use, if both stored procedure and tables are in the same schema. If we use separate schemas then I'd have to explicitly GRANT XXX on the the different-schema tables. The ownership chaining example demonstrates that. This means the stored proc executing user can read/write your tables directly. This would be like having direct access to your instance variables in a class, bypassing getter/setters, breaking encapsulation. We also

Getters and Setters are bad OO design? [duplicate]

…衆ロ難τιáo~ 提交于 2019-11-27 07:06:32
This question already has an answer here: Why use getters and setters/accessors? [closed] 38 answers Getters and Setters are bad Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating objects and how can one model objects to take this into account. In cases where a getter or setter is required what other alternatives can be used? Thanks. Getters or setters by themselves are not bad OO design. What is bad is coding practice which

Java: Subpackage visibility?

六眼飞鱼酱① 提交于 2019-11-27 07:01:48
I have two packages in my project: odp.proj and odp.proj.test . There are certain methods that I want to be visible only to the classes in these two packages. How can I do this? EDIT: If there is no concept of a subpackage in Java, is there any way around this? I have certain methods that I want to be available only to testers and other members of that package. Should I just throw everything into the same package? Use extensive reflection? You can't. In Java there is no concept of a subpackage, so odp.proj and odp.proj.test are completely separate packages. The names of your packages hint that

C#: Difference between List<T> and Collection<T> (CA1002, Do not expose generic lists) [duplicate]

北战南征 提交于 2019-11-27 06:17:29
This question already has an answer here: Collection<T> versus List<T> what should you use on your interfaces? 8 answers Tried to run Run Code Analysis on a project here, and got a number of warnings that said something like this: CA1002 : Microsoft.Design : Change 'List< SomeType >' in ' SomeClass.SomeProtectedOrPublicProperty ' to use Collection, ReadOnlyCollection or KeyedCollection Why should I use Collection<T> instead of List<T> ? When I look at the msdn documentation, they seem almost equal. After reading the error help for the warning, I found that System.Collections.Generic.List(T)_is

What are the benefits of using properties internally?

蹲街弑〆低调 提交于 2019-11-27 06:17:12
问题 Encapsulation is obviously helpful and essential when accessing members from outside the class, but when referring to class variables internally, is it better to call their private members, or use their getters? If your getter simply returns the variable, is there any performance difference ? 回答1: There shouldn't be a significant performance difference, and the reason you stick to using the properties is because that's the whole point of encapsulation. It keeps all accesses of those private

When should I prefer non-member non-friend functions to member functions?

≯℡__Kan透↙ 提交于 2019-11-27 05:25:51
问题 Meyers mentioned in his book Effective C++ that in certain scenarios non-member non-friend functions are better encapsulated than member functions. Example: // Web browser allows to clear something class WebBrowser { public: ... void clearCache(); void clearHistory(); void removeCookies(); ... }; Many users will want to perform all these actions together, so WebBrowser might also offer a function to do just that: class WebBrowser { public: ... void clearEverything(); // calls clearCache,