encapsulation

Access-specifiers are not foolproof?

眉间皱痕 提交于 2019-12-10 10:59:13
问题 If I've a class like this, class Sample { private: int X; }; Then we cannot access X from outside, so this is illegal, Sample s; s.X = 10; // error - private access But we can make it accessible without editing the class ! All we need to do is this, #define private public //note this define! class Sample { private: int X; }; //outside code Sample s; s.X = 10; //no error! Working code at ideone : http://www.ideone.com/FaGpZ That means, we can change the access-specifiers by defining such

Javascript static/singelton - this vs _this vs object name

依然范特西╮ 提交于 2019-12-10 06:58:09
问题 This is a question about performance and best practice. Assuming I have a js object that encapsulates a large number of helper methods. The object is being treated as a static class, meaning it is never instantiated and all its methods are basically helper methods. When using events and jQuery, the object's this scope keeps changing, and since it has a fairly large number of methods I am wondering what is best practice - saving this into _this at the beginning of each method or simply use the

Encapsulation in the age of frameworks

不问归期 提交于 2019-12-10 04:15:26
问题 At my old C++ job, we always took great care in encapsulating member variables, and only exposing them as properties when absolutely necessary. We'd have really specific constructors that made sure you fully constructed the object before using it. These days, with ORM frameworks, dependency-injection, serialization, etc., it seems like you're better off just relying on the default constructor and exposing everything about your class in properties, so that you can inject things, or build and

How often do you see abuse of C# shorthand getters/setters?

守給你的承諾、 提交于 2019-12-10 04:06:18
问题 In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My question is - how often do you see this abused? It seems like it has a high potential to violate encapsulation best-practices often. Don't get me wrong, I use it as appropriate, and partial variations of it for read-only write-only types of properties,

Scala type alias including companion object [beginner]

左心房为你撑大大i 提交于 2019-12-10 03:39:28
问题 I'd like to write a type alias to shorten, nice and encapsulated Scala code. Suppose I got some collection which has the property of being a list of maps, the value of which are tuples. My type would write something like List[Map[Int, (String, String)]] , or anything more generic as my application allows it. I could imagine having a supertype asking for a Seq[MapLike[Int, Any]] or whatever floats my boat, with concrete subclasses being more specific. I'd then want to write an alias for this

Acessing the backing field in an auto property

元气小坏坏 提交于 2019-12-10 03:09:25
问题 Is there any way to access the backing field for a property in order to do validation, change tracking etc.? Is something like the following possible? If not is there any plans to have it in .NET 4 / C# 4? public string Name { get; set { if (value != <Keyword>) { RaiseEvent(); } <Keyword> = value; } } The main issue I have is that using auto properties doesn't allow for the same flexibility in validation etc. that a property with a explicit backing field does. However an explicit backing

Data encapsulation in Swift

早过忘川 提交于 2019-12-09 12:28:26
问题 I've read the entire Swift book, and watched all the WWDC videos (all of which I heartily recommend). One thing I'm worried about is data encapsulation. Consider the following (entirely contrived) example: class Stack<T> { var items : T[] = [] func push( newItem: T ) { items.insert( newItem, atIndex: 0 ) } func pop() -> T? { if items.count == 0 { return nil; } return items.removeAtIndex( 0 ); } } This class implements a stack, and implements it using an Array. Problem is, items (like all

C#: Encapsulation of for example collections

限于喜欢 提交于 2019-12-09 06:39:59
问题 I am wondering which one of these would be considered the cleanest or best to use and why. One of them exposes the a list of passengers, which let the user add and remove etc. The other hides the list and only let the user enumerate them and add using a special method. Example 1 class Bus { public IEnumerable<Person> Passengers { get { return passengers; } } private List<Passengers> passengers; public Bus() { passengers = new List<Passenger>(); } public void AddPassenger(Passenger passenger)

Can There Be Private Extension Methods?

僤鯓⒐⒋嵵緔 提交于 2019-12-09 02:24:57
问题 Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try this: class Program { static void Main(string[] args) { var value = 0; value = value.GetNext(); // Compiler error } static int GetNext(this int i) { return i + 1; } } The compiler doesn't "see" the GetNext() extension method. The error is: Extension

Advantages to Nested Classes For Listeners in GUIs

拜拜、爱过 提交于 2019-12-08 23:44:31
问题 For decently sized projects I've been told that when you have classes extending JPanels that the best practice is to use nested classes to implement the listeners. For example I could have a class FactoryScreen that extends JPanel, and have a nested class FactoryScreenBrain that implements all the necessary listeners. I've never been able to get a good explanation for specific benefits or disadvantages to encapsulating my classes in this fashion, and until now have always just had classes