2,060 questions
9
votes
2
answers
757
views
How to expose "private" fields to a Service class while adhering to Single Responsibility Principle
This is potentially an XY problem, but from my understanding of the SRP (Single Responsibility Principle) if you have an Order object, the properties should live on the Order, but methods like ...
0
votes
4
answers
160
views
How to encapsulate a for clause?
This is more like an exercise.
I have several conditions that affect 2 nested for clauses:
for(...)//1st
{
for(...)//2nd
{
...
}
}
and since there are various possibilities (each one ...
-3
votes
1
answer
114
views
How can I pass a reference to an internal class upon construction of a public-facing subclass without violating nullabe safety?
I'm not super experienced with C#, and this is a bit of a puzzle. Consider the following code:
#nullable enable
using System.Collections.Generic;
// Biology.dll
public abstract class Animal
{
...
0
votes
1
answer
107
views
How to access function members in a unit test without exposing them to the client? [duplicate]
I am working on a project in C++. I am writing several unit tests, and I have functions exposed publicly so I can access them in my unit test target executables, but I really don't want them exposed. ...
3
votes
1
answer
149
views
How to properly implement polymorphism with base class pointers in C++?
I'm learning Object-Oriented Programming in C++ and trying to understand polymorphism with base class pointers.
I created a base class Animal and a derived class Dog, both with a speak() method. I ...
3
votes
2
answers
115
views
How to instantiate a struct containing many private properties in a separate module without using a constructor function?
I want the user to be able to pass the struct along and instantiate it without a
constructor function, but not be able to modify the structure's fields without
using the setter methods provided in the ...
2
votes
1
answer
126
views
How to implement abstraction/encapsulation; like a generic/opaque struct that holds a platform specific window
typedef struct
{
WindowData data;
bool vsync;
} Window;
typedef struct
{
Window window;
GLFWwindow *native_window;
} WindowsWindow;
Window *windowCreate(const WindowProperties props,...
0
votes
1
answer
126
views
OOAD Head First --Encapsulation
public class Guitar
{
private String SerialNumber ;
private double Price;
private String Model;
private Type Type;
private Builder Builder;
private Wood BackWood;
private ...
1
vote
1
answer
53
views
Why do private fields not get overridden in subclasses?
I'm trying to create a parent class A with a private field #abc and a subclass B that overrides this field. However, when I call methods from the parent class that access the private field, it still ...
-7
votes
1
answer
115
views
Is making an enum public disrespecting encapsulation?
This is a question for an assignment for Uni. For this assignment we have to respect encapsulation and inheritance in Java.
The important classes to know right now are:
Cliente Class
import java.util....
-4
votes
1
answer
104
views
Pass a reference to a sensor or create the sensor object in the class [closed]
Note to SO. If I ask this question to AI I get a very similar output as the accepted reply. Without out all the questions about how I asked the question. Just sayin.
I am using this class for ...
1
vote
0
answers
101
views
How to convert between children types when inheriting privately
Consider this piece of C++ code:
template <typename T>
struct Base
{
// data members ...
// conversion constructors:
template <typename U>
Base(const Base<U>& ...
0
votes
1
answer
119
views
how to make a structure defined in a NVM AUTOSAR module invisible for the application in C?
There is a requirement in the AUTOSAR NVM module that says: [SWS_NvM_00135] The Administrative block shall be invisible for the application and is used exclusively by the NvM module for security and ...
2
votes
2
answers
130
views
Does shared objects breaks encapsulation?
The encapsulation principle says : "You should hide the attributes of your class and only make them accessible via methods. This guarantees the validity of class invariants."
I'm totally ...
0
votes
1
answer
187
views
Encapsulate third-party Service Provider Interface
I am trying to encapsulate a third-party library by writing a wrapper over it and using only required APIs as per my need.
To achieve this, I have created this wrapper project as a standalone project ...
3
votes
1
answer
101
views
Encapsulation of inline reified method in kotlin
I have written the util class to be used with Flow<> in kotlin. The goal of the class is to simplify error handling and make possible to instantly finish the flow by calling breakFlow() method. ...
0
votes
1
answer
29
views
To collaborate with programmers with some parts of the code invisible but executable to collaborators
Suppose a code consists of several parts/functions. How to manage a code and collaborate with programmers with some parts of the code invisible but executable to collaborators? To be more specific ...
2
votes
3
answers
258
views
How to have efficient separation of data and operation in javascript/typescript?
I learnt typescript in a hurry and still a newbie. Coming from a functional background I am used to seperate object data from its methods. For example let us consider a simple data object that i ...
0
votes
0
answers
32
views
Prevent Nuget package to be used outside of project [duplicate]
I have a C# project Wrapper.csproj that will act as a wrapper around the Nuget package Azure.Storage.Blobs.
Another project Hello.csproj will now reference Wrapper.csproj, so it can use the methods I ...
1
vote
1
answer
112
views
Cannot Find Symbol error - Java - Encapsulation
I have this folder structure
Day12
| Account.java
| AccountMain.java
Account.java Contains
package Day12;
public class Account {
int id;
String name;
double balance;
public int ...
-2
votes
2
answers
68
views
How can I determine when is more convenient to use static methods instead of instance ones? Encapsulation is the preferable choice?
I am basically writing static methods that perform work on a list of strings but I can't quite determine if it would be better to create instance variable and do all the work internally.
In case my ...
0
votes
1
answer
76
views
C++ protect getters/setters using final
Let's say I have a class that requires an encapsulated variable for conditional protection.
class person {
private:
int height_;
public:
explicit person(int height) {
this->...
-1
votes
1
answer
65
views
Abstraction vs Encapsulation , not the bookish definition , real world examples
I have searched about this topic everywhere that what's the difference between abstraction and encapsulation
, even one of the most voted stack overflow question , but at the end it seems confusing
...
1
vote
0
answers
91
views
Encapsulating styles in less causes problem in styles of react bootstrap modal
I am tackling quite a problem in the React app that I have not yet come to fully understand. What happens is that I have less files which I process with webpack and the styles/main.less is loaded in ...
0
votes
2
answers
358
views
TypeScript functions within functions vs. private functions [closed]
Here is a hypothetical example:
I have a market.service.ts with 2 functions called buy() and sell() which are each 100 lines of code
export class ShopService {
public async buy() {
// 100 lines ...
0
votes
4
answers
252
views
Encapsulation in DDD
When designing an aggregate and entities, it would be good to provide the consumer with only the public properties and methods of the aggregate. But it happens that in order to change some property, ...
-1
votes
1
answer
108
views
When is it bad convention to have a Java object visible outside of a root package? [closed]
I am building a CLI chess project (my first non-school project). I have a Board class in my GameLogic package which contains a 2D array with Piece objects to represent the board. I also have a ...
1
vote
0
answers
14
views
expose a 2d array immutably from a c struct
I read this:
https://stackoverflow.com/questions/46209696/how-to-use-2d-arrays-inside-a-c-struct
I did something like that in order to protect the internals of a "graph_t" structure. is ...
0
votes
1
answer
216
views
Proxy table to encapsulate an object in lua
Exercise 21.4: A variation of the dual representation is to implement objects using proxies (the section called “Tracking table accesses”). Each object is represented by an empty proxy table. An ...
0
votes
1
answer
58
views
Encapsulating member variables of a raw C# class that was marked as serializable
I have a raw C# class that is marked as [Serializable]. The fields are all public since the object is being saved to a file with Newtonsoft JSON.NET.
[Serializable]
public class ...
0
votes
0
answers
33
views
How can a static method be called dynamically from an instance method? [duplicate]
I have several classes, each of which should be able to retrieve an existing instance of itself (using an ID) when possible, rather than creating a new instance. For encapsulation and convenience, I ...
-2
votes
1
answer
358
views
Encapsulating the query logic in EF Core
Recently, I came across a blog post regarding the encapsulation of queries in EF Core. This approach is somewhat different from the repository pattern which is much more familiar to me.
In repository ...
0
votes
3
answers
167
views
How to properly implement getter/setter functionality, either for both a property's `get` and `set`, or for two methods and a property's `get`?
I have a logic that need to reactively run some function
if a this.variableName is changed inside or outside the class
the problem is with this logic, getter doesn't work, and I get undefined.
not ...
0
votes
1
answer
169
views
What is the reason to declare private type in a struct?
OCaml allows to mark type as private in the signature. And it makes sense. The object of type declared as private can be created only inside the module and be read outside.
module X : sig
type t = ...
3
votes
1
answer
82
views
Why can't match private tuples?
Ocaml provides three kinds of encapsulation for types:
abstract - when we can do nothing with an object of abstract type outside the module (can't read, create, update)
public - when we can do ...
0
votes
1
answer
117
views
Encapsulate interface implementation of property
I have an Object class that allows objects to be linked together.
A -> B -> C (one way linking)
When a link is formed between two objects by calling Link(IObject other), I want to set ...
1
vote
3
answers
89
views
Is creating an interface to a class a way to prevent to access the setters of this same class?
Let's say I have the class User with the attributes name, email etc.. In my code, I still want to be able to update that information, but I don't want anyone being able to reach the setters methods ...
0
votes
0
answers
33
views
Am I violating encapsulation by having unit test access path to sql database file and creating a connector to it?
I have a python class that manages initilization, read, and writes to a sql database:
class Database {
def __init__(self, path2db):
# path2db is created within this constructor
self....
1
vote
1
answer
35
views
Passing in private attributes into a utility function
I have 3 classes with a merge operation that does identical operations with different arguments.
Here's the skeleton code of what I currently do:
class FirstClass:
def __init__(self):
# define ...
0
votes
1
answer
22
views
Recommandation for OOP
im been learning coding but im kinda stuck at oop. It is hard to comprehend its concept for me. Especially when it comes to encapsuling. Do you know any source that can help me for full understanding ...
0
votes
1
answer
96
views
MediatR and Handlers
I have been rcently been advised to use MediatR in my API Controllers for encapsulation purposes.
I just want to know how does it know which handler to communicate with and when?
I checked the ...
-1
votes
1
answer
71
views
In Python ,why does it end up in recursion while accessing private attribute from getter() ,without using double underscore in getter()
class Person():
def __init__(self,name:str,height :float) -> None:
#initialize
self.__name=name
self.height=height
@property
def name(self) -> str:
...
1
vote
0
answers
317
views
How do I create a vector or std::array around an existing array? [duplicate]
Given a pointer to an array of N elements of char, I want to create an object which will encapsulate the array (it's address, length and type) giving me a read-only interface to it.
I have been using ...
0
votes
1
answer
100
views
Is it better to design a class with too many layers of encapsulation?
I am reading the book of CPP-Concurrency-In-Action-2ed-2019. In chapter 9.1.2, the author gives a simple example to show how to design a wrapper of type-erasure function:
#include <memory>
#...
0
votes
0
answers
90
views
How to encapsulate abstract class
I'm working with OpenTelemetry and trying to encapsulate the logic by internal library which means prevent using OpenTelemetry public API and spread logic between the projects, means there is only one ...
0
votes
1
answer
395
views
How do i remove the wrapper element added by Angular to a component?
I am making an Angular app + Tailwind that is one large horizontal page that allows the user to scroll from left to right using scroll-snap-type. When i try to cleanup my code and create reusable ...
0
votes
1
answer
47
views
Encapsulating Subtype A within Type B in C++
I'm working on a C++ project where I have a type hierarchy involving types A and B. I would like to achieve encapsulation such that type A is only visible within the context of type B and remains ...
0
votes
1
answer
109
views
Include GAS library as a source
I am trying to include the sources of BetterLog lib as a separate file, while keeping its referencing in the same as when added "as a library". How should I wrap it properly?
I've tried ...
-1
votes
1
answer
74
views
Player class returning null 0 null [closed]
I created a player with: Player player = new Player(hand, 5000, "Player_1");
When printing class it prints "null 0 null"
Why are the variables not being saved? Thanks!
Player class:...
0
votes
2
answers
161
views
I can not understand why encapsuling does not work in my code
I am doing a course on python classes as a hobby and the topic is encapsulation and class values. I am trying to understand getters and setters. I've read tens of different explanations and I feel I ...