Skip to Content
Javaへの関数的アプローチ
book

Javaへの関数的アプローチ

by Ben Weidig
March 2025
Intermediate to advanced
414 pages
5h 47m
Japanese
O'Reilly Media, Inc.
Content preview from Javaへの関数的アプローチ

第14章. 機能的デザインパターン

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

オブジェクト指向のデザインパターンに対する関数型プログラミングの答えは、通常「代わりに関数を使えばいい」というものだ。1しかし、オブジェクト指向の考え方から、機能的な原則でコードを補強しようとすると、既知のパターンを機能的な方法で活用するために、より実践的なアドバイスが必要になる。

この章では、"Gang of Four "によって説明された、よく使われるオブジェクト指向デザインパターンのいくつかを検証する。2そして、それらが機能的アプローチからどのような利点を得ることができるかを考察する。

デザインパターンとは何か?

問題を解決する必要があるたびに車輪を再発明する必要はない。 問題の多くはすでに解決されているか、少なくとも適切な解決策への一般的なアプローチがデザインパターンという形で存在している。 Java開発者であれば、当時は知らなかったとしても、すでに1つ以上のオブジェクト指向デザインパターンを使っているか、出くわしている可能性が高い。

要するに、オブジェクト指向デザインパターンとは、一般的な問題に対する、テストされ、実証され、形式化され、再現可能な解決策なのである。

4人組」は、彼らが説明するパターンを3つのグループに分類した:

振る舞いパターン

オブジェクトの責任とオブジェクト間のコミュニケーションをどう扱うか。

創造的パターン

オブジェクトの作成/インスタンス化プロセスを抽象化し、オブジェクトの作成、合成、表現を支援する方法。

構造パターン

どのようにオブジェクトを構成して、より大きな、あるいはより強化されたオブジェクトを形成するか。

デザインパターンは、特定の問題に適用するための概念で知識を共有可能にするための一般化された足場である。 そのため、すべての言語やアプローチがすべてのパターンに適合するわけではない。 特に関数型プログラミングでは、"単なる関数 "以外に特定のパターンを必要としない問題も多い。

(機能的)デザインパターン

ここでは、よく使われる4つのオブジェクト指向デザインパターンと、それらに関数的にアプローチする方法を見てみよう:

  • ファクトリーパターン(創造的パターン)

  • デコレーター・パターン(構造用)

  • 戦略パターン(振る舞い)

  • ビルダー・パターン(創造的パターン)

工場パターン

ファクトリーパターンは、オブジェクトのインスタンスを作成するためのパターンであり、ファクトリーを使うことで、オブジェクトの作成方法の実装の詳細を公開せずにインスタンスを作成することを目的としている。

オブジェクト指向アプローチ

ファクトリーパターンを実装する方法は複数ある。 私の例では、すべてのオブジェクトが共有インタフェースを持ち、enum 、目的のオブジェクトタイプを特定する:

public interface Shape {
  int corners();
  Color color();
  ShapeType type();
}

public enum ShapeType {
  CIRCLE,
  TRIANGLE,
  SQUARE,
  PENTAGON;
}

形状はレコードで表現される。レコードは他のプロパティを直接推論できるため、Color だけが必要である。単純なCircle レコードは次のようになる:

public record ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学習アルゴリズム

学習アルゴリズム

George Heineman
エンジニアが学ぶ会計システムの「知識」と「技術」

エンジニアが学ぶ会計システムの「知識」と「技術」

広川 敬祐, 五島 伸二, 小田 恭彦, 大塚 晃, 川勝 健司

Publisher Resources

ISBN: 9798341625730