All Questions
16 questions
1
vote
1
answer
192
views
Is it an acceptable pattern to put derived classes inside an abstract base class in Java?
Suppose I have some Java code such as the following (in this case, the use of the name "interaction" is referring to interacting with an object in a video game):
public abstract class ...
1
vote
2
answers
1k
views
Concept/Design question: Alternatives to switch/conditional statements and Enums
I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine.
I have two questions focused on ideal structure and design ...
2
votes
1
answer
334
views
Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?
I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case ...
28
votes
7
answers
10k
views
Polymorphism case study - Design pattern for morphing?
Imagine two classes:
class Person {
MarriedPerson marry(Person other) {...}
}
class MarriedPerson extends Person {
}
The idea is that a Person is automatically "morphed" to type MarriedPerson ...
10
votes
5
answers
4k
views
Is it ever okay to violate the LSP?
I'm following up on this question, but I'm switching my focus from code to a principle.
From my understanding of the Liskov substitution principle (LSP), whatever methods are in my base class, they ...
4
votes
1
answer
5k
views
Designing generic type inheritance hierarchy
I have now put another rephrased version of original question as requested by the user in the comments with class names mimicking real world scenario of postal office (though I dont know how real ...
4
votes
2
answers
2k
views
Inheritance and factory together?
I have a hierarchical data model and am trying to implement their CRUD operations in my Web Application.
Currently I have code inheritance for CRUD operations of my entities (resources) as follows:
...
2
votes
3
answers
205
views
Data duplication, can it be an unavoidable practice in this example?
Say I have different employees of type Employee stored in a list inside a class SubCase.
public class SubCase{
protected ArrayList<Employee> employees;
...
}
SubCase represents a part ...
1
vote
2
answers
246
views
Processing and sending processed data to super from child class constructor
I want to do some initialization in child class constructor and pass result to super().
But Java doesn't allow any processing in child class constructor before super() call.
Whats a good way to ...
1
vote
1
answer
224
views
Pattern to share fields in inheritance
I'm trying to figure out the best way to solve a design issue. I have to be able to clone (I'm cloning them to Apache HttpClient) different types of HttpServletRequest (POST, GET...) and then send ...
11
votes
5
answers
3k
views
Separate interface for mutation methods
I've been working on refactoring some code, and I think I may have taken the first step down the rabbit hole. I'm writing the example in Java, but I suppose it could be agnostic.
I have an interface ...
3
votes
4
answers
6k
views
Implements > extends, but what about variables?
It's preferable to write programs that depend on interfaces rather than on superclasses, but what if you want a class to have certain variables? Sometimes you want a class to implement a certain ...
6
votes
3
answers
1k
views
How to refactor my design, if it seems to require multiple inheritance?
Recently I made a question about Java classes implementing methods from two sources (kinda like multiple inheritance). However, it was pointed out that this sort of need may be a sign of a design flaw....
3
votes
5
answers
2k
views
Explanation of the definition of interface inheritance as described in GoF book
I am reading the first chapter of the Gof book. Section 1.6 discusses about class vs interface inheritance:
Class versus Interface Inheritance
It's important to understand the difference between an ...
3
votes
2
answers
1k
views
Template method within one class without subclasses or inheritance
I have an algorithm with mostly invariant parts that needs to be reused within one class so as to stay DRY.
Code duplication with repeating method structure
public void save(String key, int value) {...