encapsulation

TDD, DDD and Encapsulation

≡放荡痞女 提交于 2019-12-02 18:08:27
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 respected people in the industry (Greg Young most noticeably so with his talks on CQRS) advocate fully

Is OO design's strength in semantics or encapsulation?

喜欢而已 提交于 2019-12-02 16:21:45
Object-oriented design (OOD) combines data and its methods. This, as far as I can see, achieves two great things: it provides encapsulation (so I don't care what data there is, only how I get values I want) and semantics (it relates the data together with names, and its methods consistently use the data as originally intended). So where does OOD's strength lie? In constrast, functional programming attributes the richness to the verbs rather than the nouns, and so both encapsulation and semantics are provided by the methods rather than the data structures. I work with a system that is on the

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

╄→尐↘猪︶ㄣ 提交于 2019-12-02 14:22:55
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 are implemented. Inversion of Control is a pattern implementation of the Dependency Inversion Principle,

Class Data Encapsulation(private data) in operator overloading

 ̄綄美尐妖づ 提交于 2019-12-02 11:47:42
问题 Below is the code The Code: #include <iostream> using namespace std; class Rational { int num; // numerator int den; // denominator friend istream& operator>> (istream & , Rational&); friend ostream& operator<< (ostream & , const Rational&); public: Rational (int num = 0, int den = 1) : num(num), den(den) {} void getUserInput() { cout << "num = "; cin >> num; cout << "den = "; cin >> den; } Rational operator+(const Rational &); }; Rational Rational::operator+ (const Rational& r) { //HERE int

How-to return a const std::vector<Object *const>?

 ̄綄美尐妖づ 提交于 2019-12-02 06:49:20
I have a class with a container (containing pointer) as a member: MyClass{ private: std::vector<MyObject*> _VecMyObjs; public: const std::vector<MyObject* const> GetVecMyObj(); } Now I try to implement GetVecMyObj(). Here is what I came up with... const vector<MyObject *const> ACI_CALL MyClass::GetVecMyObjs() { const vector<MyObject *const> VecMyObjs; VecMyObjs.assign( _VecMyObjs.begin(), _VecMyObjs.end()); return VecMyObjs; } But of course the compiler is warning me, that I use the assign-function on a const-Object. Is there a better way to do this? I mean, I don't want VecMyObjs to change

Class Data Encapsulation(private data) in operator overloading

倾然丶 夕夏残阳落幕 提交于 2019-12-02 03:57:40
Below is the code The Code: #include <iostream> using namespace std; class Rational { int num; // numerator int den; // denominator friend istream& operator>> (istream & , Rational&); friend ostream& operator<< (ostream & , const Rational&); public: Rational (int num = 0, int den = 1) : num(num), den(den) {} void getUserInput() { cout << "num = "; cin >> num; cout << "den = "; cin >> den; } Rational operator+(const Rational &); }; Rational Rational::operator+ (const Rational& r) { //HERE int n = num * r.den + den * r.num; int d = den * r.den; return Rational (n, d); } istream& operator>>

Is it needed to declare innards of private nested classes private?

妖精的绣舞 提交于 2019-12-02 03:18:35
问题 After 1000s of private s in private it occurred to me that it may not be needed public class Outer { private static class Inner { // you may drop static private void innerMethod() {} } } Is there any case that dropping private from innerMethod() would make a difference in encapsulation (or use , by Outer for instance) ? Think also reflection If not is it recommended to drop it or keep it vis a vis coding style ? I'd say no and drop but not sure really. EDIT : just realized that the way I do

Do both these classes support encapsulation and …?

本秂侑毒 提交于 2019-12-02 02:07:36
public class Normal { public string name; // name is public public String getName() { return name ; } public String setName(String newName) { name = newName ; return name ; } public static void main(String args[]) { Normal normal = new Normal(); normal.setName("suhail gupta"); System.out.println( "My name is : " + normal.getName() ); } } New class starts from here public class Different { private string name; // name is private public String getName() { return name ; } public String setName(String newName) { name = newName ; return name ; } public static void main(String args[]) { Different

Is it needed to declare innards of private nested classes private?

为君一笑 提交于 2019-12-02 00:46:35
After 1000s of private s in private it occurred to me that it may not be needed public class Outer { private static class Inner { // you may drop static private void innerMethod() {} } } Is there any case that dropping private from innerMethod() would make a difference in encapsulation (or use , by Outer for instance) ? Think also reflection If not is it recommended to drop it or keep it vis a vis coding style ? I'd say no and drop but not sure really. EDIT : just realized that the way I do it is certainly wrong - at least for Inner 's fields - declaring those fields private and then using

Encapsulation for mutable objects in Java

好久不见. 提交于 2019-12-01 21:59:00
问题 I was studying the "Java SE 7 Programmer I & II Study Guide" and I did not understand the explanation below. class Fortress{ private String name; private ArrayList<Integer> list; Fortress() {list=new ArrayList<Integer>; String getName{return name;} void addToList(int x){list.add(x);} ArrayList getList(){return list;} // line 1 } Which lines of code break encapsulation? Answer: line 9. "When encapsulating a mutable object like an ArrayList, your getter must return a reference to a copy of the