Repository: iluwatar/java-design-patterns
Issue: Explanation for Extension Objects
Labels: type: feature
epic: documentation

In this issue, let's add a proper explanation for the pattern.

Acceptance criteria

- The pattern's README.md has been amended with an explanation
- The explanation consists of
  - Real world example
  - In plain words
  - Wikipedia says
  - Programmatic example


Issue comments:
Hi @iluwatar,

I was wondering if you could assign this issue to me as I would like to work on it.

Regards,
Jerry

Pull request review comments:

Patch that resolved the issue:
diff --git a/extension-objects/README.md b/extension-objects/README.md
index fa00d3d5319d..5ecc08efb394 100644
--- a/extension-objects/README.md
+++ b/extension-objects/README.md
@@ -6,10 +6,120 @@ tag:
  - Extensibility
 ---
 
+# Extention Objects Pattern
+
 ## Intent
 Anticipate that an object’s interface needs to be extended in the future. Additional
 interfaces are defined by extension objects.
 
+## Explanation
+Real-world example
+
+> 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.
+
+In plain words
+
+> The Extension Objects pattern is used to dynamically add functionality to objects without modifying their core classes. It is a behavioural design pattern used for adding new functionality to existing classes and objects within a program. This pattern provides programmers with the ability to extend/modify class functionality without having to refactor existing source code.
+
+Wikipedia says
+
+> In object-oriented computer programming, an extension objects pattern is a design pattern added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension object patterns are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition.
+
+**Programmatic example**
+
+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:
+
+Primary App class to execute our program from.
+```java
+public class App {
+    public static void main(String[] args) {
+        Entity enemy = new Enemy("Enemy");
+        checkExtensionsForEntity(enemy);
+    }
+
+    private static void checkExtensionsForEntity(Entity entity) {
+        Logger logger = Logger.getLogger(App.class.getName());
+        String name = entity.getName();
+        Function<String, Runnable> func = (e) -> () -> logger.info(name + " without " + e);
+
+        String extension = "EnemyExtension";
+        Optional.ofNullable(entity.getEntityExtension(extension))
+                .map(e -> (EnemyExtension) e)
+                .ifPresentOrElse(EnemyExtension::extendedAction, func.apply(extension));
+    }
+}
+```
+Enemy class with initial actions and extensions.
+```java
+class Enemy extends Entity {
+    public Enemy(String name) {
+        super(name);
+    }
+
+    @Override
+    protected void performInitialAction() {
+        super.performInitialAction();
+        System.out.println("Enemy wants to attack you.");
+    }
+
+    @Override
+    public EntityExtension getEntityExtension(String extensionName) {
+        if (extensionName.equals("EnemyExtension")) {
+            return Optional.ofNullable(entityExtension).orElseGet(EnemyExtension::new);
+        }
+        return super.getEntityExtension(extensionName);
+    }
+}
+```
+EnemyExtension class with overriding extendAction() method.
+```java
+class EnemyExtension implements EntityExtension {
+    @Override
+    public void extendedAction() {
+        System.out.println("Enemy has advanced towards you!");
+    }
+}
+```
+Entity class which will be extended by Enemy.
+```java
+class Entity {
+    private String name;
+    protected EntityExtension entityExtension;
+
+    public Entity(String name) {
+        this.name = name;
+        performInitialAction();
+    }
+
+    protected void performInitialAction() {
+        System.out.println(name + " performs the initial action.");
+    }
+
+    public EntityExtension getEntityExtension(String extensionName) {
+        return null;
+    }
+
+    public String getName() {
+        return name;
+    }
+}
+```
+EntityExtension interface to be used by EnemyExtension.
+```java
+interface EntityExtension {
+    void extendedAction();
+}
+```
+Program output:
+```markdown
+Enemy performs the initial action.
+Enemy wants to attack you.
+Enemy has advanced towards you!
+```
+In this example, the Extension Objects 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.
+
+
 ## Class diagram
 ![Extension_objects](./etc/extension_obj.png "Extension objects")
 


Test patch: