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
+56-14Lines changed: 56 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,24 @@
1
1
---
2
-
title: Extension objects
3
-
category: Behavioral
2
+
title: Extension Objects
3
+
category: Structural
4
4
language: en
5
5
tag:
6
-
- Extensibility
6
+
- Encapsulation
7
+
- Extensibility
8
+
- Object composition
9
+
- Polymorphism
7
10
---
8
11
9
-
# Extention Objects Pattern
12
+
## Also known as
13
+
14
+
* Interface Extensions
10
15
11
16
## Intent
12
-
Anticipate that an object’s interface needs to be extended in the future. Additional
13
-
interfaces are defined by extension objects.
17
+
18
+
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.
14
19
15
20
## Explanation
21
+
16
22
Real-world example
17
23
18
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.
@@ -27,10 +33,10 @@ Wikipedia says
27
33
28
34
**Programmatic example**
29
35
30
-
The aim of utilising the Extension Objects pattern is to implement new features/functionality without having to refactor every class.
31
-
The following examples shows utilising this pattern for an Enemy class extending Entity within a game:
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:
@@ -81,7 +91,9 @@ class EnemyExtension implements EntityExtension {
81
91
}
82
92
}
83
93
```
94
+
84
95
Entity classwhich will be extended by Enemy.
96
+
85
97
```java
86
98
classEntity {
87
99
privateString name;
@@ -105,32 +117,62 @@ class Entity {
105
117
}
106
118
}
107
119
```
120
+
108
121
EntityExtension interfaceto be used by EnemyExtension.
122
+
109
123
```java
110
124
interfaceEntityExtension {
111
125
voidextendedAction();
112
126
}
113
127
```
128
+
114
129
Program output:
130
+
115
131
```markdown
116
132
Enemy performs the initial action.
117
133
Enemy wants to attack you.
118
134
Enemy has advanced towards you!
119
135
```
120
-
Inthis example, 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.
121
136
137
+
Inthis example, 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.
* you need to support the addition of new or unforeseen interfaces to existing classes and you don't want to impact clients that don't need this new interface. Extension Objects lets you keep related operations together by defining them in a separate class
130
-
* a class representing a key abstraction plays different roles for different clients. The number of roles the class can play should be open-ended. There is a need to preserve the key abstraction itself. For example, a customer object is still a customer object even if different subsystems view it differently.
131
-
* a class should be extensible with new behavior without subclassing from it.
145
+
This pattern is applicable in scenarios where an object's functionality needs to be extended at runtime, avoiding the complications of subclassing. It's particularly useful in systems where object capabilities need to be augmented post-deployment, or where the capabilities might vary significantly across instances.
132
146
133
-
## Real world examples
147
+
## KnownUses
134
148
149
+
*Extending services in an application server without altering existing code.
150
+
*Plugins in IDEs like IntelliJIDEA or Eclipse to add features to the base application.
151
+
*Enabling additional features in enterprise software based on license levels.
* [ObjectLinking and Embedding](https://en.wikipedia.org/wiki/Object_Linking_and_Embedding)
154
+
155
+
## Consequences
156
+
157
+
Benefits:
158
+
159
+
*Enhances flexibility by allowing dynamic extension of an object's capabilities.
160
+
* Promotes loose coupling between the base object and its extensions.
161
+
* Supports the [Open/Closed Principle](https://java-design-patterns.com/principles/#open-closed-principle) by keeping the object open for extension but closed for modification.
162
+
163
+
Trade-offs:
164
+
165
+
* Can increase complexity due to the management of extension objects.
166
+
* May introduce performance overhead if the interaction between objects and extensions is not efficiently designed.
167
+
168
+
## Related Patterns
169
+
170
+
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Similar in intent to add responsibilities dynamically, but uses a different structure.
171
+
* [Composite](https://java-design-patterns.com/patterns/composite/): Also manages a group of objects, which can be seen as a form of extension.
172
+
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Offers an alternative way to change the behavior of an object dynamically.
173
+
174
+
## Credits
175
+
176
+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/4aBMuuL)
177
+
* [Pattern-Oriented Software Architecture: A System of Patterns](https://amzn.to/3Q9YOtX)
178
+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3W6IZYQ)
0 commit comments