You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: extension-objects/README.md
+73-68Lines changed: 73 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The Extension Objects pattern allows for the flexible extension of an object's b
21
21
22
22
Real-world example
23
23
24
-
> Suppose you are developing a Java-based game for a client, and in the middle of the development process, new features are suggested. The Extension Objects pattern empowers your program to adapt to unforeseen changes with minimal refactoring, especially when integrating additional functionalities into your project.
24
+
> An analogous real-world example of the Extension Objects design pattern can be found in modular kitchen appliances. Consider a base blender unit to which different attachments can be added, such as a food processor, juicer, or grinder. Each attachment adds new functionality to the blender without altering the base unit itself. Users can dynamically switch between different functionalities based on their current needs, making the blender highly versatile and adaptable to various tasks. This mirrors the Extension Objects pattern in software, where new functionalities are added to an object dynamically and contextually, enhancing flexibility and reuse.
25
25
26
26
In plain words
27
27
@@ -33,108 +33,113 @@ Wikipedia says
33
33
34
34
**Programmatic example**
35
35
36
-
The aim of utilising the Extension Objects pattern is to implement new features/functionality without having to refactor every class. The following examples shows utilising this pattern for an Enemy class extending Entity within a game:
36
+
The Extension Objects pattern allows for the flexible extension of an object's behavior without modifying its structure, by attaching additional objects that can dynamically add new functionality.
37
+
38
+
In this example, we have three types of units: `SoldierUnit`, `SergeantUnit`, and `CommanderUnit`. Each unit can have extensions that provide additional functionality. The extensions are `SoldierExtension`, `SergeantExtension`, and `CommanderExtension`.
37
39
38
-
Primary App class to execute our program from.
40
+
The `Unit` class is the base class for all units. It has a method `getUnitExtension` that returns an extension object based on the extension name.
The `SoldierExtension`class is a specific type of extension. It implements the `UnitExtension` interface and provides additional functionality for the `SoldierUnit`.
EntityExtension interfacetobe used by EnemyExtension.
105
+
In the `main` application, we create different types of units and check for each unit to have an extension. If the extension exists, we call the specific method on the extension object.
122
106
123
107
```java
124
-
interfaceEntityExtension {
125
-
voidextendedAction();
108
+
publicclassApp {
109
+
publicstaticvoidmain(String[] args) {
110
+
var soldierUnit =newSoldierUnit("SoldierUnit1");
111
+
var sergeantUnit =newSergeantUnit("SergeantUnit1");
112
+
var commanderUnit =newCommanderUnit("CommanderUnit1");
.ifPresentOrElse(SoldierExtension::soldierReady, () ->System.out.println(unit.getName() +" without "+ extension));
124
+
}
126
125
}
127
126
```
128
127
129
-
Programoutput:
128
+
This produces the following console output.
130
129
131
-
```markdown
132
-
Enemy performs the initial action.
133
-
Enemy wants to attack you.
134
-
Enemy has advanced towards you!
130
+
```
131
+
22:58:03.779 [main] INFO concreteextensions.Soldier -- [Soldier] SoldierUnit1 is ready!
132
+
22:58:03.781 [main] INFO App -- SoldierUnit1 without SergeantExtension
133
+
22:58:03.782 [main] INFO App -- SoldierUnit1 without CommanderExtension
134
+
22:58:03.782 [main] INFO App -- SergeantUnit1 without SoldierExtension
135
+
22:58:03.783 [main] INFO concreteextensions.Sergeant -- [Sergeant] SergeantUnit1 is ready!
136
+
22:58:03.783 [main] INFO App -- SergeantUnit1 without CommanderExtension
137
+
22:58:03.783 [main] INFO App -- CommanderUnit1 without SoldierExtension
138
+
22:58:03.783 [main] INFO App -- CommanderUnit1 without SergeantExtension
139
+
22:58:03.783 [main] INFO concreteextensions.Commander -- [Commander] CommanderUnit1 is ready!
135
140
```
136
141
137
-
Inthisexample, the ExtensionObjects pattern allows the enemy entity to perform unique initial actions and advanced actions when specific extensions are applied. This pattern provides flexibility and extensibility to the codebase while minimizing the need for major code changes.
142
+
This example demonstrates how the Extension Objects pattern allows for the flexible extension of an object's behavior without modifying its structure.
0 commit comments