Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Design Patterns for Java Developers

You're reading from   Practical Design Patterns for Java Developers Hone your software design skills by implementing popular design patterns in Java

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781804614679
Length 266 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Miroslav Wengner Miroslav Wengner
Author Profile Icon Miroslav Wengner
Miroslav Wengner
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1: Design Patterns and Java Platform Functionalities
2. Chapter 1: Getting into Software Design Patterns FREE CHAPTER 3. Chapter 2: Discovering the Java Platform for Design Patterns 4. Part 2: Implementing Standard Design Patterns Using Java Programming
5. Chapter 3: Working with Creational Design Patterns 6. Chapter 4: Applying Structural Design Patterns 7. Chapter 5: Behavioral Design Patterns 8. Part 3: Other Essential Patterns and Anti-Patterns
9. Chapter 6: Concurrency Design Patterns 10. Chapter 7: Understanding Common Anti-Patterns 11. Assessments 12. Index 13. Other Books You May Enjoy

Discovering multiple inheritance in Java with the twin pattern

This pattern allows you to combine functions of objects that tend to be used together, which is a common paradigm used by languages without multiple inheritance support.

Motivation

The twin pattern presents the possibility to implement multiple inheritance in Java. Multiple inheritance is not a supported concept as it may lead to compiler inconsistency, known as the diamond problem. The diamond problem defines a state through class abstraction where the compiler may turn out to be inconsistent. This state is due to the lack of information due to multiple abstract classes. The compiler does not have enough information about which methods should execute.

Sample code

This pattern is not supported by the platform and is rarely required for development. For these reasons, the pattern most likely does not exist inside the released JDK, as described. However, let us examine a possible example to better understand the pattern. Imagine the vehicle initiation sequence. During initiation, the engine and brake units need to be initiated together. In other words, when the engine is initiated, the brakes must be initiated too, and the other way around (Example 4.24):

public static void main(String[] args) {
        System.out.println("Pattern Twin, vehicle
            initiation sequence");
        var vehicleBrakes1  = new VehicleBrakes();
        var vehicleEngine1 = new VehicleEngine();
        vehicleBrakes1.setEngine(vehicleEngine1);
        vehicleEngine1.setBrakes(vehicleBrakes1);
        vehicleEngine1.init();
    }

Here’s the output:

Pattern Twin, vehicle initiation sequence
AbstractVehiclePart, constructor
AbstractVehiclePart, constructor
VehicleBrakes, initiated
VehicleEngine, initiated

The following diagram shows us tight coupling between units:

Figure 4.12 – Both considered units, VehicleEngine and VehicleBrakes, are very closely coupled

Figure 4.12 – Both considered units, VehicleEngine and VehicleBrakes, are very closely coupled

The coupling also translates into a code base that can be very fragile for future development (Example 4.25):

public class VehicleBrakes extends AbstractVehiclePart {
    private VehicleEngine twin;
    VehicleBrakes() {
    }
    void setEngine(VehicleEngine engine) {
        this.twin = engine;
    }
    @Override
    void init() {
        if (twin.isReady()) {
            setReady();
        } else {
            setReady();
            twin.init();
        }
        System.out.println("VehicleBrakes, initiated");
    }
}

Conclusion

The twin pattern can be used to achieve multiple inheritance in Java. It must be used wisely, as a logical unwritten requirement is to guarantee complete separation of the objects under consideration. In other words, the twin design pattern allows twins to function as a single instance with extended functionality and features.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime