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.
143
questions
152
votes
12
answers
27k
views
Is this a violation of the Liskov Substitution Principle?
Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure ...
21
votes
6
answers
16k
views
What are the differences between abstract classes, interfaces, and when to use them
Recently I have started to wrap my head around OOP, and I am now to the point where the more I read about the differences between abstract classes and interfaces the more confused I become. So far, ...
245
votes
15
answers
164k
views
Is it wrong to use a boolean parameter to determine behavior?
I have seen a practice from time to time that "feels" wrong, but I can't quite articulate what is wrong about it. Or maybe it's just my prejudice. Here goes:
A developer defines a method with a ...
123
votes
12
answers
31k
views
Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?
If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design?
I have heard people say:
If you made Square derive from Rectangle, then a Square ...
8
votes
4
answers
2k
views
Are trivial protected getters blatant overkill?
Something I really have not thought about before (AS3 syntax):
private var m_obj:Object;
protected function get obj():Object
{
return m_obj;
}
private var m_str:String;
protected function get ...
53
votes
3
answers
72k
views
Where to put business logic in MVC design?
I have created a simple MVC Java application that adds records through data forms to a database.
My app collects data, it also validates it and stores it. This is because the data is being sourced ...
2
votes
2
answers
1k
views
Law of Demeter and its applicability
Let's say I'd like to perform the following command:
house.getFloor(0).getWall(WEST).getDoor().getDoorknob();
To avoid a NullPointerException, I'd have to do the following if:
if (house != ...
164
votes
7
answers
113k
views
What really is the "business logic"?
I'm working with web development since 2009, when I started with PHP. When I moved to ASP.NET, I've heard a lot about DDD and OOAD where a lot of focus is given to this "business logic" and &...
74
votes
9
answers
27k
views
Are classes with only a single (public) method a problem?
I am currently working on a software project that performs compression and indexing on video surveillance footage. The compression works by splitting background and foreground objects, then saving the ...
52
votes
3
answers
113k
views
How to solve circular dependency?
I have three classes that are circular dependant to each other:
TestExecuter execute requests of TestScenario and save a report file using ReportGenerator class.
So:
TestExecuter depends on ...
29
votes
9
answers
10k
views
What can go wrong if the Liskov substitution principle is violated?
I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what ...
0
votes
3
answers
445
views
Tell one, but ask the others?
Consider we have three classes which want to collaborate, then, where is the behaviour?
I guess it can only be in one of the three classes or in a fourth one acting than as a procuedural connector-...
31
votes
17
answers
8k
views
What is the value in hiding the details through abstractions? Isn't there value in transparency?
Background
I am not a big fan of abstraction. I will admit that one can benefit from adaptability, portability and re-usability of interfaces etc. There is real benefit there, and I don't wish to ...
19
votes
7
answers
6k
views
OO best practices for C programs [closed]
"If you really want OO sugar - go use C++" -- was the immediate response I got from one of my friends when I asked this. I know two things are dead wrong here. First OO is NOT 'sugar', and second, C++ ...
17
votes
5
answers
1k
views
Is SRP (Single Responsibility Principle) objective?
Consider two UI designers who want to design "user attractive" designs. "User attraction" is a concept that is not objective and only resides in the mind of designers. Thus designer A could for ...
5
votes
6
answers
8k
views
What are abstract classes and abstract methods?
I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.
Some said it has to do something with the security of the program, other said ...
3
votes
5
answers
988
views
Using a variable to indicate object type
The use of instanceof or getClass() is largely considered code smell. Is using a variable to indicate the type of object you're using also considered code smell?
Suppose if I had an enum called ...
152
votes
14
answers
22k
views
Did the Gang of Four thoroughly explore "Pattern Space"?
Ever since I first learned about the Gang of Four (GoF) design patterns, at least 10 years ago, I am having the impression that these 23 patterns should be only a small sample of something much larger ...
42
votes
2
answers
56k
views
Why is inheritance generally viewed as a bad thing by OOP proponents [duplicate]
I keep hearing the phrase "Favour Composition over Inheritance" from GoF, which is being annoyingly mentioned repeatedly by my friend, who thinks it is a valid blanket statement. But is it ...
31
votes
9
answers
29k
views
Are init() methods a code smell?
Is there any purpose for declaring an init() method for a type?
I'm not asking whether we should prefer init() over a constructor or how to avoid declaring init().
I'm asking if there is any ...
27
votes
3
answers
11k
views
How to become good at object-oriented analysis and design (OOAD)?
Becoming a good analyzer and designer can greatly benefit a developer. But there are definitely obstacles for this. Not everybody is interested in OOAD, and not every person who is interested, knows ...
20
votes
4
answers
4k
views
How to create better OO code in a relational database driven application where the database is poorly designed
I am writing a Java web application that consists mainly of a bunch of similar pages in which every page has several tables and a filter that applies to those tables. The data on these tables comes ...
12
votes
5
answers
3k
views
Too much abstraction making code hard to extend
I'm facing problems with what I feel is too much abstraction in the code base (or at least dealing with it). Most methods in the code base have been abstracted to take in the highest parent A in the ...
12
votes
4
answers
2k
views
How to clearly define boundaries of a bounded context
After a month or so of reading and researching DDD, I decided to start my own project and created DDD with these bounded contexts>
Clients
Products
Orders
Billing
Each bounded context has rest API ...
7
votes
4
answers
504
views
Is inheritance that adds rules bad? [duplicate]
I got into a debate on this question which distilled to if it is a good idea for a specialization of a class to add business rules. Unfortunately this point got trampled in the comments so I'm asking ...
7
votes
4
answers
1k
views
What books or resources would you recommend to learn practical OO design and development concepts? [closed]
I'm a self taught programmer; I understand the foundations of programming and what classes are, but my code tends to be messy with class methods interacting with a half a dozen other classes, and my ...
5
votes
5
answers
4k
views
Start Method vs. Setting up everything in constructor
The internal framework my company uses has a pretty pivotal Object that has a pattern of being instantiated as a member of a class with a no argument constructor but to be usable you have to call a ...
3
votes
1
answer
519
views
Advice for Evaluating Web Frameworks (or software libraries in general)
At my workplace we are currently looking to redesign our application stack on the web. We are satisfied and accepted that our server will be Node.js but we are finding it very difficult to find a ...
168
votes
17
answers
289k
views
Is it better to return NULL or empty values from functions/methods where the return value is not present?
I am looking for a recommendation here. I am struggling with whether it is better to return NULL or an empty value from a method when the return value is not present or cannot be determined.
Take ...
73
votes
10
answers
8k
views
My proposed design is usually worse than my colleague's - how do I get better? [closed]
I have been programming for couple of years and am generally good when it comes to fixing problems and creating small-to-medium scripts, however, I'm generally not good at designing large scale ...
59
votes
5
answers
31k
views
Using public final rather than private getters
I see most immutable POJOs written like this:
public class MyObject {
private final String foo;
private final int bar;
public MyObject(String foo, int bar) {
this.foo = foo;
...
56
votes
11
answers
4k
views
What is meant by, "A user shouldn't decide whether it is an Admin or not. The Privileges or Security system should."
The example used in the question pass bare minimum data to a function touches on the best way to determine whether the user is an administrator or not. One common answer was:
user.isAdmin()
This ...
30
votes
5
answers
7k
views
Do unit tests sometimes break encapsulation? [duplicate]
I very often hear the following: "If you want to test private methods, you'd better put that in another class and expose it."
While sometimes that's the case and we have a hiding concept ...
29
votes
6
answers
9k
views
Visitor Pattern: what's the point of the `accept` method?
I'm trying to fully understand the visitor pattern. What I've learnt so far (correct me if I'm wrong) is:
It's about adding operations to classes, without modifying the source code of those classes. ...
20
votes
2
answers
10k
views
Is there an elegant way to check unique constraints on domain object attributes without moving business logic into service layer?
I have been adapting domain-driven design for about 8 years now and even after all these years, there is still one thing, that has been bugging me. That is checking for a unique record in data storage ...
18
votes
9
answers
4k
views
Which object should have the method?
I am trying to create an object model for a user and a chatroom. I'm stuck on where to place certain functionality when the objects collaborate.
At the moment all the functionality for the User is ...
16
votes
6
answers
5k
views
Loose Coupling in Object Oriented Design
I am trying to learn GRASP and I found this explained (here on page 3) about Low Coupling and I was very surprised when I found this:
Consider the method addTrack for an Album class, two possible ...
16
votes
2
answers
2k
views
Design: Object method vs separate class's method which takes Object as parameter?
For example, is it better to do:
Pdf pdf = new Pdf();
pdf.Print();
or:
Pdf pdf = new Pdf();
PdfPrinter printer = new PdfPrinter();
printer.Print(pdf);
Another example:
Country m = new Country("...
16
votes
2
answers
2k
views
Illegal in PHP: Is there an OOP design reason?
The below interface inheritance is illegal in PHP, but I think it would be fairly useful in real life. Is there an actual antipattern or documented problem with the below design, that PHP is ...
12
votes
10
answers
10k
views
What is the point of properties?
Here are some arguments for properties and my counter-arguments:
Easier to use than writing getter and setter methods
Getter and setter method pairs are a code smell. Making it easier to write ...
11
votes
5
answers
2k
views
Is it a violation of the Open-Closed Principle to update a constant representing a real-world value?
I have a class calculating workers' net yearly income. It has a constant representing a tax percentage. But one day the tax rate has changed, so I need to fix the code.
Does the act of fixing this ...
11
votes
4
answers
5k
views
What are the valid uses of static classes?
I noticed that nearly every time I see programmers using static classes in object oriented languages such as C#, they are doing it wrong. The major problems are obviously the global state and the ...
11
votes
3
answers
21k
views
How do you annotate instantiation in UML class diagrams?
Given this pseudo code:
class B { }
class A : B
{
int val;
};
alpha = new A();
What arrow do I draw between alpha and A in a UML class diagram? Is this even something UML is meant to do?
+----...
10
votes
4
answers
3k
views
Why not apply Interface Segregation Principle to "extreme"
Providing that clients would typically consume just one method, though methods would be conceptually related, why not always apply the Interface Segregation Principle to the extreme and have [many] ...
9
votes
2
answers
774
views
I'm struggling to see how dependency inversion doesn't lead to tighter coupling in lower level modules and less reuseability
I'm refactoring a project I did for my work and I'm trying to apply the SOLID principles to make the architecture cleaner. I've run into an issue with the Dependency Inversion Principle that I can't ...
9
votes
3
answers
3k
views
OO - are large classes acceptable? [duplicate]
Despite many years in IT, I still struggle with OO design. One particular problem I seem to keep ending up with is large classes, often containing many hundreds of lines of code.
The OO world talks a ...
7
votes
1
answer
2k
views
Clean OOP-Design: How to implement single responsibility and no procedural programming
I am currently trying to refactor a piece of C# code that is somewhat procedurally written. I want to make the design clean, object oriented and using classes with single responsibilities.
The code ...
6
votes
1
answer
804
views
Which design is better for this problem?
I have a problem and I want to know what is the best way to solve it.
Problem:
I have a Binary Decision Tree. Each leaf node has an object (called Matrix) that stores some information and performs ...
5
votes
4
answers
7k
views
What is a software architecture and how to represent it?
I've searched a lot for a clear explanation for a software architecture but each time I got more confused, so What a software architecture really is and is there a standard way to represent it or it's ...
4
votes
2
answers
2k
views
Liskov substitution and abstract classes / strategy pattern
I'm trying to follow LSP in practical programming. And I wonder if different constructors of subclasses violate it. It would be great to hear an explanation instead of just yes/no. Thanks much!
P.S. ...