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
3,374
questions
0
votes
1
answer
744
views
Is this a 'proper' use of a trait in PHP?
In Laravel 5.6 an Illuminate\Database\Eloquent\Relations\BelongsTo relation was given the ability to support default models. This allows a model that will be returned if the relationship is empty.
...
0
votes
4
answers
153
views
Should a class which has a method to create object A also implement a method to delete A?
For example, suppose I have a class to create a button with specific styles common to my app:
class ButtonFactory{
public:
static Button* createAppButton(std::string st,int font size){
Button* ...
0
votes
2
answers
759
views
Achieving Liskov-compliant contravariance in method arguments in C#
Trying to practice LSP, the following is not really clear to me:
Liskov requirements (some)
-There must be contravariance of the method
arguments in the subtype.
– There must be covariance ...
0
votes
2
answers
9k
views
Design Pattern for a "manager" type of class
I have a class that I usually end up calling XManager. This class usually acts a dispatcher. Where it will read a config and do what the config says.
Assuming that I have the following class ...
0
votes
2
answers
197
views
Where Should I put data related to more than one entity?
Right Now I design Time & Attendance System.
If I have the following entities :
Shift (Id,Name,start_time,end_time, ...etc)
EX :1,Morning,08:00,16:00
WorkSchedule (Id,Name,Type,...etc)
EX :1,...
0
votes
1
answer
121
views
Static field injection into subclasses
Say that I have a C++ class with some fields with static storage duration, call it class A.
Is there some way to use inheritance to "inject" these static fields into classes which derive from class A?...
0
votes
1
answer
383
views
Is this an example of Composition or Aggregation?
I understand the concept behind Composition ('has a') where the contained class is destroyed upon termination of the container class. Likewise, I get Aggregation which is a 'looser' relationship ...
0
votes
1
answer
119
views
Abstract class vs relation between classes
Searching for simplest solution where a User can order an item.
If someone purchases an item he will have one address. Which then is the invoice and shipping address. But he could add another address ...
0
votes
2
answers
73
views
Trouble handling constraints for the input of a method
I'm looking for advice on how to handle constraints on the input of a method. I have a method that only works if the input argument(s) fulfill certain constraints. If the method is called on input ...
0
votes
1
answer
319
views
Is it wrong to call a "non-static nested class" an "inner class" in Java?
I was reading this question in SO, and it contains the following:
A non-static nested class (sometimes incorrectly referred to as an 'inner class') (...)
And it confused me. At 350+ upvotes, I ...
0
votes
1
answer
286
views
When to use "is a" or "has a"? [closed]
In my node.js application, I have a queue class which has push and pop methods and a data property. I have an Event class which handles an event and pushes it on to the queue.
If I think object ...
0
votes
2
answers
256
views
OO design patterns similar to Drupal's hook system?
The core of the CMS Drupal is not based in object-orientation in version 7. For writing pluggable modules, it has a "hook" system, whereby you can alter the behavior (by altering passed data) for ...
0
votes
2
answers
484
views
Mermaid problem (OOP) [closed]
A Mermaid is half fish and half human. It has upper human body and lower fish body. When under water its upper part swims like a human while its lower part swims like a fish. Please explain how can it ...
0
votes
1
answer
119
views
Upgrade object in PHP
I cannot find information on this anywhere so I assume it doesn't exist.
It makes much sense for me for such functionality to exist and I want to ask whether it actually does and I am wrong in my ...
0
votes
2
answers
157
views
How to ensure a new Unit Test is for the correct Test Case? [duplicate]
When writing a unit test for a scenario believed to be already covered, ie, the first run of the test would be green, what is a good guideline to ensure that it is in fact testing the proper test case,...
0
votes
2
answers
2k
views
What is the rule for nested loop code?
I am new to programming and currently learning Java from Stanford free learning course. There was this assignment that I had trouble translating my algorithm into code. I feel like I have a ...
0
votes
1
answer
2k
views
Splitting single Class into multiple Classes
I am writing automated test scripts with Selenium Webdriver (Python) and I try to follow the correct programming practices, specifically the Object Oriented methodologies, where possible.
At the ...
0
votes
1
answer
81
views
Condition before statement or statement in condition
I've come across this problem a few times:
Is there any "programming rule" for checking for an condition (error checking for example) before you want to do execute code, or only execute the code when ...
0
votes
1
answer
156
views
Why is it said that a class represents an abstraction? [closed]
I think that abstraction represents only the essential things needed for the purpose as there are so many unnecessary properties. So why is it said that a class represents an abstraction?
0
votes
1
answer
1k
views
Interface or boolean?
I have a CustomObject class which has the abilities like being Clickable, KeyPressable and Movable. I created interfaces: IClickable, IKeyPressable and IMovable and make different CustomObject classes ...
0
votes
2
answers
1k
views
In plain English, what is an object model? [closed]
I read about objects models on Wikipedia, but it is too abstract to really make much sense to me. Can someone explain what an object model is in plain English?
0
votes
1
answer
983
views
Intermediate results as variables or only base values?
I have a object which has a few base variables (integers as example) and one intermediate variable for further processing. The intermediate variable can be calculated from the base vars. Now the ...
0
votes
3
answers
148
views
OOP: relying on protected properties in a class-subclass contract
I'm coding in Python, but the question seems independent of programming language.
I have a class that represents some system check:
class Check:
@abstractmethod
def run()
""" You have to ...
0
votes
1
answer
362
views
What should be in a PIMPL?
I am kinda trying to implement a windowing library in C++, the circumstances are forcing me to implement it all myself, that's not the point of this question however.
My question is: What should be ...
0
votes
1
answer
321
views
Method Parameters Ordering [closed]
I was recently changing a method to add in an additional parameter, and I couldn't help but wonder if there were "best practices" or "generally accepted rules" in deciding what order parameters of a ...
0
votes
1
answer
1k
views
Efficiency of getters/setters with database performance
I'm currently in the process of refactoring a very old personal project of mine, which used procedural PHP-in-HTML code. I'm modernising it as a learning project, using OOP where beneficial. I ...
0
votes
3
answers
358
views
Structuring an application that reads from a .properties file
I have a Java app with three classes: Foo, Bar and Baz. All three depend on a bunch of what are currently constants defined in each class in order to determine how to run. On top of that, Baz ...
0
votes
3
answers
685
views
In which object should I implement wait()/notify()?
I'm working in an Android project with multithreading. Basically I have to wait to the server to respond before sending more data.
The data sending task is delimited by the flag boolean ...
0
votes
3
answers
976
views
Reason to treat internal class variables and functions inside a class as "separate" entities
Note: there is a similar question that addresses my subject: Better style for member variables?
... but that question does not address "dealing with large legacy code base and comprehension of ...
0
votes
1
answer
845
views
Read-only class properties [duplicate]
When is recommended to use read-only properties -- if the language allows me?
Are public read-only properties meant to replace getter methods or at least the Magic Methods like what is available as ...
0
votes
1
answer
693
views
Objects as parameters: Is there a simple way that explain this? [closed]
I tried to understand how objects can be passed as parameters but it was difficult for me. How can we pass an object as a parameter to a method of another object? I need to understand the idea behind ...
0
votes
2
answers
1k
views
When to decide to introduce interfaces (pure abstract base classes) in C++?
Assume that you are developing a functionality and are 90% sure that the implementation class will stay alone. If I was in this position in Java I would probably not use the interface right now to ...
0
votes
2
answers
777
views
Object Orientation done right with PHP [closed]
I've started to work with web development using PHP as server side programming language. In that time I didn't know the benefits of object orientation and had a hard time trying to write more ...
0
votes
1
answer
518
views
How can my code structure be improved? [closed]
I am looking for advice on the neatest way to structure my code.
class Password
{
private string cipher;
Password(string cipher)
{
this.cipher = cipher;
}
public string ...
0
votes
3
answers
509
views
Should a service use session information?
As an example, if I have a Post service and have a method to retrieve all posts for the logged in user, is it OK to have a findPosts() method that uses an injected Security service to get the user ID ...
0
votes
3
answers
3k
views
Avoiding coupling
It is also true that a system may become so coupled, where each class
is dependent on other classes that depend on other classes, that it is
no longer possible to make a change in one place without ...
0
votes
1
answer
141
views
Separation of concerns: persisting complex types
I have the following module structure:
core
persistence
extension
In my core module, I have an interface: Handler. This interface has multiple implementations in the core module as well as extension ...
0
votes
2
answers
150
views
Appropriate design pattern for providing a default Argparse instance, eliminating boilerplate
I'm using argparse.ArgumentParser extensively; however, it comes with a lot of boilerplate to set up, and this is especially noticeable when you've got more than a few common arguments that probably ...
0
votes
2
answers
236
views
Is circular referencing required in this situation?
class ItemList {
constructor() {
this.list = [];//list holds many instances of Item Class
}
removeItem(id) {
//...search for item in this.list, remove it
}
}
class Item {
...
0
votes
2
answers
194
views
For non-container classes, are "better naming" and "ready for commented codes" good reasons not to declare the most abstract type?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare the most abstract type possible, but the question is about template class ...
0
votes
2
answers
201
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 ...
0
votes
3
answers
1k
views
How to handle a new method in the interface that is not applicable for all classes?
I am faced with an interesting OOD problem: I have an interface with 3 methods:
interface TestInterface {
String action1();
String action2();
String action3();
}
and 3 classes that ...
0
votes
2
answers
813
views
Inheritance/Composition VS "Direct Injection Construction"
my following example seems to go into the direction Inheritance VS Composition. But that's not, what i want to ask. I see the concept Inheritance and Composition on one side and the alternative, which ...
0
votes
1
answer
114
views
Time consideration in multiplicity of classes
In the following image, regarding a CD shop that rents its music CDs.
My question is regarding the correct multiplicity of X. I can think of several possibilities for X:
X= 1..1 as each copy of ...
0
votes
2
answers
978
views
How to implement resources of a REST API as classes?
I need to access and process different resources from a REST API. For this I've come up with two different approaches:
Approach 1:
Create one class per resource, that handles accessing the data from ...
0
votes
3
answers
157
views
Handling Objects with Same Output but different required inputs
I have various types of financial securities. Each one of these securities shares a common set of methods. For instance, they all pay some amount of cash interest between two dates. Each security has ...
0
votes
1
answer
115
views
Should I define a separate interface for clarity rather than implementing Supplier<T>?
Let's say you have to meet some prerequisites before doing something. For that, I think implementing Supplier<Boolean> is a great candidate since it's widely used, and quick to understand. But, ...
0
votes
3
answers
1k
views
How should an abstract class require a value that will be constant for each subclass?
Let's say I have an abstract class A exposing a method called getE() which returns an object of type E which is an enum. The value returned by getE() will be defined per subclass. Put differently, ...
0
votes
2
answers
183
views
Should we put behaviors (method) in constant class in Java?
Should we put behaviors (method) in constant class in Java? If not then why? Which clean code practice/principle I am breaking while doing that?
public class TagConstants {
public static final ...
0
votes
3
answers
2k
views
DDD - storing and retrieving entities of an aggregate from the outside
In my doctor's appointment booking system, I identified the following entities:
Doctor
Patient
Appointment
I also identified an aggregate, which is Doctor (aggregate root) and Appointment. It's an ...