0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🏦 Java で学ぶオブジェクト指向(銀行システムの例)

0
Last updated at Posted at 2026-06-08

1. オブジェクト指向は現実世界のモノコトをシステムで表現するためのもの

ポイント

  • オブジェクト指向は 現実の登場人物・役割・モノをプログラム上のクラスとして表現する 考え方
  • 銀行の世界なら、次のような“現実の構造”をそのままクラスに落とし込める
    • 顧客(Customer)
    • 口座(Account)
    • 銀行(Bank)
    • 窓口担当者(Teller)

🏦 銀行の例(現実 → クラス)

  • 顧客(Customer)
    • 名前や口座番号を持つ
    • 口座を開設したり、預金・引き出しができる
  • 口座(Account)
    • 口座番号、残高を持つ
    • 「預け入れ(deposit)」「引き出し(withdraw)」などの振る舞いを持つ
  • 銀行(Bank)
    • 複数の口座を管理
    • 口座間の送金(transfer)を行う
  • 窓口担当者(Teller)
    • 顧客の依頼を受け、口座に対して処理を依頼する

👉 現実の役割をそのままクラスにする

これがオブジェクト指向の基本です。

2. クラスは設計図、オブジェクトは実体

🔧 クラスとは?

  • 属性(データ)
  • メソッド(振る舞い)

🧱 オブジェクトとは?

  • クラスから作られた“実体”

実際の処理はオブジェクトに対して行う

🏦 口座クラス(Account)

public class Account {
    private String accountNumber;  // 口座番号
    private String ownerName;      // 口座名義人
    private int balance;           // 残高(初期値は 0)

    // コンストラクタ(初期化)
    public Account(String accountNumber, String ownerName, int balance) {
        this.accountNumber = accountNumber;
        this.ownerName = ownerName;
        this.balance = balance;
    }

    // 預け入れ
    public void deposit(int amount) {
        balance += amount;
        System.out.println(ownerName + " さんの口座に " + amount + " 円入金しました。残高:" + balance + " 円");
    }

    // 引き出し
    public boolean withdraw(int amount) {
        if (amount > balance) {
            System.out.println("残高不足で引き出せません。");
            return false;
        }
        balance -= amount;
        System.out.println(ownerName + " さんの口座から " + amount + " 円引き出しました。残高:" + balance + " 円");
        return true;
    }

    public int getBalance() {
        return balance;
    }
}

2. オブジェクト生成とメソッド呼び出し(Java)

public class Main {
    public static void main(String[] args) {
        // 口座クラス(Account)をもとに新しい口座を作る(オブジェクト生成)
        Account myAccount = new Account("123-45678", "田中太郎", 10000);

        // オブジェクトに対してメソッドを呼び出す
        myAccount.deposit(5000);    // 入金
        myAccount.withdraw(3000);   // 引き出し
        myAccount.withdraw(20000);  // 残高不足
    }
}

new Account(...) によってオブジェクト(口座)が生成される
myAccount.deposit(...) のようにメソッドを呼び出すことで処理が動く

3. オブジェクトからオブジェクトに「処理を依頼する」

🔑 ポイント

オブジェクト指向の本質は 「役割ごとに責務を分け、必要なときに依頼する」
送金処理は Bank が担当するが、実際の残高操作は Account に依頼する
これにより責務が分散され、拡張性が高まる

🏦 Bank クラス(送金の責務)

import java.util.HashMap;
import java.util.Map;

public class Bank {
    private Map<String, Account> accounts = new HashMap<>();

    // 口座を銀行に登録
    public void addAccount(Account account) {
        accounts.put(account.getAccountNumber(), account);
    }

    // 送金処理
    public boolean transfer(String fromAccNum, String toAccNum, int amount) {
        Account from = accounts.get(fromAccNum);
        Account to = accounts.get(toAccNum);

        if (from == null || to == null) {
            System.out.println("送金に必要な口座が存在しません。");
            return false;
        }

        // 送金元に「引き出し」を依頼
        if (!from.withdraw(amount)) {
            System.out.println("送金元の引き出しに失敗しました。");
            return false;
        }

        // 送金先に「預け入れ」を依頼
        to.deposit(amount);
        System.out.println(fromAccNum + " → " + toAccNum + " に " + amount + " 円を送金しました。");
        return true;
    }
}

クラス図

・シーケンス図

4. 責務(Responsibility)

✔ 責務とは?

クラスやオブジェクトが「何をすべきか」「どんな役割を果たすか」

設計の軸になる考え方

🏦 銀行システムの責務分担

  • ✔ Account の責務

    • 残高を管理する
    • 預け入れ・引き出しを行う
  • ✔ Bank の責務

    • 口座を管理する
    • 送金処理を取りまとめる(実際の残高操作は Account に依頼)

5. 責務を明確にするメリット

  • 役割がはっきりする
  • 変更に強くなる
  • チーム開発で分担しやすい

6. 責務を意識した設計のコツ

  • 「このクラスは何を担当する?」と常に考える
  • 名前で責務を伝える(Account は残高管理、Bank は口座管理)
  • 依頼(メソッド呼び出し)で責務を分担する

まとめ

  • オブジェクト指向は現実世界をそのままクラスとして表現する考え方
  • クラスは設計図、オブジェクトは実体
  • オブジェクト間で処理を依頼し合うことでシステムが動く
  • 責務を分けることでコードが読みやすく、変更に強くなる
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?