All Questions
Tagged with inheritance design-patterns
77 questions
-2
votes
3
answers
191
views
Class inheritance design with protected methods and run time base handlers
Using C#, I got a class called BaseConfigurations which handles CRUD operations. This class also contains a protected method which is used inside the CRUD handling methods:
public class ...
24
votes
11
answers
8k
views
Is it logical to not use inheritance because the function is critical?
Our codebase has a typical base-class with a ton of sub-classes. The base-class already has many default functions for the sub-classes.
However, one particular function has the same verbatim ...
2
votes
2
answers
259
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 ...
1
vote
1
answer
185
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 ...
0
votes
1
answer
100
views
How to refactor parallel inheritance tree?
I have a (php) program, which must change yearly. This program calculates tax for every year and there are sometime changes in requirements.
First, the user fills their incomes, expenses, etc.
Then ...
0
votes
1
answer
283
views
Exposing only the getters of a singleton interface in C++
I've got a file in my includes folder, which is the folder I expose, that isn't used externally and isn't supposed to be used too. When I noticed that and tried to remove it - I noticed that it's ...
0
votes
3
answers
783
views
How to handle subclasses needing different method signatures for the overriden function?
I have an abstract class that represents chess pieces, it has an abstract method isMoveValid(Square futurePosition, PieceColor color) which checks if the piece moving to that square is valid or not, ...
1
vote
2
answers
285
views
How to design relationships with constraints on subclass type?
I'm developing a full-stack Rest application following a narrative description of a working context. There is a class Job with two subclasses Job_A and Job_B. Job_A produces Report_A while Job_B ...
0
votes
2
answers
555
views
How to solve an issue when a decorator needs variables from the base class?
I have a service class that does some magic. I want to introduce a new type of functionality - raise an event. I am absolutely sure that decorator pattern is great for this scenario. The problem is ...
0
votes
1
answer
255
views
Wrapping the UI framework
I want to write an application where I would delegate certain functionalities to 3rd party libraries. To make sure the code remains modular, I want to put these libraries behind an interface so I can ...
-1
votes
1
answer
821
views
Subclasses with same behaviour but different attributes for different inputs
Say I have two object types of the same interface MyObj (I am using python in this project so I will use the same for the explanation)
class Foo(MyObj):
a = [5, 10]
class Bar(MyObj):
a = [[1, ...
1
vote
2
answers
938
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
321
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 ...
2
votes
1
answer
231
views
How can I enforce that decorator pattern is complete at compile time?
I have a C++ class (Class D) that is a decorator of another class (Class B). Class D inherits from B and also requires an instance of B to construct that it keeps track of. Class D overrides all ...
-4
votes
1
answer
993
views
What's the best way to design a state machine with different object inheritance
I have a class A and class B, both derive from the same parent class. Each object has a state machine inside it that defines it's behaviour. The behaviour is different though depending on if the ...
1
vote
1
answer
586
views
Strategy Design Pattern vs Inheritance
I have to write a code where i have send some data (call it List of cases) using one of two integration tools (Jitterbit, Mulesoft), with possibility of adding more tools in future.
Integration tool ...
-2
votes
1
answer
49
views
Inheritance and domain-specific logic loses its independency?
Im sorry in forward, Im sure I wont be able to express myself well.
Its a game system:
abstract class GameType
{
private $score;
public __constructor($score)
{
$this->score = $...
-3
votes
1
answer
70
views
How would you architect a simple cascading style sheet object?
How would you architect a simple cascading stylesheet like inheritance object?
For example, I have Apple that extends Fruit.
class Fruit {
constructor() {
this.total = 10;
}
}
...
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 ...
0
votes
3
answers
2k
views
How to inherit a static function in a class?
Probably the answer is you can't. However, I would like to have a work-around to solve my problem.
Objective
I am trying to create a program in which I try to avoid nulls as much as possible.
...
1
vote
3
answers
386
views
subclass with no logic of abstract class
I have a class named Change which should be abstract and have some basic methods.
I have classes Insert, Update and Delete that extends Change.
In the case of Insert, Update I just use extend and add ...
0
votes
2
answers
528
views
Design issue with delegation, inheritance and dependency injection
My question relates to usage of delegation together with inheritance and dependency injection.
I have a MailerService class that requires a delegate in order to do its job.
Furthermore, I have a ...
5
votes
4
answers
14k
views
Why the industry prefer/use composition over inheritance? [duplicate]
I was having a discussion with few friends about inheritance and composition and what I learnt from that discussion is that the use of inheritance more or less condemned in industry nowadays and ...
1
vote
3
answers
473
views
design pattern for class with data attached
I have a class Tiles that looks something like this:
class Tiles {
public:
void AddTile(int x) { tiles_.push_back(x); }
std::vector<int> tiles_;
}
Now I want to create a class Tiles with ...
1
vote
1
answer
152
views
Inheritance or strategy in my circular buffer
I've looked at some other threads but they don't quite answer my question, I think...
I have a CircularBuffer object which I have separated from a ThreadSafeCircularBuffer object so that the CB logic ...
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 ...
3
votes
3
answers
8k
views
How to share the same behaviour in different classes?
I have a class called Process, which is extended by some different process types called ProcessA, ProcessB, etc.
class Process{}
class ProcessA extends Process{}
class ProcessB extends Process{}
//.....
2
votes
4
answers
3k
views
Abstract base class with only protected members
Often, I'll abstract common logic out of a class by creating an abstract base class with only protected members. For example:
class Base {
protected:
void foo() { ... }
std::map<KeyType, ...
4
votes
4
answers
3k
views
Composition over Inheritance, why not both?
I have this out of context scenario, where what I think is good practices leaves me in a situation of both implementing an interface, and using composition to do the implementation.
Imagine the ...
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:
...
3
votes
1
answer
71
views
Critique of composition design pattern requiring derived class
I have the following two inheritance chains:
BaseQueryBuilder BaseApplication
| |
| |
AppQueryBuilder ...
3
votes
2
answers
811
views
Using PHP traits to simulate multiple inheritance
For this question I want to present my current design and my idea of using a trait. I would like to know whether my understanding of traits is correct and whether my problem can be solved with another ...
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
242
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 ...
2
votes
3
answers
160
views
Base Class vs Common Member Class
Imagine I have two classes like this...
public class SingleplayerGameSetup
{
public int Seed { get; private set; }
public int AiNations { get; private set; }
}
public class ...
1
vote
1
answer
1k
views
Inheritance is better or composition design pattern in this scenario?
Design and implement Cash Register:
Given a number of items you will be required to calculate the total bill.
Items are charged for in a couple of different ways:
A given price for each item, e.g. ...
1
vote
4
answers
294
views
Abilities interacting with aspects of other abilities - how to implement efficiently
In my game I want characters to be able to cast spells which deal a certain amount of damage of a certain type to other characters in the game.
I also want abilities that negate an amount of damage ...
-1
votes
2
answers
228
views
Per my design requirements, does this design hierarchy seem reasonable?
Background
Construction
Note that I am using C# here, but it may not be necessary to provide input to my conceptual questions about design. Consider the following design methodology...
I work at a ...
8
votes
7
answers
2k
views
A property that can represent both a single date and a date range: How to properly model that?
I work in a system that can represent a "shipping estimate" in two ways:
A specific date: The item is guaranteed to ship at that date
A day interval: The item will be shipped "X to Y" days from today
...
13
votes
4
answers
4k
views
Parallel hierarchies - partly same, partly different
There are quite a few similar questions out there 1, 2, 3, 4, but non seems exactly the case in this question, nor do the solutions seem optimal.
This is a general OOP question, assuming polymorphism,...
6
votes
7
answers
5k
views
Does this code solve the square/rectangle Liskov Substitution Principle example?
I just wanted to check that I understand the LSP correctly and can solve it. I am taking the classic rectangle/square problem and attempting a solution:
class Rectangle{
public $width;
public ...
0
votes
2
answers
892
views
What is the pattern that uses multiple instances rather than multiple classes called? When would I use it?
Sometimes I find it useful to have a single class with multiple instances (configured differently via their properties), rather than multiple classes (inheritance).
??? Pattern
Single class (Fruit)
...
4
votes
5
answers
2k
views
Inheritance when following the Repository Pattern in PHP
I am trying to build a PHP application using the Repository Pattern but I'm not sure how I should implement the save method.
I have an abstract class called ItemRepository which have the following ...
1
vote
1
answer
337
views
Accepted Patterns For Base Class with both Static And User-Defined Data Sources
I have a requirement to build a 'base' profile library (in C# - NET 4.5.2) for various different internal (technical) consumers. This profile consists of metadata fields that would be shared by a wide-...
4
votes
2
answers
3k
views
C++ design for encoder/decoder classes with different stored types
I need to handle different elements in a vector, each element owning a specific parameter (integer or string), so that I can easily handle encoding/decoding of a series of elements.
Encoding a list ...
1
vote
1
answer
547
views
Design Pattern - Adding features to a class
I'm finding myself unable to solve a design problem.
For the sake of my simplicity, I'm going to use C# (Web API).
Let's say I have an abstract class RestController:
public abstract class ...
1
vote
1
answer
221
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 ...
2
votes
2
answers
346
views
Is this the solution to static inheritance?
I've seen a lot of posts recently on why Singletons should be avoided. However, I can't see any of those problems with the following solution to a common problem: static inheritance.
For example, I ...