0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

『データ結合』とは何か👀

0
Last updated at Posted at 2026-07-02

はじめに

ソフトウェア設計におけるモジュール結合について学んでおり、

その中で表題のパターンについて知ったので、今回はこれを記事にしてみたいと思います。

データ結合とは

モジュールが何らかの処理を実装する際に、

データ構造を丸ごと利用するのではなく、その一部の本当に必要な要素のみを利用する、つまりプリミティブな値を利用することで、

不用意な知識(情報)の流出を避けるパターンのことをデータ結合と言います。

スタンプ結合の場合は、何らかの処理の実装時にデータ構造を介して実装する必要がありますが、

データ結合の場合はそれをしないで実装を行っており、モジュール結合の度合いはスタンプ結合よりも低いです。

スタンプ結合については、僭越ながら、以下の記事をご参照ください。

具体例

言葉だけだとイメージがしにくいので、ここで以下のコードを見てみましょう。

class Order {
  final String id;
  final int totalPrice;
  final String shippingAddress;
  final DateTime orderedAt;

  Order({
    required this.id,
    required this.totalPrice,
    required this.shippingAddress,
    required this.orderedAt,
  });
}

String generateReceiptTextPure(int price) {
  return 'お買い上げ金額は: ¥$price です。';
}

void main() {
  final myOrder = Order(
    id: 'ORD-123',
    totalPrice: 5500,
    shippingAddress: '福岡県博多区...',
    orderedAt: DateTime.now(),
  );

  final receipt = generateReceiptTextPure(myOrder.totalPrice);
  print(receipt);
}

上記はデータ結合を表すコードなのですが、スタンプ結合との違いを見れば、このデータ結合の状態がよくわかります。

/// スタンプ結合の場合

String generateReceiptText(Order order) {
  return 'お買い上げ金額は: ¥${order.totalPrice} です。';
}

final receipt = generateReceiptText(myOrder);

---------

/// データ結合の場合

String generateReceiptTextPure(int price) {
  return 'お買い上げ金額は: ¥$price です。';
}

final receipt = generateReceiptTextPure(myOrder.totalPrice);

データ結合の仮引数も実引数も、スタンプ結合の時のようなデータ構造を介したコミュニケーションではなく、

Intというプリミティブな型を用いたものになっています。

こうする事でOrderオブジェクトの余計な知識(情報)が漏れ出すことがなくなり、

かつ、generateReceiptTextPure関数の目的も達成できるようになっています。

少し見方を変えれば、generateReceiptTextPure関数の単一責任性を鑑みた際に、

その引数がスコープを越えずに適した範囲のものになっている、と言うことも出来るかと思われます。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?