Questions tagged [encapsulation]

Encapsulation is the principle of bundling software elements in a way to hide implementation details and to expose only a known interface.

Filter by
Sorted by
Tagged with
0 votes
2 answers
294 views

Recommended way of hiding implementation details?

I have a single *.h file. This file contains a single (more to come) function declaration. Now the implementation of that file is very complex. the corresponding *.cpp contains several function ...
user avatar
3 votes
5 answers
275 views

Add specific behavior inside classes

I'm learning to make games with OOP and there's something I don't understand. What I can see is that the more I add methods to a class, the less it becomes reusable and flexible. For example, if we ...
user avatar
0 votes
1 answer
142 views

How far can one debug a low-level API in closed-source environments?

Assume a low-level API is provided without source code (e.g. DirectX). The API provides a virtualization of hardware resources (GPU, CPU, audio card, etc.), which enables the user to call hardware-...
user avatar
4 votes
3 answers
460 views

Are there any legitimate use cases for protected visibility?

Protected visibility in languages like C++, Java or PHP is a strange beast: it makes fields and methods accessible in subclasses, but not in code completely outside the class. It strikes me as ...
user avatar
2 votes
3 answers
348 views

How do you achieve encapsulation while "separating view and business logic"?

I've been trying to get a better understanding of OOP (I'm not the biggest fan of it, but I still want to understand it). One of the core principles of OOP is encapsulation - you're supposed to ...
user avatar
0 votes
1 answer
246 views

Is this dependency propogation an anti-pattern, and how do I remove it?

Module A contains a gap buffer for manipulating text and some associated methods. Relevant to this question is the dependency on a Module B, used for syntax highlighting of text. Module A also ...
user avatar
  • 281
1 vote
1 answer
122 views

How to test functionality that requires a certain internal state?

I'm struggling to test functionality in a class where the class has to be in a certain state for the functionality to work, but the class cannot be put directly into a given state by design, to ...
user avatar
  • 127
0 votes
2 answers
68 views

Restructuring a “processing slip” like object used in many steps

I have worked on a library which processed data through multiple steps. It was written in R, a dynamic programming language where one could just add fields to existing “objects”, which were just ...
user avatar
1 vote
1 answer
52 views

Guidelines for using extensions in Swift

The other day, I came across this question on StackOverflow. In short, the user who asked the question wanted to extend a class from a third-party library to implement the Codable protocol, but ...
user avatar
  • 142
-3 votes
2 answers
87 views

What design pattern / class / interface should I use for encapsulating a program? [closed]

I am building a chess - related application, and I want to use a pre-compiled program called Stockfish as my chess engine. I am wondering what is the best practice to encapsulate the usage of the ...
user avatar
  • 107
0 votes
2 answers
64 views

Where to put files (interfaces) that link two independent assemblies: Authentication and Main Program

I'm having an issue with dependencies in a C# app that I'm creating. I have an assembly for my authentication process, and a separate assembly for starting up the main program once authentication is ...
user avatar
  • 1,476
0 votes
3 answers
223 views

Does encapsulation in OOP happen at run time or compile time?

Encapsulation is the hiding of an implementation in OOP the way I understand it. I searched on Google and I was thinking of trying to write a program to test, but my tests would only happen at run ...
user avatar
4 votes
4 answers
401 views

How name public method that relays to abstract methods of its children (c#)

I've run into the following situation multiple times, and in every case have struggled with the naming. I want a class to force its children to implement a method, but I want the parent class to be ...
user avatar
  • 1,476
1 vote
1 answer
338 views

How to separate public and private headers when internal data structures are private?

When developing a shared library in C, it is common to separate the "public" headers from the "private" headers. The public headers contain all the functions and types that are ...
user avatar
1 vote
2 answers
124 views

How does one choose where to place logic which converts data between two formats/representations?

Suppose I have two "modules", A, and B (I'm choosing not to use classes, because you generally can't create an instance of a module, which makes this question simpler). These modules contain ...
user avatar
0 votes
1 answer
92 views

Could a getState() method in a Caretaker violates Memento Pattern?

I have looked at several examples for implementations of Momento on the web. I wonder if it is correct to retrieve the status of Originator, since this is much more accurate, up-to-date and reliable? ...
user avatar
  • 243
1 vote
2 answers
299 views

Are multiple dynamic dispatch methods possible?

C++ only supports single dynamic dispatch methods. Indeed, the following program: #include <iostream> struct Shape { virtual void overlap(Shape* y) { std::cout << "Shape, Shape\n&...
user avatar
  • 570
3 votes
3 answers
954 views

Encapsulation violation

I was going through a question and here the answer says that Encapsulation is being violated class car { int speed; public : int* getSpeed() { return &speed; } }; int ...
user avatar
  • 149
0 votes
0 answers
41 views

Use debug-functionality without breaking architecture

note: This is of couse about software-architecture/design-principles, but as no architecture is completely detached from its language, please note that the language i am using is C++. I am using an ...
user avatar
1 vote
1 answer
178 views

Modular programming with C: Separate device logic from communication protocol logic

I have no experience with C (only C++ and higher level languages). Right now I have tried and failed to find general guidelines on how to write good C code in a way that allows to separate the ...
user avatar
0 votes
2 answers
667 views

Should I use a class with only static members to encapsulate my program?

So I'm writing a network simulator in C++ as part of a university project. Right now, I have a class structure that looks something like: //includes type aliases #include "GlobalTypes.h" //main body ...
user avatar
0 votes
2 answers
87 views

Pros and cons of different implementations wrt encapsulation

I have an application. It needs to send emails. We've all been there, done that, got the t-shirt. I've got an IEmailMessage interface: public interface IEmailMessage { string From { get; } ...
user avatar
  • 310
1 vote
1 answer
235 views

State Machine: what object is responsible for state transfer?

I would like to create a state machine. Each State would have its run method, and, according to some logic would then set a next state. Option 1: If each state is responsible for determining a next ...
user avatar
  • 1,131
0 votes
4 answers
338 views

Open closed principle: code duplication and encapsulation

In another question on this site, asking to clarify the open closed principle, @Kate Gregory answered this. I'm interested in this part specifically: Imagine you wrote an Invoice class that works ...
user avatar
2 votes
5 answers
308 views

Splitting class responsibilities without exposing private data

I'm struggling to find good ways to split up classes without exposing private data. Most articles I read about SRP seem to ignore how the new classes that take on the separated responsibilities access ...
user avatar
36 votes
7 answers
9k 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 ...
user avatar
13 votes
3 answers
6k 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,...
user avatar
  • 909
2 votes
4 answers
1k 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 ...
user avatar
4 votes
2 answers
1k 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 ...
user avatar
1 vote
2 answers
839 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 ...
user avatar
  • 873
9 votes
4 answers
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; } ...
user avatar
-2 votes
2 answers
188 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 ...
user avatar
0 votes
1 answer
93 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 #...
user avatar
1 vote
4 answers
179 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, ...
user avatar
  • 153
0 votes
4 answers
304 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 ...
user avatar
  • 396
1 vote
4 answers
332 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 ...
user avatar
  • 43
3 votes
4 answers
2k 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 ...
user avatar
3 votes
6 answers
1k 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 { ...
user avatar
  • 396
4 votes
1 answer
219 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: ...
user avatar
1 vote
4 answers
584 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 ...
user avatar
-1 votes
3 answers
325 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 ...
user avatar
  • 264
13 votes
7 answers
8k 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] +...
user avatar
  • 273
13 votes
1 answer
3k 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; ...
user avatar
-2 votes
1 answer
91 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 ...
user avatar
  • 117
2 votes
3 answers
312 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 - ...
user avatar
  • 189
8 votes
7 answers
3k 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" ...
user avatar
2 votes
1 answer
210 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 { ...
user avatar
  • 43
9 votes
5 answers
2k 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 ...
user avatar
0 votes
1 answer
79 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 ...
user avatar
1 vote
1 answer
180 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 ...
user avatar
  • 181