Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [object-oriented]

A methodology that enables a system to be modeled as a set of objects that can be controlled and manipulated in a modular manner

0
votes
0answers
14 views

What naming convention should you use for the 2 model classes of the same business logic, one shallow and one deep

In one of our services we have business objects, and sometimes we would like to return them in their "flat" form and sometimes in their hierarchical form. An example: Let's say we have an Account ...
1
vote
1answer
67 views

Ideas for designing a system for processing status updates [on hold]

Given that this question is related to my work, I will simplify what I need to do. The system consists of 4 primary types of statuses (internally, we call them events): Working, not operating ...
1
vote
1answer
81 views

User class: How do I handle different user “subjects” and still keep my single responsability principal?

I have a User class, this class handles things like: login (UserAuthenticateModel) Handle user sessions: user login tokens, fetching user from session if set in session, ect' (UserSessionsModel) ...
0
votes
1answer
78 views

What is the best way to handle the scenario in C++ where there are two methods that are identical, except one is const?

I am currently working on a program in which I encountered an issue (not for the first time) where I have two acessor methods for a data structure. The methods are identical, except one is const and ...
1
vote
1answer
57 views

ES6 - Where is it better to initialize a member - parent class or derived class?

I am trying to figure out if there is a better practice for initializing class members of derived classes in ES6 - in the child or the parent, and why? For example: Option 1: class AbstractAnimal {...
0
votes
0answers
21 views

Move operator for class that has a member with deleted move operator [migrated]

Class X has a deleted operator= X& operator=(X const&) = delete; Class Y looks like this: class Y { private: X variableX; public: Y(int p) { variableX= X(p); } } I'm ...
0
votes
0answers
38 views

Separate Class for Current User vs List of Users?

Suppose I have system with a User class, meant to represent one row in a database table of users of the system. There will be queries for the administrator's screen reading a list of all users, in ...
4
votes
2answers
212 views

Doesn't repository pattern in clean architecture violate Dependency inversion principle?

From what I have read and seen clean architecture assumes that you have some entities which know nothing about persistence though at the same layer as they reside there might be an interface which has ...
2
votes
1answer
112 views

Approach for implementing Device and Protocol layers in C++?

I'm writing a program that will interface with an external device. It will support numerous devices that may use different communication interfaces like USB, serial, etc. This is what I have so far: ...
2
votes
1answer
60 views

Angular class design dilemma

I'm trying to set up a good class diagram for my Angular application, however I have a problem figuring out how to solve the following situation: I created a base Link class that can be used anywhere ...
1
vote
1answer
83 views

UML - Identifying use cases and actors

I'm currently learning object-oriented programming and we have moved on to UML. We are learning about use cases and actors currently and we need to create a list of actors and use cases initiated by ...
2
votes
2answers
56 views

Swappable state object or decoupling data and functions

I come from OOP pradigm and I also know a bit about functional programming and its advantages. Over time I came to like the separation of data and transformations that are applied to it using pure ...
5
votes
1answer
109 views

Working with “external” object IDs

I have often encountered a situation in which users of a program (not necessarily human, just an external agent) need to be able to reference objects of a class with a certain "external" identifier, ...
10
votes
6answers
896 views

Is separating most classes into data field only class and method only classes (if possible) a good or an anti-pattern?

For example, a class usually have class members and methods, eg: public class Cat{ private String name; private int weight; private Image image; public void printInfo(){ ...
2
votes
1answer
74 views

Should I group related data fields and views together, or group data fields into a DataClass (as well as group views into a ViewsClass)?

For example, if I have a game which have some components, which has Player and Enemy,as well as the parent container,Game, e.g.: Version 1: public class Player{ private int hp; private ...
0
votes
1answer
103 views

How to bring a code to conformance with tell, don't ask without creating tons of methods on other classes?

It used to be fairly common for people to call a getter, do some calculation on it, then call a setter with the result. This is a clear sign your calculation actually belongs to the class you called ...
1
vote
2answers
82 views

Transforming one collection of objects into a separate unrelated collection?

Say I have a customer class class Customer(name, age) somewhere in my code, I have a list of customers List[Customers] at some point, I need to transform this list of customers into a Map with ...
0
votes
3answers
102 views

Designing a class whose dependancies require “starting”/initializing step

I have Facade class which requires an Endpoint instance to function (it uses it in most of the functions). Endpoint is attached to Platform that is passed in Endpoint's constructor and has to be ...
0
votes
1answer
52 views

Objectreference vs Equality [closed]

While coding I stumbled upon something like this (extremely simplified example): public bool Func() { Object[] array = new Object[] {false, false}; return array[0] != array[1]; } I was ...
0
votes
7answers
297 views

Good object oriented practice to use an object to build itself?

Do you think its a good OO practice to use parts of an object to build itself? Here is an example. var car = new Car { Make = BuildMake(), Model = BuildModel(), Vin = ...
0
votes
3answers
192 views

Responsibility of a Factory

I am trying to refactor a huge chunk of our software, and it requires to set up a factory. However, I am not sure where lies its responsibility. (PS: The code below is shown simply as an illustration ...
3
votes
3answers
87 views

Open Close Principle is applicable when requirement is to add new operation(s) to existing type?

I understand OCP in SOLID can be applied when we have same set of interfaces defined via abstract type, so that we can have varying implementations of those interfaces via concrete types. For example,...
11
votes
6answers
517 views

DDD meets OOP: How to implement an object-oriented repository?

A typical implementation of a DDD repository doesn't look very OO, for example a save() method: package com.example.domain; public class Product { /* public attributes for brevity */ public ...
1
vote
1answer
101 views

Chain accessors or return the value of private pointer

I am having trouble thinking through what best-practice dictate I do in this situation. Say I have the following class structure Class A { public: bool isCompressed() { return compressed_; } ...
2
votes
3answers
306 views

Confusion on Single Responsibility Principle (SRP) with modem example?

I am currently reading through Robert Martins book "Agile Software Development" book and I am struggling to see how his modem example provides any benefit. He says that he has an interface that ...
2
votes
1answer
307 views

Is 'Keep it looks similar to sample code' a valid reason to prefer WET over DRY principle?

For example, for oauth, I may need to copy and paste oauth and oauth callback code like it (assume Client is the framework I use, I don't use real framework like google because I want to simplify the ...
1
vote
1answer
61 views

Check if object exists before updating/deleting

In an application that doesn't use an ORM, is it good practice to check whether an object exists before attempting to update/delete it? For example: @Route(method="DELETE", path="/posts/\d+") ...
1
vote
1answer
55 views

Dynamic Object Abilities When Saving State

I have n classes that extends from a State class, the purpose of the State class is to manage the state of the extended classes. For each class we need to save it in the database, remove it and select ...
0
votes
3answers
81 views

How to “open interfaces to expansion” without breaking existing implementations?

For example, I have an interface Requests that basically lists all the requests a client can make to a server program. Now an obvious problem arises where I have to add a new request to the program i....
3
votes
2answers
94 views

How to create a tree-hierarchical structure with unique behavior for every type combination?

Assume: There is some module whose interface is IA. There is some module B, that takes in a parameter an instance of IA, and whose behavior depends upon the type of that IA, meaning (pseudo code, no ...
2
votes
6answers
764 views

Should I still prefer composition over inheritance if the child classes need BOTH the parent interface AND its class properties?

According to Why should I prefer composition over inheritance?, I should prefer composition over inheritance. But what if I need to access the interface and class member in generic way? For example, I ...
4
votes
1answer
193 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: ...
1
vote
1answer
139 views

Building a MVC Framework: Should I get an instance of Database, or should I extend with a “general” model class that already has an instance of DB

I am trying to get a database instance via any model-class that uses the database. This is ideal in my opinion: class UserAuthenticator { private $db; private $customer; public ...
1
vote
1answer
140 views

use always get and set methods is a bad practique, is call directly an attribute class a bad practique in OOP?

I have a doubt about if there are some recommendations for call directly an attribute in a class I think that in OOP you always should call an attribute by the get method. For example: On set ...
0
votes
0answers
54 views

Apply Value Object (Stringly Typed) in PHP

What is the best way to prevent wrong states in an object? This is the source of my question. Basically my curiosity started with the intention of not letting a class exist with wrong values. Prevent ...
0
votes
3answers
224 views

Should a method decide whether to execute or not? [closed]

I couldn't find a question on SE but it has probably been asked elsewhere already (in case, please mark it). A method containing code to run just with specific external conditions can: decide ...
0
votes
1answer
92 views

Should I “Connect->To controller A”, or “To controller A->Connect”?

For example, suppose I have a shopping app, which has a user info page to show user info: class UserInfoController{ toPurchaseRecord(){ } } and a purchase record page to show purchase record,...
0
votes
2answers
84 views

Object composition in a static function

I have a class that provides basic functionality of a modal window. For different situations I need to provide different modals(different number of buttons, titles, etc...). I have a kind of factory ...
42
votes
9answers
11k views

Is “avoid the yo-yo problem” a valid reason to allow the “primitive obsession”?

According to When is primitive obsession not a code smell?, I should create a ZipCode object to represent a zip code instead of a String object. However, in my experience, I prefer to see public ...
11
votes
5answers
894 views

What is a good design practice to avoid asking a subclass type?

I've read that when your program needs to know what class an object is, usually indicates a design flaw so I want to know what is a good practice to handle this. I'm implementing a class Shape with ...
0
votes
3answers
218 views

Is it sometimes okay to intentionally violate the Dependency Inversion Principle?

I am doing some work to refactor a class. It currently a 'God class' and contains all different logic/operations solely in that class. One of my solutions is to extract all different parts of logic to ...
-2
votes
1answer
65 views

method taking a class parameter

I have recently begun studying UML. All is going fine so far until I saw the following: This is a class Called Point2D It has 2 attributes which are x, type float and y, type float. It has 3 methods ...
5
votes
1answer
392 views

How common are destructors in modern c++ code?

In the standard c++ library, all containers and all input/output streams have their own constructors and destructors, that handle all the relevant resource acquisition and release. So for most tasks ...
9
votes
4answers
821 views

Programming style in Perl

I work in Java so basically I use OOP paradigm during coding. I am about to start working in Perl and I was wondering what is the paradigm that Perl developers follow. In wiki it mentions that it ...
-2
votes
1answer
49 views

Using static member methods to check for object being NULL

Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse. Something like, #include <...
0
votes
0answers
111 views

Decorator Pattern Java

This is an object-oriented design question that is specific to Spring Boot. I'm extending a Spring Boot application that has an interface that is being extended and used inside another service. The ...
2
votes
5answers
210 views

DDD/OOP - saving Aggregates without ORM. Public getter, reflection, or injecting repository?

So for DDD folks there, Aggregate Roots are supposed to contain business logics only and exposed what is needed only. In DDD Red Book by Vaugh Vernon, he used LevelDB and Hibernate as examples. ...
3
votes
4answers
9k views

Object oriented immutability: final classes or final methods

One of the things you see in numerous places in the standard java library is final classes. It is claimed that this is for immutability which I understand...to an extent. Suppose you have a class: ...
3
votes
1answer
275 views

Read Only Generic data access layer Best practice

I am trying to write some art of "generic" data access library to access the data of my company's ERP Software, which is our main/core application where all our related data is managed. I am a ...
5
votes
5answers
573 views

Is it feasible to use User Stories and Use Cases in the same team?

I am trying to encourage working practices that are more agile. I am trying to understand the difference between a Use Case and a User Story. I have read a lot of articles and questions like What'...