The encapsulation tag has no usage guidance.
5
votes
3answers
301 views
Best practices for using public, protected, private?
Is it fair to say that it is good practice to default everything to private up front when coding something?
And then only upgrade it to protected if a subclass needs it, or public if another class ...
19
votes
5answers
3k views
Is it okay for a class to use its own public method?
Background
I currently have a situation where I have an object that is both transmitted and received by a device. This message has several constructs, as follows:
public void ReverseData()
public ...
237
votes
17answers
18k views
Why have private fields, isn't protected enough?
Is the visibility private of class fields/properties/attributes useful? In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and being able ...
1
vote
0answers
58 views
Data Mapper pattern vs. model encapsulation
I am currently developing my first custom data mapper layer but have run into a conceptual problem that seems to be glossed over all books and guides I have read.
My problem is as follows. My data ...
1
vote
0answers
99 views
Change object state in different point in program
This is not about whether or not getters/setters are wrong. I understand its impact to encapsulation and that question has been raised here and SO several times already. I also do not want to just ...
0
votes
1answer
89 views
Best practice for settings for PHP class for code generation [closed]
I would like to have a class creating any code from given options and output it in a chosen way.
I have written class for generation of simple JavaScript code for such cases where is needed to ...
2
votes
1answer
139 views
Javascript Closure Style Similar to Java Class Structure
PROBLEM: There is a coding imperative (S. McConnel, Code Complete) that one shouldn't code on language, but by means of it, e.g. doing right style things even if language doesn't have some ...
3
votes
1answer
144 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
316 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
89 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
90 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
112 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
113 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
103 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
151 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
152 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
354 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
312 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
51 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
235 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
111 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
46 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
301 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
283 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
186 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
178 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
656 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
849 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
410 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
386 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
122 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
133 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
134 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
477 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
216 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
154 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
454 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
2k 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
206 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
385 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
637 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
184 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
121 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
601 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
269 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
942 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 ...