Layer Supertype is an object that acts as the parent of all objects in a given layer. Its purpose is to act as a central place where shared logic and common attributes for all objects in that layer are placed.
For example, if you have a domain layer where all your business objects need a unique identifier (ID), you can create a Layer Supertype that all those objects inherit from.
# When to use it?
It is ideal when you identify repetitive behaviors or attributes among multiple classes within the same architectural layer. It helps maintain the DRY (Don't Repeat Yourself) principle.
# Pros
-
verified
Reduction of Duplication
Centralizes common logic, avoiding code repetition in multiple classes.
-
verified
Consistency
Ensures that all objects in a layer follow the same standards and base behaviors.
-
verified
Maintainability
Any changes to the shared logic are made in a single place.
# Cons
-
warning
Coupling
Creates a strong dependency of all subclasses on the supertype class.
-
warning
Rigid Inheritance
In languages that only allow single inheritance, it consumes the only opportunity to inherit from another class.
# Detailed Example in Java
An example of a Layer Supertype for domain objects:
BaseEntity.java
public abstract class BaseEntity {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// Other common methods such as auditing, base validation, etc.
}
public class User extends BaseEntity {
private String name;
// User automatically inherits the ID from BaseEntity
}
# Conclusions
Layer Supertype is a powerful tool to simplify the code of an architectural layer and ensure consistency. By centralizing fundamental aspects, we allow child classes to focus solely on their specific logic.