Questions tagged [object-oriented-design]
Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.
1,683
questions
0
votes
0
answers
38
views
Should I separate algorithms that rely on a data structure into different classes
I'm learning about graph algorithms, so to learn them better I'm beginning to write some of them using c++. For example, I created the code for implementing the Depth First Search algorithm. Here's ...
0
votes
1
answer
26
views
Multiple objects using the same connection for communication, filter messages "above" or inside the objects?
I have a single connection being used by multiple devices, and I'd like to filter the messages so that each device only reacts to messages intended for it. This can be done with an unique ID field in ...
0
votes
2
answers
69
views
How to structure a cart with cart products object
I am new to DDD and I am having trouble structuring an object. I have a cart, where I can add products to it. Each product has some properties like image, title, price, etc. Whenever a product is ...
0
votes
1
answer
65
views
REST - Adding a new field
I have an object with fields like ID, CODE, NAME, etc., and it has its own database table with each field. And I am working on implementing the REST API for this object. I want to expose all these ...
66
votes
8
answers
13k
views
When to *not* use SOLID principles
Coming from a math background, counterexamples are equally, if not more, helpful to me for understanding concepts than examples. I've seen many, many examples of when and how to use the SOLID ...
1
vote
6
answers
313
views
How encapsulating what varies can help us?
I have a question about encapsulation and I read these two topic (this & this) but I got more confused.
I've been reading Head First Object-Oriented Analysis and Design book and I'm trying to ...
0
votes
0
answers
43
views
How to handle multiple-state domain object?
Let's take an example of Order.
An order can have the state new, canceled, shipped, or processing.
The new order can be processed.
The new order can be canceled.
The processing order can be shipped.
...
0
votes
2
answers
90
views
Best way of creating different instance types(facotry method) with business logic?
Say I am trying instantiate an instance of a Table. Table here denotes a restaurant table used for eating. Say I have 3 different table types
enum TableTypes {
SMALL,
MED,
LARGE
}
I have ...
2
votes
3
answers
168
views
Class design using Open and Close Methods
As the title says, Im thinking if it's a good practise to have classes that have Open and Close methods in the sense that they can be reused without a new instance being created again.
Also, if I want ...
2
votes
1
answer
185
views
Domain models: Can they be an abstract class
When talking about having a rich domain model successfully, in real applications, it needs to, somehow, access some abstraction of complex functionality (instead of being a row state calculator the ...
0
votes
1
answer
137
views
Some approach or Design Pattern to implement same method different parameters of Interface class
I am using the Flyweight pattern to cache and reuse objects of the different classes. For example, I have a Shape interface class and multiple types of Shapes implementing the methods from the ...
-2
votes
3
answers
115
views
What are the pros and cons of different combinations of objects and arrays for data storing?
From the article JavaScript 2D Array – Two Dimensional Arrays in JS, I see one way to store data is to put all properties of each element into one array:
let dataRepresentation1 = [
['John Doe', ...
1
vote
2
answers
173
views
Is there a name for this pattern of sealing base classes to a strict set?
In C#, it's possible to create a strictly sealed set of classes that inherit from a base that cannot be further extended like so:
public abstract record MyBase
{
public sealed record A: MyBase;
...
-1
votes
1
answer
160
views
Help in understanding if my design getting complicated [closed]
I have 3 classes
class Backup
end
class Database
end
Class App
end
The backup database has a reference to Database and App, like
class Backup
def getDatabase
Database.create
end
def ...
2
votes
3
answers
329
views
Is having many build-once factories a sign of bad dependency injection design?
I have a form. It contains things like grids. Users do things with these grids and what they do with them is sent to a SQL server. Clearly, the server is a volatile dependency and should be dependency ...
-1
votes
2
answers
76
views
Is there any guideline on how to choose the objects to be modelled? [closed]
From my conversation with @Steve in the question Is there any philosophical theory behind the concept of object in computer science? on Philosophy Stack Exchange:
Steve: More often than not, ...
2
votes
2
answers
171
views
Data getter inside Model or ViewModel in MVVM
I'm working on an app I created for myself using the MVVM architecture. On my ViewModel I have a list_items that, when changed, updates the corresponding UI element on the View. The list of items ...
-1
votes
1
answer
138
views
Using a shared enum across 15 nanoservices [closed]
Originally posted here, moved to code review, redirected from code review back here as there is no code to review. I think the question is enough abstract to be asked here.
I have a mono repo with ...
0
votes
0
answers
85
views
How to handle dependency injection in a library to avoid frequent breaking changes?
Let's say I have a C# .NET library with the following classes:
public class FooService
{
private readonly IDependencyA a;
public FooService(IDependencyA a)
{
this.a = a;
}
...
2
votes
3
answers
216
views
Correctness of an implementation beyond the methods signature contract of an interface
Is there a programming principle/guideline that tackles the correctness of a implementation beyond the methods signature contract of an interface?
Let's say we have a repository interface with two ...
0
votes
2
answers
196
views
How to refactor this tightly-coupled method and (mostly) preserve its encapsulation?
I have recently encountered this problematic method (minimal reproducible sample in C++ but this question aims to be language agnostic past the syntax):
void MyObject::twice_bind_cycle() {
_bind1()...
0
votes
4
answers
278
views
Object matching using generic method? [closed]
I have a piece of code where two objects (incoming request object and a profile object) are to be checked for matching conditions.
So the first method is as below where I check whether the profile ...
1
vote
4
answers
645
views
Dependency injection using method injection vs constructor injection
Where should I inject the dependency when I write a class? Should it be given to __init__ or to the specific method that uses the dependent object?
Take the below two pieces of code for example, to me ...
-1
votes
1
answer
71
views
Optimal OOP security for PubSub - Should mutual consent be required?
I am implementing the Publish-Subscribe pattern.
I have class Broker and interfaces IPublisher, ISubscriber.
interface IPublisher {
broker: Broker;
publish(channel: Channel, data: object): ...
1
vote
1
answer
71
views
Querying objects in a list by class type vs string identifier - Best practices
I have a class Character who has a list of Spell objects that he can cast.
abstract class Spell {
name: string
manaCost: number
abstract cast() { ... }
}
class InvisibilitySpell extends ...
1
vote
0
answers
143
views
Should I extract my functions/types to new files in TypeScript?
Basically I have this class file which was getting quite large with functions and type definitions.
Is it best practice to keep all the code that works on the same aspect in the same file for "...
-1
votes
1
answer
181
views
Avoid use of the visitor pattern in this very common scenario
Let's assume we need to send a message, and to do so we would like our client to be concerned only with constructing the message (DTO) and using a facade service to send it.
We already know that we ...
3
votes
2
answers
215
views
Efficient way to Decouple classes in class design
I am working on a class design question - design a simple grocery store with a self-checkout system
I am a beginner
After briefly jotting down requirements, I started with the Product class as follows ...
3
votes
2
answers
274
views
Creating an abstraction just for exception handling - a pattern or anti-pattern?
Assume that an external library or framework not under our control exposes a Controller API:
abstract class Controller {
abstract fun call(): Result
}
Assume that we want to handle exceptions ...
-1
votes
1
answer
94
views
How can I prevent an object from being re-sanitized everytime it is passed as input to a function?
Suppose that I have a class named CharStream
Additionally, there are a large number of functions which convert their function input into a CharStream
def funky_the_function(_input):
input = ...
1
vote
1
answer
192
views
Newbie: OOP working with different class-datatypes and loop through them
I'm relatively new to OOP. Let's say I have three different objects/classes defined, like:
Public Class Part
Private m_Name As String = ""
Public Property Name() As String
...
2
votes
2
answers
129
views
Pattern for a base class to do pre-validation and/or post-processing on a deriving class's overridden method?
I have the following pattern repeating itself in multiple places:
abstract class Database {
void connect() {
this.setStatus( CONNECTING )
try {
await this.realConnect()
}
catch ...
0
votes
1
answer
80
views
Is it OK to stick with object graph in React State and should I make helpers for handling it?
I am trying to build ERP application using React frontend and I have not found decisive answer whether to use object graphs in React state?
E.g. I would like to build Invoice view and I have the ...
1
vote
1
answer
105
views
Wrapper class for min/max clamping and validation
I have a lot of classes that have numeric values that are configurable by the customer during runtime and should be clamped between a min and a maximum value. The value changes should also be logged. ...
-1
votes
3
answers
224
views
Is it better to override methods in classes or make methods general?
I am creating the backend of a microservice that will serve as a tool to see in real time how the company's employees are distributed by projects and what days they have assigned to each one. The ...
0
votes
2
answers
82
views
Where to specify UI views in a hierarchy of model objects?
In a Flutter app I'm developing, I have a class hierarchy of model objects of a certain base class. To take a typical example, the abstract base class could be Vehicle. Then I have a (pretty much) ...
1
vote
2
answers
124
views
Designing Products with multiple variations
I have a Product entity:
public class Product
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
[ForeignKey("Brand"...
0
votes
1
answer
58
views
Separating model and applicative user in app
I am working on a mobile application, and I want to plan a separation between the model layer that represents a user (that comes from the server) and the "applicative" user that has the same ...
1
vote
2
answers
94
views
Sharing a data class in a flow
Suppose there is a flow of functions in C++
step1();
step2();
step3();
step4();
step5();
and they interact by adding and modifying data on a data class D (only data, no functions). For example, step1(...
0
votes
2
answers
154
views
Is there a UML symbol representing instantiation? This one doesn't seem to be one
The following figure comes from https://spring.io/guides/topicals/spring-security-architecture
I refer to it as a "figure" rather than a "diagram" as I'm not sure if it is.
the ...
1
vote
1
answer
178
views
Creating a new type as slice of strings in Rust?
I have a little bit of experience with Go, that I have been trying to use as a reference point to wrap my mind around Rust via a cards game I wrote in Go that I would like to now write in Rust.
I know ...
2
votes
1
answer
89
views
Can we use external parties as objects in a sequence diagram?
I am currently building a sequence diagram for a website to allow customers to make online bookings for a travel agency. If a customer books a flight, he/she can use an online payment method to pay ...
-1
votes
2
answers
178
views
Liskov Substitution Principle Edge Case
I thought I understood Liskov Substitution Principle but then I thought of a case and I wanted to ask the community here if I get it right.
So I read somewhere to check if we have violated Liskov or ...
1
vote
1
answer
165
views
Representing database tables as objects
I'm building an address book API, where a user can input multiple contact names, phone numbers, and addresses.
My database structure would be as follows:
create table user (
id int not null primary ...
0
votes
1
answer
430
views
Pattern to convert different types of similar unchangeable objects to the same object
I have an existing system that inserts books into a library database, and I want to generalize it to include other media.
The other media is similar, but not exactly the same, and all media are third ...
1
vote
1
answer
107
views
What to use in B's configuration: A or A's configuration?
When I have a class A in a module amod as follows
// module amod
type AConfig = {
x: number;
// ...
};
class A {
constructor(public config: AConfig) {}
// ...
}
export {A, type AConfig}
...
2
votes
2
answers
243
views
Types with same name, same concept, but different... runtime uses?
I keep running into this problem when programming, and I wonder if someone has written about it before, or maybe developed some ideas about it.
For an example program, lets say I have a graphics ...
21
votes
6
answers
6k
views
Does subclassing int to forbid negative integers break Liskov Substitution Principle?
In Python 3, I subclassed int to forbid the creation of negative integers:
class PositiveInteger(int):
def __new__(cls, value):
if value <= 0:
raise ValueError("value ...
-3
votes
1
answer
440
views
What's the right data structure to combine iteration of key-value pairs, and iteration of values
I'm looking for a pre-existing or easy-to-build data structure available in Java, which can do these 2 things efficiently:
Fetch all the values stored in the collection quickly.
Iterate over the data ...
1
vote
0
answers
327
views
Avoiding forward declaration and circular reference in C++
Consider a card game (something like Magic or Legends of Runeterra) program, with BaseCard, Deck and GameState classes/structs. The way I've conceptualized the relationship between these classes would ...