Compare commits

..

13 Commits

Author SHA1 Message Date
allcontributors[bot]
785cbf42b7 docs: add Fiordy as a contributor for doc (#1890)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-10-24 09:51:18 +03:00
Fiordy
c51eb66c89 docs: abstract-factory - readme - corrected code description as per real file (#1887) 2021-10-24 09:49:52 +03:00
allcontributors[bot]
1eb74203fc docs: add uh-zz as a contributor for translation (#1889)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-10-24 09:41:49 +03:00
Reo Uehara
c6803f920d translation: Add Japanese README (#1885) 2021-10-24 09:40:05 +03:00
Anum Amin
b80ac39e1f docs: Add tutorial link for Acyclic Visitor pattern (#525) (#1884) 2021-10-24 09:23:12 +03:00
Anum Amin
da6d20e997 docs: Add tutorial links for Builder pattern (#525) (#1883) 2021-10-24 09:11:34 +03:00
Anum Amin
5dbb176a33 docs: Add tutorial links for Adapter pattern (#525) (#1882) 2021-10-24 09:09:06 +03:00
Anum Amin
e3c3b02efd docs: Add tutorial links for Active object pattern (#525) (#1881) 2021-10-24 09:06:15 +03:00
allcontributors[bot]
988c478ad4 docs: add blueberry404 as a contributor for doc (#1888)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-10-24 09:04:52 +03:00
Anum Amin
221daf5d74 docs: Add tutorial links for Visitor pattern (#525) (#1880) 2021-10-24 09:03:29 +03:00
allcontributors[bot]
90b1b922e1 docs: add ManviGoel26 as a contributor for doc (#1878)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-10-21 21:15:45 +03:00
Manvi Goel
bd71bc1311 docs: add explanation for twin pattern (#1869)
* add explanation for twin pattern

* updated explanation of twin pattern
2021-10-21 21:14:10 +03:00
allcontributors[bot]
54d19d4c87 docs: add carldea as a contributor for code (#1876)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

* Removed Conflicts

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
Co-authored-by: Subhrodip Mohanta <contact@subho.xyz>
2021-10-21 23:39:05 +05:30
10 changed files with 277 additions and 2 deletions

View File

@@ -1676,6 +1676,42 @@
"contributions": [ "contributions": [
"translation" "translation"
] ]
},
{
"login": "ManviGoel26",
"name": "Manvi Goel",
"avatar_url": "https://avatars.githubusercontent.com/u/55682355?v=4",
"profile": "https://github.com/ManviGoel26",
"contributions": [
"doc"
]
},
{
"login": "blueberry404",
"name": "Anum Amin",
"avatar_url": "https://avatars.githubusercontent.com/u/39243539?v=4",
"profile": "https://github.com/blueberry404",
"contributions": [
"doc"
]
},
{
"login": "uh-zz",
"name": "Reo Uehara",
"avatar_url": "https://avatars.githubusercontent.com/u/47747828?v=4",
"profile": "https://uh-zz.github.io/blog/",
"contributions": [
"translation"
]
},
{
"login": "Fiordy",
"name": "Fiordy",
"avatar_url": "https://avatars.githubusercontent.com/u/53420573?v=4",
"profile": "https://github.com/Fiordy",
"contributions": [
"doc"
]
} }
], ],
"contributorsPerLine": 7, "contributorsPerLine": 7,

View File

@@ -10,12 +10,12 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns) [![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) [![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-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-183-orange.svg?style=flat-square)](#contributors-) [![All Contributors](https://img.shields.io/badge/all_contributors-188-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END --> <!-- ALL-CONTRIBUTORS-BADGE:END -->
<br/> <br/>
Read in different language : [**zh**](localization/zh/README.md), [**ko**](localization/ko/README.md), [**fr**](localization/fr/README.md), [**tr**](localization/tr/README.md), [**ar**](localization/ar/README.md), [**es**](localization/es/README.md), [**pt**](localization/pt/README.md), [**id**](localization/id/README.md), [**ru**](localization/ru/README.md), [**de**](localization/de/README.md) Read in different language : [**zh**](localization/zh/README.md), [**ko**](localization/ko/README.md), [**fr**](localization/fr/README.md), [**tr**](localization/tr/README.md), [**ar**](localization/ar/README.md), [**es**](localization/es/README.md), [**pt**](localization/pt/README.md), [**id**](localization/id/README.md), [**ru**](localization/ru/README.md), [**de**](localization/de/README.md), [**ja**](localization/ja/README.md)
<br/> <br/>
@@ -312,6 +312,10 @@ This project is licensed under the terms of the MIT license.
<tr> <tr>
<td align="center"><a href="http://carlfx.wordpress.com"><img src="https://avatars.githubusercontent.com/u/1594624?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Carl Dea</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=carldea" title="Code">💻</a></td> <td align="center"><a href="http://carlfx.wordpress.com"><img src="https://avatars.githubusercontent.com/u/1594624?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Carl Dea</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=carldea" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/Mozartuss"><img src="https://avatars.githubusercontent.com/u/32893711?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mozartus</b></sub></a><br /><a href="#translation-Mozartuss" title="Translation">🌍</a></td> <td align="center"><a href="https://github.com/Mozartuss"><img src="https://avatars.githubusercontent.com/u/32893711?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mozartus</b></sub></a><br /><a href="#translation-Mozartuss" title="Translation">🌍</a></td>
<td align="center"><a href="https://github.com/ManviGoel26"><img src="https://avatars.githubusercontent.com/u/55682355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Manvi Goel</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ManviGoel26" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/blueberry404"><img src="https://avatars.githubusercontent.com/u/39243539?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anum Amin</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=blueberry404" title="Documentation">📖</a></td>
<td align="center"><a href="https://uh-zz.github.io/blog/"><img src="https://avatars.githubusercontent.com/u/47747828?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Reo Uehara</b></sub></a><br /><a href="#translation-uh-zz" title="Translation">🌍</a></td>
<td align="center"><a href="https://github.com/Fiordy"><img src="https://avatars.githubusercontent.com/u/53420573?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fiordy</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Fiordy" title="Documentation">📖</a></td>
</tr> </tr>
</table> </table>

View File

@@ -87,24 +87,36 @@ public interface KingdomFactory {
} }
public class ElfKingdomFactory implements KingdomFactory { public class ElfKingdomFactory implements KingdomFactory {
@Override
public Castle createCastle() { public Castle createCastle() {
return new ElfCastle(); return new ElfCastle();
} }
@Override
public King createKing() { public King createKing() {
return new ElfKing(); return new ElfKing();
} }
@Override
public Army createArmy() { public Army createArmy() {
return new ElfArmy(); return new ElfArmy();
} }
} }
public class OrcKingdomFactory implements KingdomFactory { public class OrcKingdomFactory implements KingdomFactory {
@Override
public Castle createCastle() { public Castle createCastle() {
return new OrcCastle(); return new OrcCastle();
} }
@Override
public King createKing() { public King createKing() {
return new OrcKing(); return new OrcKing();
} }
@Override
public Army createArmy() { public Army createArmy() {
return new OrcArmy(); return new OrcArmy();
} }

View File

@@ -124,3 +124,7 @@ Now, we can create multiple creatures such as Orcs, tell them to eat and roam, a
## Class diagram ## Class diagram
![alt text](./etc/active-object.urm.png "Active Object class diagram") ![alt text](./etc/active-object.urm.png "Active Object class diagram")
## Tutorials
* [Android and Java Concurrency: The Active Object Pattern](https://www.youtube.com/watch?v=Cd8t2u5Qmvc)

View File

@@ -138,6 +138,10 @@ This pattern can be used:
* When the visited class hierarchy will be frequently extended with new derivatives of the Element class. * When the visited class hierarchy will be frequently extended with new derivatives of the Element class.
* When the recompilation, relinking, retesting or redistribution of the derivatives of Element is very expensive. * When the recompilation, relinking, retesting or redistribution of the derivatives of Element is very expensive.
## Tutorial
* [Acyclic Visitor Pattern Example](https://codecrafter.blogspot.com/2012/12/the-acyclic-visitor-pattern.html)
## Consequences ## Consequences
The good: The good:

View File

@@ -105,6 +105,12 @@ Use the Adapter pattern when
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class. * you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code. * most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
## Tutorials
* [Dzone](https://dzone.com/articles/adapter-design-pattern-in-java)
* [Refactoring Guru](https://refactoring.guru/design-patterns/adapter/java/example)
* [Baeldung](https://www.baeldung.com/java-adapter-pattern)
## Consequences ## Consequences
Class and object adapters have different trade-offs. A class adapter Class and object adapters have different trade-offs. A class adapter

View File

@@ -134,6 +134,12 @@ Use the Builder pattern when
* The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled * The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
* The construction process must allow different representations for the object that's constructed * The construction process must allow different representations for the object that's constructed
## Tutorials
* [Refactoring Guru](https://refactoring.guru/design-patterns/builder)
* [Oracle Blog](https://blogs.oracle.com/javamagazine/post/exploring-joshua-blochs-builder-design-pattern-in-java)
* [Journal Dev](https://www.journaldev.com/1425/builder-design-pattern-in-java)
## Known uses ## Known uses
* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html) * [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)

59
localization/ja/README.md Normal file
View File

@@ -0,0 +1,59 @@
# Java で実装されたデザインパターン
![Java CI](https://github.com/iluwatar/java-design-patterns/workflows/Java%20CI/badge.svg)
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=ncloc)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![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-185-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
# はじめに
デザインパターンとは、アプリケーションやシステムを設計する際によくある問題を解決するために、プログラマーが使用できる形式化された最良の手法です。
デザインパターンは、テストされ、証明された開発パラダイムを提供することで、開発プロセスを高速化することができます。
デザインパターンを再利用することで、大きな問題を引き起こす微妙な問題を防ぐことができます。
また、デザインパターンを熟知したコーダーやアーキテクトにとっても、コードの読みやすさが向上します。
# はじめる
このサイトでは、Java のデザインパターンを紹介しています。
これらのソリューションは、オープンソースコミュニティの経験豊富なプログラマーやアーキテクトによって開発されています。
パターンは、ハイレベルの説明またはソースコードを確認することで参照できます。
ソースコードの例はよくコメントされていて、特定のパターンをどのように実装するかを説明するプログラミングチュートリアルのようなものです。
私たちは、最も一般的で実績のあるオープンソースの Java 技術を使用しています。
資料に飛び込む前に、さまざまな[ソフトウェア設計の原則](https://java-design-patterns.com/principles/)に精通している必要があります。
すべてのデザインは、可能な限りシンプルであるべきです。
KISS(シンプルにしておけ!この間抜け)、YAGNI(機能は実際に必要となるまでは追加しないのがよい)、そして Do The Simplest Thing That Could Possibly Work(機能する可能性のある中で最もシンプルなことをする)の原則に基づいて設計を始めるべきです。
複雑さやパターンは、実用的な拡張性が必要な場合にのみ導入すべきです。
これらの概念を理解した上で、以下のような方法で[デザインパターン](https://java-design-patterns.com/patterns/) を掘り下げていくことができます。
- 特定のパターンを、パターン名で検索することができます。見つかない場合は、新しいパターンを[ここ](https://github.com/iluwatar/java-design-patterns/issues)で報告してください
- `Performance`, `Gang of Four` もしくは`Data access`などのタグを使用。
- `Creational`, `Behavioral`などのパターンカテゴリーを使用。
このサイトで紹介されているオブジェクト指向のソリューションが、あなたのアーキテクチャに役立ち、私たちが開発したように楽しく学ぶことができますように。
# コントリビュート方法
プロジェクトに貢献したいとお考えの方は、[開発者 wiki](https://github.com/iluwatar/java-design-patterns/wiki)に関連情報が掲載されています。
私たちは、[Gitter チャットルーム](https://gitter.im/iluwatar/java-design-patterns)であなたの質問にお答えします。
# ライセンス
このプロジェクトは、MIT ライセンスの条件の下でライセンスされています。

View File

@@ -13,6 +13,144 @@ tags:
Twin pattern is a design pattern which provides a standard solution to simulate multiple Twin pattern is a design pattern which provides a standard solution to simulate multiple
inheritance in java inheritance in java
## Explanation
Real-world example
> Consider a game with a ball that needs features of two types, Game Item, and threads to function
> smoothly in the game. We can use two objects, with one object compatible with the first type and
> the other compatible with the second type.
> The pair of objects together can function as one ball in the game.
In plain words
> It provides a way to form two closely coupled sub-classes that can act as a twin class having two ends.
Wikipedia says
> In software engineering, the Twin pattern is a software design pattern that allows developers
> to model multiple inheritance in programming languages that do not support multiple inheritance.
> This pattern avoids many of the problems with multiple inheritance.
**Programmatic Example**
Take our game ball example from above. Consider we have a game in which the ball needs to be both a `GameItem` and `Thread`.
First of all, we have the `GameItem` class given below and the `Thread` class.
```java
@Slf4j
public abstract class GameItem {
public void draw() {
LOGGER.info("draw");
doDraw();
}
public abstract void doDraw();
public abstract void click();
}
```
Then, we have subclasses `BallItem` and `BallThread` inheriting them, respectively.
```java
@Slf4j
public class BallItem extends GameItem {
private boolean isSuspended;
@Setter
private BallThread twin;
@Override
public void doDraw() {
LOGGER.info("doDraw");
}
public void move() {
LOGGER.info("move");
}
@Override
public void click() {
isSuspended = !isSuspended;
if (isSuspended) {
twin.suspendMe();
} else {
twin.resumeMe();
}
}
}
@Slf4j
public class BallThread extends Thread {
@Setter
private BallItem twin;
private volatile boolean isSuspended;
private volatile boolean isRunning = true;
/**
* Run the thread.
*/
public void run() {
while (isRunning) {
if (!isSuspended) {
twin.draw();
twin.move();
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void suspendMe() {
isSuspended = true;
LOGGER.info("Begin to suspend BallThread");
}
public void resumeMe() {
isSuspended = false;
LOGGER.info("Begin to resume BallThread");
}
public void stopMe() {
this.isRunning = false;
this.isSuspended = true;
}
}
```
Now, when we need the ball, we can instantiate objects from both the `BallThread` and `BallItem` as a pair and pass them to its pair object so they can act together as appropriate.
```java
var ballItem = new BallItem();
var ballThread = new BallThread();
ballItem.setTwin(ballThread);
ballThread.setTwin(ballItem);
```
## Class diagram ## Class diagram
![alt text](./etc/twin.png "Twin") ![alt text](./etc/twin.png "Twin")

View File

@@ -214,6 +214,12 @@ Use the Visitor pattern when
* Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them. * Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
* The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes. * The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes.
## Tutorials
* [Refactoring Guru](https://refactoring.guru/design-patterns/visitor)
* [Dzone](https://dzone.com/articles/design-patterns-visitor)
* [Sourcemaking](https://sourcemaking.com/design_patterns/visitor)
## Known uses ## Known uses
* [Apache Wicket](https://github.com/apache/wicket) component tree, see [MarkupContainer](https://github.com/apache/wicket/blob/b60ec64d0b50a611a9549809c9ab216f0ffa3ae3/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java) * [Apache Wicket](https://github.com/apache/wicket) component tree, see [MarkupContainer](https://github.com/apache/wicket/blob/b60ec64d0b50a611a9549809c9ab216f0ffa3ae3/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java)