The encapsulation tag has no usage guidance.
3
votes
1answer
115 views
Passing MVVM objects to other classes [closed]
So I'm using C# WPF and binding to a Model View to display states. The data that needs to be displayed is spread across a couple different classes. For instance I have a class that reads out digital ...
6
votes
4answers
296 views
Class design dilemma: Encapsulation vs Single Responsibility/Separation of Concerns
I'm working on a class that represents an object with multiple representations - one is an XML type representation used by an automatic ordering system, the other is a POJO-based representation used ...
1
vote
0answers
81 views
Having a globally static cached list without breaking encapsulation?
I have a couple wrappers around third party APIs that allow me to retrieve information from certain web services. Querying these services can be rather time intensive, so in my older version of my ...
1
vote
1answer
84 views
Setting up my inheritance model?
I'm working on refactoring a synchronization web app that our company uses. Currently it's merely a set of controllers that fire up a set of helpers that go through a long chain of if-else type ...
0
votes
1answer
109 views
Should I hold an Id and the Item [duplicate]
Let's say I have a Customer which contains an Address class like:
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; ...
3
votes
1answer
112 views
A condition on an argument used in multiple calls of a method: enforced by caller or by the method?
Let's assume I have an object a of a class A. It has a method that needs an argument in form of another object of a particular type - but the argument should also be in particular state, because the ...
1
vote
2answers
89 views
How to mitigate complexity of fallbacks introduced to automatically retry upon failure?
So this may come off a bit broad and generalized, but after spending some time working around under developers, I've come to notice many different methods and design patterns. However, one big thing ...
0
votes
3answers
162 views
Difference of efficiency [duplicate]
I'm doing a compiler, and I'm using the System.out.println(); to print assembly;
And the code get bigger, and more complicate to understand. I want to know whats is the difference of efficiency ...
0
votes
2answers
134 views
Meaning of using getters and setters and Uses of parameterized Constructor. [duplicate]
I am using getters and setters to for the purpose of encapsulation.
public class Student {
private String studentID;
private String studentName;
private String address;
public ...
4
votes
2answers
125 views
Does returning pointer to composed objects violate encapsulation
When I want to create an object which aggregates other objects, I find myself wanting to give access to the internal objects instead of revealing the interface to the internal objects with passthrough ...
6
votes
6answers
2k views
What is the usefulness of private variables? [duplicate]
While I've never used a language that had built-in variable privacy, a book I'm reading by Douglas Crockford explains a way to create privacy in JavaScript, however it doesn't make sense to me so. The ...
2
votes
2answers
300 views
How is encapsulation broken by getters/setters, even when using MVC model [duplicate]
I was thinking about encapsulation in Java and then I thought how getters/setters break encapsulation. After I went through this , I saw many recommendations like to avoid getters/setters on fields ...
3
votes
3answers
303 views
'Encapsulation Vs Performance' trade-off in a lockable linked list
Main purpose of encapsulation is to protect in-variants of any class(in java).
Here is the complete code for class DList/class lockDList/class DListNode/class LockDListNode.
class DList and it's ...
3
votes
1answer
37 views
Abstract/encapsulating session superglobal usage inside common accessor class
I am in the process of removing direct dependencies from an existing project. I have now come to removing the direct use of $_SESSION inside scripts. This is to make the same scripts available for an ...
4
votes
3answers
195 views
Nested POJOs, exposing collections, detecting changes
I have a couple plain object classes to represent data read from a file. They look something like this:
public class Command {
private String name;
private List<Message> messages;
}
...
1
vote
1answer
101 views
If the representation part of type abstraction is declared private, then how does sub-classing violate encapsulation?
Here, I learned that sub-classing violates encapsulation:
it violates encapsulation, since the implementations of the superclass and subclass become tightly coupled
For instance, If we consider ...
0
votes
0answers
40 views
Abstracting AutoLayout sensibly - why is this so conceptually tough and where should I begin?
Below are some babbling, probably incoherent thoughts, but I am so stuck on this problem and I feel like an elegant solution is possible.
Though it is indisputably awesome, and makes programatic view ...
1
vote
4answers
294 views
Why I need to make method of my class private, If I need to access it throughout my application?
I am going through the beginners tutorial for C# from a website
http://www.homeandlearn.co.uk/csharp/csharp_s10p1.html
On chapter of classes, it makes the method of class private and then use ...
0
votes
1answer
193 views
Access fields of super class from derived classes [duplicate]
There is an abstract BaseGame class that will be inherited by specific game classes.
This class has an attribute requestHandler that will be used in all subclasses and should be initialized in the ...
85
votes
15answers
8k views
TDD Red-Green-Refactor and if/how to test methods that become private
as far as I understand it, most people seem to agree that private methods should not be tested directly, but rather through whatever public methods call them. I can see their point, but I have some ...
4
votes
4answers
185 views
Design question concerning proper and practical encapsulation
I have been working on refactoring old code and found a lot of instances of the following type of situation: There is a master object which we call "Application" and there is only one of these. The ...
0
votes
1answer
173 views
How much to encapsulate when objects being dealt with are pretty similar
As I have been answering questions related to object-oriented design and how to go about reducing the amount of classes to make sure that there is not a lot of "class clutter" and code repetition I ...
1
vote
2answers
439 views
Why am I getting field visibility warnings in Sonar?
Some static analysis tools flag non-private fields with
Variable '[nameHere]' must be private and have accessor methods.
Sonar consistently presents such warnings and wants to change all ...
6
votes
3answers
835 views
Can “higher order function” feature allow/maintain abstraction and encapsulation?
Below is the function repeat written using a functional paradigm, such that when called as repeat(square, 2)(5) it will apply the square function 2 times on the number 5, something like ...
7
votes
5answers
402 views
Should private functions be held to the same standards as public functions?
If I'm building private utility functions, should they be held to the same rigorous standards in terms of handling invalid data as public functions?
Example:If I'm writing code to calculate the ...
4
votes
1answer
328 views
How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?
In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function:
...
0
votes
2answers
118 views
Should methods perform checks that they accomplished the task they were built for? or should they just throw an exception?
What are some pro's and con's of validating your performed the task intended?
public static bool UploadFile(string filename)
{
// 1. upload the file
// 2. check to see if the file now exist ...
4
votes
1answer
107 views
Encapsulation: Should all properties be private and protected?
After learning about encapsulation in OOP, I started to use only protected and private properties, share data via accessors and change properties via mutators.
Right now, I'm afraid of declaring ...
1
vote
2answers
121 views
Query on hiding implementation details in java
With the below piece of thread related code, I see that author of Thread class is hiding the details about the working of start() method. What a user of Thread class need to know is, class Thread ...
-2
votes
2answers
418 views
Why public access level method get overridden in java?
This question is raised with a clarification required to decide when to declare a method protected or public during class design of a package.
My point is, if one needs to override a method of a ...
2
votes
3answers
205 views
Getters and Setters unclear example
I know autmatic getters and setters are considers bad as they tend to break object's encapsulation. They also move the work that should have been done within the object outside.
Allen Holub is a big ...
-2
votes
1answer
133 views
Architecture - 3D Modeling Library
I'm writing a library to allow users to generate 3D models programatically.
User writes code (e.g. Square(4))
User views preview of 3D model (with built in visualizer)
User can "print" the model to ...
5
votes
1answer
430 views
How to store satellite data in C data structures
I've been reading through Introduction To Algorithms 3rd Ed, and I am having difficulty in implementing some practical situations. It's not the theory, or implementing the internals of the data ...
1
vote
1answer
291 views
Should ORM data access methods be wrapped or used directly?
We're using an internally-built ORM. Each table is represented by a Model class, which inherits from a base model class that has a handful of methods like GetAll, GetWhere (to get rows with specified ...
21
votes
6answers
1k views
Should I place functions that are only used in one other function, within that function?
Specifically, I'm writing in JavaScript.
Let's say my primary function is Function A. If Function A makes several calls to Function B, but Function B is not used anywhere else, then should I just ...
4
votes
1answer
197 views
Implementing a Username Class
I am attempting to encapsulate several features about a user in a single class. Although the main use for this class would be to initialize it once and never have to call set any variables again, I ...
5
votes
4answers
373 views
Which language introduced the idea of private (hidden) variables? [duplicate]
I know C++ had private variables inside classes, and Java, C#, Javascript, and others picked up on that and ran with it. Aside from variable scope, most languages before that just made everything ...
8
votes
2answers
597 views
Why shouldn't I make variables public, but should use public getters/setters? [duplicate]
I'm watching a C++ tutorial video. It is talking about variables inside classes and assert that variables should be marked private. It explains that if I want to use them publicly, I should do it ...
2
votes
0answers
165 views
Having trouble with exposing class properties across projects
I have several projects in my solution:
Logan.Web.DBContext defines the DbContext for the database and builds the data model
Logan.Web.Objects defines the classes that will represent the tables in ...
1
vote
1answer
110 views
Public versus private inheritance when some of the parent's methods need to be exposed?
Public inheritance means that all fields from the base class retain their declared visibility, while private means that they are forced to 'private' within the derived class's scope.
What should be ...
3
votes
1answer
506 views
How to maintain encapsulation with composition in C++?
I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
1
vote
5answers
265 views
“Default approach” when creating a class from scratch: getters for everything, or limited access?
Until recently I always had getters (and sometimes setters but not always) for all the fields in my class. It was my 'default': very automatic and I never doubted it. However recently some discussions ...
6
votes
5answers
861 views
Should I always encapsulate an internal data structure entirely?
Please consider this class:
class ClassA{
private Thing[] things; // stores data
// stuff omitted
public Thing[] getThings(){
return things;
}
}
This class exposes the ...
0
votes
4answers
242 views
Why encapsulate container variables? [duplicate]
I wasn't sure how to formulate the title, but there's something that's made little sense to me in OOP
I understand the idea of encapsulation, which is to create a layer of protection, and ...
2
votes
1answer
219 views
What's the right OO way to create a counter/inventory class that works for both differentiated and undifferentiated countables?
You are writing a videogame about trading beans. Red beans, black beans, pinto beans, you name it. As everybody knows all beans are the same. You write the "Inventory" class for a trader in that ...
2
votes
1answer
317 views
Should we encapsulate everything in a try{} block in a Try object?
Why can't I make a class for a Try including what I try and then run that in the try {} block? Why is it impractical?
class DBConnectTry extends Try {
TryResponse response[] attempt(TryObject ...
2
votes
5answers
344 views
Relative encapsulation design
Let's say I am doing a 2D application with the following design:
There is the Level object that manages the world, and there are world objects which are entities inside the Level object.
A world ...
0
votes
0answers
376 views
Use a global variable, a singleton, or something else
Preface: I am working in PHP (Abandon hope all ye who enter here).
Background: There exists a large set of global functions in PHP, a number of which are miscellaneous system calls, like sleep (and ...
3
votes
1answer
185 views
Should I assert in packages? [duplicate]
I have read a few asserts vs exception questions already; I ask specifically about package visible classes though.
When you write a package, the classes marked with "public" are exposed to the ...
3
votes
3answers
373 views
Abstraction concept in OOP
I hear that Abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden.
Encapsulation is then the technique for packaging ...