encapsulation

Scala type alias including companion object [beginner]

≯℡__Kan透↙ 提交于 2019-12-05 04:30:49
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 long type. class ConcreteClass { type DataType = List[Map[Int, (String, String)]] ... } I would then

C++ Is private really private?

别来无恙 提交于 2019-12-04 22:40:02
I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { actualPrintX(); } void A::actualPrintX() { std::cout << x: } I built this in to a static library (.a/.lib). We now have a class_A.h and classA.a (or classA.lib) pair. I edited class_A.h and removed the private: from it. Now in another classTester.cpp: #include "class_A.h" // the newly edited header int main() { A a; a.x = 12; // both G++ and VC++ allowed this! a.printX(); /

Do these two classes violate encapsulation?

人走茶凉 提交于 2019-12-04 20:23:54
问题 class X { protected: void protectedFunction() { cout << "I am protected" ; } }; class Y : public X { public: using X::protectedFunction; }; int main() { Y y1; y1.protectedFunction(); } This way I am able to expose one of the functions of the base class. Doesn't this violate the encapsulation principle? Is there a specific reason as to why this is in standard? Is there any uses of this, or is it going to be changed in the new standard? Are there any open issues related to this in the standard?

what is the advantage of using private variables in C#

拜拜、爱过 提交于 2019-12-04 20:16:16
问题 Sample code (alternative code is below), // person.cs using System; class Person { private string myName ="N/A"; // Declare a Name property of type string: public string Name { get { return myName; } set { myName = value; } } public override string ToString() { return "Name = " + Name; } public static void Main() { Person person = new Person(); Console.WriteLine("Person details - {0}", person); person.Name = "Joe"; Console.WriteLine("Person details - {0}", person); } } Can't we directly write

Getters for Display

送分小仙女□ 提交于 2019-12-04 14:10:54
I was researching on getters/setters , and the general idea is that they are evil and should be avoided. You should let the object do the work, and produce the result. Reading Material: Why getter and setter methods are evil Are getters and setters poor design? Contradictory advice seen Why use getters and setters? With all that in mind, suppose I have a Book class that looked like this: publc final Book{ private final String title; private final List<Authors> listofAuthors; private final int numberofpages; //constructor not included in example. //getters for title, listofAuthors, and

When should encapsulation be used? [closed]

南笙酒味 提交于 2019-12-04 11:44:20
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 5 years ago . I am completing Sun/Oracle's Trail ( http://docs.oracle.com/javase/tutorial/java/TOC.html ) and it keeps reiterating the importance of encapsulation. How important, really, is encapsulation? I mean, if I may need to access the value of a given class field, why would I do so through a method when I could just access the field directly? Since the field would be

How to encapsulate a C API into RAII C++ classes?

六月ゝ 毕业季﹏ 提交于 2019-12-04 11:35:38
问题 Given a C API to a library controlling sessions that owns items, what is the best design to encapsulate the C API into RAII C++ classes? The C API looks like: HANDLE OpenSession(STRING sessionID); void CloseSession(HANDLE hSession); HANDLE OpenItem(HANDLE hSession, STRING itemID); void CloseItem(HANDLE hItem); Plus other functions that are useful for one of these types (Session, or Item) and map directly to C++ member functions of the relevant object. But they are not needed here. My main

Inheritance breaking encapsulation?

流过昼夜 提交于 2019-12-04 08:01:27
People say inheritance breaks encapsulation, which i agree with. They say delegation is better- although the modifiers in delegation can also be public/protected. So is the real reason why inheritance breaks encapsulation because of the "knock-on" effect of the public/protected modifiers from the super class being exposed to any new classes which extend the current subclass? Yes. Since it gives the derived class access to members of the base class (depending on what language and which kind of inheritance) it is said that it breaks encapsulation. IMHO this is only if you are clinging to

TDD, DDD and Encapsulation

房东的猫 提交于 2019-12-04 07:53:20
问题 After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a great fit for the complexity of the software we write. However, many of the TDD samples I have seen call a method on the domain object and then test properties of the object to ensure the behaviour executed correctly. On the other hand, several

Data encapsulation…?

二次信任 提交于 2019-12-04 05:02:49
Would anyone be able to explain to me what data encapsulation in Objective-C is? I've been told that this an important concept of Objective-C but I don't see why... Explain it to me as if I was 5 and then as if I was 25.... Thanks for your time, ~Daniel From http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-2/ : What we mean by data encapsulation is that data is contained (so to speak) by methods meaning to access it we need to use methods. Some of you who have programmed in other languages and havenʼt heard of data encapsulation may be wondering why we do things this way. The