Skip to content

Commit

Permalink
Merge branch 'master' into 2681-messaging-design-pattern-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pandiandk authored Nov 2, 2023
2 parents 6d2314a + 163c301 commit ca934b4
Show file tree
Hide file tree
Showing 27 changed files with 3,036 additions and 42 deletions.
92 changes: 92 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,98 @@
"contributions": [
"doc"
]
},
{
"login": "JerryZhao275",
"name": "JerryZhao275",
"avatar_url": "https://avatars.githubusercontent.com/u/132061401?v=4",
"profile": "https://github.com/JerryZhao275",
"contributions": [
"doc"
]
},
{
"login": "leonardol",
"name": "Leonardo Lisanti",
"avatar_url": "https://avatars.githubusercontent.com/u/1445890?v=4",
"profile": "https://twitter.com/llisant",
"contributions": [
"translation"
]
},
{
"login": "Yenniferh",
"name": "Yennifer Herrera",
"avatar_url": "https://avatars.githubusercontent.com/u/19285706?v=4",
"profile": "http://www.linkedin.com/in/yenniferh",
"contributions": [
"translation",
"review"
]
},
{
"login": "jnniu-n",
"name": "jnniu-n",
"avatar_url": "https://avatars.githubusercontent.com/u/141746282?v=4",
"profile": "https://github.com/jnniu-n",
"contributions": [
"translation"
]
},
{
"login": "miguel-is-coding",
"name": "Miguel Angel Perez Garcia",
"avatar_url": "https://avatars.githubusercontent.com/u/48435117?v=4",
"profile": "https://github.com/miguel-is-coding",
"contributions": [
"review",
"translation"
]
},
{
"login": "SuwanSankaja",
"name": "Suwan Sankaja",
"avatar_url": "https://avatars.githubusercontent.com/u/86839778?v=4",
"profile": "https://github.com/SuwanSankaja",
"contributions": [
"translation"
]
},
{
"login": "Iamalok007",
"name": "alok",
"avatar_url": "https://avatars.githubusercontent.com/u/97467835?v=4",
"profile": "https://github.com/Iamalok007",
"contributions": [
"doc"
]
},
{
"login": "dlvideira",
"name": "Daniel Lisboa",
"avatar_url": "https://avatars.githubusercontent.com/u/53951425?v=4",
"profile": "https://github.com/dlvideira",
"contributions": [
"translation"
]
},
{
"login": "ComplexOW",
"name": "Sam Powell",
"avatar_url": "https://avatars.githubusercontent.com/u/105279107?v=4",
"profile": "https://github.com/ComplexOW",
"contributions": [
"doc"
]
},
{
"login": "jppf22",
"name": "João Fernandes",
"avatar_url": "https://avatars.githubusercontent.com/u/104360594?v=4",
"profile": "https://github.com/jppf22",
"contributions": [
"translation"
]
}
],
"contributorsPerLine": 7,
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-299-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-309-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br/>
Expand Down Expand Up @@ -459,6 +459,20 @@ This project is licensed under the terms of the MIT license.
<td align="center" valign="top" width="14.28%"><a href="https://github.com/versus2004"><img src="https://avatars.githubusercontent.com/u/132815243?v=4?s=100" width="100px;" alt="Shubham"/><br /><sub><b>Shubham</b></sub></a><br /><a href="#translation-versus2004" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/inishantjain"><img src="https://avatars.githubusercontent.com/u/121454072?v=4?s=100" width="100px;" alt="Nishant Jain"/><br /><sub><b>Nishant Jain</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=inishantjain" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/hallowshaw"><img src="https://avatars.githubusercontent.com/u/90751158?v=4?s=100" width="100px;" alt="Rhitam Chaudhury"/><br /><sub><b>Rhitam Chaudhury</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=hallowshaw" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/JerryZhao275"><img src="https://avatars.githubusercontent.com/u/132061401?v=4?s=100" width="100px;" alt="JerryZhao275"/><br /><sub><b>JerryZhao275</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=JerryZhao275" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://twitter.com/llisant"><img src="https://avatars.githubusercontent.com/u/1445890?v=4?s=100" width="100px;" alt="Leonardo Lisanti"/><br /><sub><b>Leonardo Lisanti</b></sub></a><br /><a href="#translation-leonardol" title="Translation">🌍</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="http://www.linkedin.com/in/yenniferh"><img src="https://avatars.githubusercontent.com/u/19285706?v=4?s=100" width="100px;" alt="Yennifer Herrera"/><br /><sub><b>Yennifer Herrera</b></sub></a><br /><a href="#translation-Yenniferh" title="Translation">🌍</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AYenniferh" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jnniu-n"><img src="https://avatars.githubusercontent.com/u/141746282?v=4?s=100" width="100px;" alt="jnniu-n"/><br /><sub><b>jnniu-n</b></sub></a><br /><a href="#translation-jnniu-n" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/miguel-is-coding"><img src="https://avatars.githubusercontent.com/u/48435117?v=4?s=100" width="100px;" alt="Miguel Angel Perez Garcia"/><br /><sub><b>Miguel Angel Perez Garcia</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Amiguel-is-coding" title="Reviewed Pull Requests">👀</a> <a href="#translation-miguel-is-coding" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/SuwanSankaja"><img src="https://avatars.githubusercontent.com/u/86839778?v=4?s=100" width="100px;" alt="Suwan Sankaja"/><br /><sub><b>Suwan Sankaja</b></sub></a><br /><a href="#translation-SuwanSankaja" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Iamalok007"><img src="https://avatars.githubusercontent.com/u/97467835?v=4?s=100" width="100px;" alt="alok"/><br /><sub><b>alok</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Iamalok007" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dlvideira"><img src="https://avatars.githubusercontent.com/u/53951425?v=4?s=100" width="100px;" alt="Daniel Lisboa"/><br /><sub><b>Daniel Lisboa</b></sub></a><br /><a href="#translation-dlvideira" title="Translation">🌍</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ComplexOW"><img src="https://avatars.githubusercontent.com/u/105279107?v=4?s=100" width="100px;" alt="Sam Powell"/><br /><sub><b>Sam Powell</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ComplexOW" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jppf22"><img src="https://avatars.githubusercontent.com/u/104360594?v=4?s=100" width="100px;" alt="João Fernandes"/><br /><sub><b>João Fernandes</b></sub></a><br /><a href="#translation-jppf22" title="Translation">🌍</a></td>
</tr>
</tbody>
</table>
Expand Down
110 changes: 110 additions & 0 deletions extension-objects/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading

0 comments on commit ca934b4

Please sign in to comment.