Questions tagged [encapsulation]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
21
votes
5answers
4k views

Do you need to think about encapsulation if you can ensure immutability?

Encapsulation In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an ...
13
votes
2answers
5k views

How does encapsulation actually work?

I made the following diagram to show a typical separation of concerns as typically taught - Here ClassA indirectly uses ClassB via the ISomeInterface, of course ensuring it doesn't know ClassB exists,...
1
vote
4answers
153 views

Separating data and behavior in Java

I am in the process of refining my code to adhere more to my current understanding of the Single Responsibility Principle (SRP). Originally, I had a class called Animal that had a set of methods and ...
4
votes
2answers
539 views

Is there anything wrong with writing getter/setter methods in C#? [duplicate]

I am a Java dev for almost all of my programming (at least in the workplace) but I do some Unity for fun on the side. I have used C# properties many times and they are convenient to still provide ...
1
vote
2answers
240 views

Clean Code: Avoiding “Order Matters” without passing member variables to private methods?

I am trying to implement some of the principles laid out in Clean Code by Robert C. Martin. I had a class that was heavily suffering from the ordering problem. I have solved most of this by ...
9
votes
4answers
1k views

Can renaming a method preserve encapsulation?

I was reading this page, about when getters/setters are justified, and the OP gave the following code sample: class Fridge { int cheese; void set_cheese(int _cheese) { cheese = _cheese; } ...
-2
votes
2answers
104 views

C# Static & Constructor Logic [closed]

I learn C# and try to understand the logic between static and Constructor right now. One thing I need ask you about an example which I will give at below. (please ignore the quality of code or how ...
0
votes
1answer
85 views

Sharing #define's between higher and lower layer. Where should shared types go? [closed]

Let's say I have a stacked 2 layer app (High Layer (HL) and Low Layer (LL)) that is implemented in C. HL defines a few #defines. HL calls a LL function with a parameter that takes values of the #...
1
vote
4answers
120 views

Can encapsulation/information hiding cause problems in error identifying/locating?

I understand the principles behind opaque data types, encapsulation, information hiding, etc. in theory: preventing alteration to parts of a program can prevent people from accidentally messing it up, ...
0
votes
4answers
201 views

methods that has only behavior and not manage class state, is that violation of encapsulation

This is a simple scenario in an office with employees and their manager. In this scenario, both managers and employees have a name attribute. Only the manager can change his own name Only the ...
1
vote
4answers
297 views

What's the purpose of explicit encapsulation?

Assume you have a library in which every function is public. Sooner or later developers who use your library will come up with a stable pattern of usage. In that terms external information of how ...
3
votes
4answers
1k views

Should a class provide public mutators for all its private fields?

I work on refactoring an Java application based on a CAST audit. One of the criterion says that To respect OO encapsulation concepts, private fields should always be accessed through accessors ...
3
votes
6answers
348 views

The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?

This question is related to How should an `Employee` class be designed? In the above question, to uniquely identify an employee, each Employee object has an id field as shown below class Employee { ...
4
votes
1answer
210 views

Is using getters to exchange information between objects acceptable?

Suppose I have the following Character, Potion, and PotionType classes: class Player: def __init__(self, name: str, health: int, mana: int): self._name = name self._attributes: ...
2
votes
4answers
414 views

Can method names give any implementation details and break encapsulation?

I was reading here about OOP and methods, and the accepted answer states that method names should be verbs. However, that doesn't really answer my question. Suppose if I had a Character class with a ...
0
votes
1answer
149 views

restricting access to a public setter

My question is how I can achieve more encapsulation in TypeScript. I have a class Item, with a public setter isOwned, but I only want to call this method in specific situations: if the item is picked ...
14
votes
7answers
7k views

Is it a bad idea have make a class method that is passed class variables?

Here's what I mean: class MyClass { int arr1[100]; int arr2[100]; int len = 100; void add(int* x1, int* x2, int size) { for (int i = 0; i < size; i++) { x1[i] +...
12
votes
1answer
2k views

Using friend classes to encapsulate private member functions in C++ - good practice or abuse?

So I noticed it's possible to avoid putting private functions in headers by doing something like this: // In file pred_list.h: class PredicateList { int somePrivateField; ...
-2
votes
1answer
74 views

looking to solidify understanding of OOP principles

I just wanted some feedback on my definitions of these terms. Abstraction - only showing relevant information and hiding implementation details. Interfaces and abstract classes are an examples of ...
2
votes
3answers
195 views

Wrapping Object VS Copying Fields

Almost every software has a number of internal data structures that we don't want to expose externally (through APIs for example). Let's say we have a few classes representing 1:1 database tables - ...
9
votes
7answers
1k views

Separation of concerns: When is it “too much” separation?

I really love clean code and I always want to code my code in the best possible way. But there was always one thing, I didn't really understand: When is it too much of "separation of concerns" ...
2
votes
1answer
153 views

OOP Encapsulation Philosophy

Is it fair to say that it is good practice to default everything to private up when defining a class? For example, for my public interface I would set my class something like this: class foo { ...
7
votes
5answers
1k views

C# encapsulate field is a violation of YAGNI

Consider these two ways to declare a field in a C# class option A public class AuditController { public DataAccess Service; } option B public class AuditController { private DataAccess ...
0
votes
1answer
66 views

API DESIGN: Store Table of function pointers in client or dynamically in api service

FIRST AND FOREMOST: Forgive me if the following question falls into the "off topic" category of this StackExchange, due to its slightly opinion polling nature. This is not my intention, I am simply ...
1
vote
1answer
88 views

Ensure encapsulation and Open/Closed principle with Command pattern?

I'm developing a small multiplayer game. It'll be served by one websockets server and consumed by multiple consumers. As such I need to be cautious about concurrency errors. The general software ...
5
votes
5answers
368 views

A problem of decoupling

This is a simplified version of a problem I'm facing in my current project. Let's say we want to display shapes: rectangles, circles, etc. I'll have a Shape class which is subclassed by Rectangle, ...
3
votes
4answers
153 views

What is the best practice for a regression test that wants to know about encapsulated information?

Suppose you have two classes: Alpha, and Bravo. Alpha constructs a new Bravo class during its own constructor and exposes no public visibility to it. Bravo has a String named charlie (and a method to ...
2
votes
4answers
568 views

Use of static methods to implement logic within a class

I've recently caught an odd behaviour in my own coding: At some point in the last year I began creating protected static methods to implement the logic of my public methods, ensuring that the ...
-1
votes
1answer
191 views

OO Class Design for ATM card

I have to design a Class for ATM cards with the following attributes: Cardholder Name Card Number Pin Other than getters and setters, I have a method to validate the pin of the ATM. As per the ...
0
votes
1answer
531 views

Should we use guard clauses in a method using auto validated properties of an object?

Let's say I have a class like this: public class MyClass { public string A { get; } public string B { get; } public string C { get; } public MyClass(string a, string b, string c) ...
1
vote
1answer
246 views

Rest api manager encapsulation required or not

I am currently designing a software in which I have to use a rest client. I have decided to use a particular library, but am stuck whether I should use it directly or add a custom encapsulation layer. ...
6
votes
6answers
3k views

Is it appropriate to process a property in the setter?

I have a class with a Name property, where the name is set by the end-user. In order to ensure compatibility with the rest of the environment, and to avoid rejecting a lot of inputs, some processing ...
1
vote
3answers
745 views

Is the Liskov Substitution Principle concerned with protecting state invariants?

Wiki says: Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (...
1
vote
1answer
207 views

Encapsulation and SRP

Suppose I have the following requirements: We organize books by author first name, this might change in the future. When ever an Author we carry at our store releases a new book, the Authors previous ...
1
vote
0answers
144 views

How to create an “internal” api for my library which is not exposed to the user?

Note that my question is not platform specific so that's why I don't add the [kotlin] tag (the code is in Kotlin). If there is a Kotlin-specific answer I'd like to hear about it though I am working ...
0
votes
1answer
81 views

Store reference to object or specific functions [closed]

Having single (or few) method(s) interface constructor injection should I store in a field a reference to the interface instance passed as is or just necessary "entry point(s)"? class Client { ...
3
votes
2answers
627 views

Dealing with a lot of getters and setters

I've already asked "Dealing with a large interface". I have a further question regarding that situation. It was pointed out to me that I used a lot of getters and setters and so I broke encapsulation....
1
vote
2answers
2k views

Are service objects necessary in OOP?

I have been using AngularJs since a long time. Me and my team makes heavy use of services for retrieving the remote resources like Users which intern uses $http service so, basically, for each entity, ...
7
votes
4answers
450 views

Encapsulation and Displaying Information

This site and SO contain many pages about getters/setters and if they break encapsulation or enforce it. My question is for those developers that agree that getters/setters break encapsulation and ...
2
votes
2answers
142 views

Implementation wise fields per object in a class composing a collection of objects

Context When implementing a class, there's a distinction between fields that are there because they are part of what the object is and fields that are there because they are needed to implement the ...
4
votes
4answers
793 views

How exactly are getters and setters defined?

Note: Questions with similar title have been asked before, but please read the full text before claiming that this is a duplicate. Since everybody in OOP uses the terms getter and setter, I would ...
6
votes
10answers
2k views

Why combining getters, setters and private access modifier is not enough to hide implementation?

In Clean Code: public class Point { public double x; public double y; } The author wrote about the above Point() class: This exposes implementation. Indeed, it would expose implementation ...
5
votes
1answer
223 views

In more canonical OO Python situations, what is the rule of thumb for default access modifiers?

Generally speaking in canonical OOP situations, the rule of thumb is to write your classes with the least access as necessary. i.e. only make public only what is necessary, make protected only what is ...
1
vote
2answers
223 views

Does changing the private member of a class through an overloaded subscript operator break encapsulation?

I have always been taught that in order to in-keep with encapsulation objects should be responsible for performing calculations on their own data. So in this case here is this breaking rules of ...
3
votes
1answer
222 views

Is it always optimal to preserve encapsulation in MVC?

I've just finished up a project in which I created a visual simulation of the life cycle of an ARM instruction in a single cycle processor. I used the MVC pattern in this project and ran into a design ...
-2
votes
2answers
459 views

Is 'List' a leaky abstraction?

Abstraction - Creates barrier between representation & usage of List ADT Encapsulation - Ensures maintaining in-variants of List ADT. An invariant is a fact about a data structure that is ...
25
votes
4answers
6k views

What does it mean when one says “Encapsulate what varies”?

One of the OOP principles I came across is: -Encapsulate what varies. I understand what the literal meaning of the phrase is i.e. hide what varies. However, I don't know how exactly would it ...
0
votes
2answers
71 views

Use XmlDeserializer in factory method?

I have the following class representing an XML configuration stored in the DB: using System.IO; using System.Xml; using System.Xml.Serialization; [XmlRoot("ModalConfiguration")] public class ...
2
votes
1answer
889 views

Do singletons use encapsulation?

I'm reading the Design patterns book by the Gang of 4. Does a singleton use encapsulation? To me it would seem like yes, but i'm not 100% sure. Maybe I don't have a good understanding of ...
4
votes
1answer
191 views

Does InternalsVisibleToAttribute indicate a bad design smell?

Every time I have used the InternalsVisibleToAttribute when developing an API, it feels a bit smelly. Like the API is not being designed in a truly encapsulated way. A lot of the times this is out of ...