encapsulation

Encapsulation Vs Plain

痴心易碎 提交于 2019-12-04 03:52:13
问题 Why should I use encapsulation if the code below will produce the same result? The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. But I can use this benefit whithout using encapsulation right? because every object's field differs from each other's field. // Person.java public class Person { // Plain public String name; // Uses encapsulation private String name2; public void setName(String name2) { this.name2 =

DDD and the use of Getters and Setters

血红的双手。 提交于 2019-12-04 02:31:07
I've read a few articles/posts regarding the use of Getters and Setters, and how they help to defeat the purpose of encapsulation in domain model objects. I understand the logic behind not using setters - you are allowing client code to manipulate attributes of that object, outside the context of the object business rules and invariants. Now this principal does still confuse me. For example, what happens if I need to change the value of a member variable of an object? For example, if the name of a person changes how can I reflect this in the model? At first I thought, well why not have a

Extending a type in C++

a 夏天 提交于 2019-12-04 01:39:01
Sadly, UFCS did not make it into C++17 and that left me with a recurring problem: Sometimes I want to give types extra functionality using the method call syntax (without writing global functions). That would especially come handy when dealing with monads. I see two options: One is inheritance, and the other is encapsulation. Because you cannot safely inherit from STL containers, that leaves encapsulation. For example, I want to extend std::optional , so I write: template <typename T> struct myoption { // Some functionality private: std::optional<T> impl; }; My problem is that every time I

Java: Which is faster? Local variables or accessing encapsulation?

二次信任 提交于 2019-12-03 20:12:14
I recently read a StackOverflow question that indicated, when accessing variables, it is faster to use the stack than the heap: void f() { int x = 123; // <- located in stack } int x; // <- located in heap void f() { x = 123 } However, I can't work it through my head which is faster in my example (since I assume they are both using the stack). I'm working on hitbox calculation and such, which uses alot of X-Y, width, height variables (up to 10-20 times for each) in the function. Is it faster to use an object's get() method each time or set it to a local variable at the start of the function?

Is there any workaround for making a structure member somehow 'private' in C?

╄→гoц情女王★ 提交于 2019-12-03 18:42:39
问题 I am developing a simple library in C, for my own + some friends personal use. I am currently having a C structure with some members that should be somehow hidden from the rest of the application, as their use is only internal. Modifying by accident one of this members will probably make the library 'go wild'. Is there any 'workaround' to hide those members so that they can't be accessible ? 回答1: The usual techique is this: /* foo.h */ typedef struct Foo Foo; Foo *foo_create(...); void foo

what is the advantage of using private variables in C#

爱⌒轻易说出口 提交于 2019-12-03 13:12:11
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, changing myName from private to public, no requirement to declare another public variable Name and no

Whats the point of accessing private variables through getter and setter (accessor) functions?

允我心安 提交于 2019-12-03 12:13:12
In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can occur with that variable. However, I always see variables made private, and then a getter and setter function used to retrieve that value (sometimes even a pointer to that variable!). For example int a is private to prevent public access, but then getA() and setA() allow direct access to them. So don't getter functions and setter functions defy the

MVC 3, reuse of partial views and jquery, without conflicting the DOM

喜夏-厌秋 提交于 2019-12-03 11:36:55
As i am still new to MVC 3 and jquery, i would like to know a best practice solution to how the following can be solved: I have a view, where I use jquery ajax to fetch and display a partial view with some product details for product A. The loaded partial view consist of a bunch of html and jquery code, which is tied to the defined id's within the partial view. Thus, i would like to reuse the same partial view to show details from other products on the same View (e.g. show product B details in a pop-up dialog). Whenever the pop-up is shown, the newly fetched partial view will conflict with the

Ruby class with static method calling a private method?

本小妞迷上赌 提交于 2019-12-03 10:45:58
问题 I have a class with a number of static methods. Each one has to call a common method, but I'm trying not to expose this latter method. Making it private would only allow access from an own instance of the class? Protected does not seem like it would solve the problem here either. How do I hide do_calc from being called externally in a static context? (Leaving it available to be called from the first two static methods.) class Foo def self.bar do_calc() end def self.baz do_calc() end def self

Abstracting away from data structure implementation details in Clojure

試著忘記壹切 提交于 2019-12-03 10:40:23
I am developing a complex data structure in Clojure with multiple sub-structures. I know that I will want to extend this structure over time, and may at times want to change the internal structure without breaking different users of the data structure (for example I may want to change a vector into a hashmap, add some kind of indexing structure for performance reasons, or incorporate a Java type) My current thinking is: Define a protocol for the overall structure with various accessor methods Create a mini-library of functions that navigate the data structure e.g. (query-substructure-abc