encapsulation

The reason for encapsulation [closed]

时光毁灭记忆、已成空白 提交于 2019-11-30 10:01:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I've read some things about this, I even found similar question, but it didn't really answer this. For me it seems that privatizing something only makes my life so much harder when I need to find a private variable in a class to use it elsewhere. So what is would the problem be if everything was public? Would it

Shorthand Accessors and Mutators

梦想的初衷 提交于 2019-11-30 08:40:26
I am learning C#, and am learning about making fields private to the class, and using Getters and Setters to expose Methods instead of field values. Are the get; set; in Method 1 and Method 2 equivalent? e.g. is one a shorthand of the other? class Student { // Instance fields private string name; private int mark; // Method 1 public string Name { get; set; } // Method 2 public int Mark { get { return mark; } set { mark = value; } } } Finally, would Method 2 be used when you want to for example perform a calculation before getting or setting a value? e.g. converting value to a percentage or

Why do people write private-field getters returning a non-const reference?

喜欢而已 提交于 2019-11-30 08:02:22
问题 We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int& integer() { return integer_; } string& someString() { return someString_; } // other "functions" } int main() { foo f; f.integer() = 10; f.someString() = "something"; return 0; } I have seen this being used in many places and I don't get why. Basically it returns a

Objective-c: why private ivars are not hidden from the outside access when using KVC

泪湿孤枕 提交于 2019-11-30 07:15:08
问题 After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC method "setValue". Here is my code where all of the seven ivars and properties are changeble outside the class instance: //************ interface file ***************// @interface MyClass : NSObject { @public NSNumber *public_num; @protected

When should a class use its own getters/setters vs accessing the members directly?

扶醉桌前 提交于 2019-11-30 06:50:41
When generating setters and getters in Eclipse one of the options is to use the getters and setters within the class rather than accessing the class members directly. Is this level of class internal encapsulation useful or is it taking a good idea one step too far? DUPE : Should you use accessor properties from within the class, or just from outside of the class? I think it's a good idea if you want the potential side-effects to occur - validation, logging etc. (In C# I'd like to be able to declare a variable and property and say that the only access to the variable is through the property.)

Design pattern for multiple output formats

最后都变了- 提交于 2019-11-30 05:00:26
问题 I have a class structure which represents (internally) the data I wish to output to a file. Some of the member variables are private to the data class so that it can manage itself and stop things going awry. I then want this data to be output into a number of file formats. I could do something like savefile_formatA(DataClass* pDataClass, ofstream& fout); savefile_formatB(DataClass* pDataClass, ofstream& fout); except that the functions need to then see the private member variables of

Pattern for Creating a Simple and Efficient Value type

孤街醉人 提交于 2019-11-30 04:38:39
Motivation: In reading Mark Seemann’s blog on Code Smell: Automatic Property he says near the end: The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value type and all conceivable values are allowed. He gives int Temperature as an example of a bad smell and suggests the best fix is unit specific value type like Celsius. So I decided to try writing a custom Celsius value type that encapsulates all the bounds checking and type conversion logic as an exercise in being more SOLID . Basic requirements:

Allow access to but prevent instantiation of a nested class by external classes

a 夏天 提交于 2019-11-30 04:17:32
问题 I'm looking to define a nested class that is accessible to the container class and external classes, but I want to control instantiation of the nested class, such that only instances of the container class can create new instances of the nested class. The proceeding code should hopefully demonstrate this: public class Container { public class Nested { public Nested() { } } public Nested CreateNested() { return new Nested(); // Allow } } class External { static void Main(string[] args) {

Why am I able to use my value constructor even though I don't export it?

半世苍凉 提交于 2019-11-30 04:15:22
问题 For practice, I'm implementing a queue data type in a module called "Queue". My data type is also called "Queue", as is its only value constructor: module Queue (Queue, enq, emptyQueue) where data Queue a = Queue { inbox :: [a], outbox :: [a] } deriving (Eq, Show) emptyQueue :: Queue a emptyQueue = Queue [] [] enq :: a -> Queue a -> Queue a enq x (Queue inb out) = Queue (x:inb) out -- other function definitions (irrelevant here)... As far as I understand, because I wrote Queue , not Queue(..)

How to make a reference type property “readonly”

大城市里の小女人 提交于 2019-11-30 03:53:56
I have a class Bar with a private field containing the reference type Foo . I would like to expose Foo in a public property, but I do not want the consumers of the property to be able to alter Foo ... It should however be alterable internally by Bar , i.e. I can't make the field readonly . So what I would like is: private _Foo; public Foo { get { return readonly _Foo; } } ...which is of course not valid. I could just return a clone of Foo (assumming that it is IClonable ), but this is not obvious to the consumer. Should I change the name of the property to FooCopy ?? Should it be a