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?

Javaチュートリアルのオブジェクト指向解説が分かりやす過ぎる

Posted at

エンジニア歴4年になったタイミングで、設計・アーキテクチャに関してしっかり学ぼうと思い立ちました。
まずは今まで触れてきたオブジェクト指向とは何か、改めて学び直そうを思いました。
そうするとOracle公式のJavaチュートリアルが非常に分かり易く、もはや答えなのでアウトプットします。

オブジェクトとは?

Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

冒頭の一文です。
Objectとは日本語で 「物体」 のことです。
オブジェクト指向プログラミングを考える上で、現実世界の物体について考えると分かり易いです。
以降ひたすらプログラミングではなく、物体についての解説が続きます。

状態と動作

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

全ての物体には 「状態」「動作」 の2つの特性があります。
自転車を例に挙げると、下記のような物があります。

  • 状態
    • 現在のギア
    • 現在のペダルのリズム
    • 現在の速度
  • 動作
    • ギアの変更
    • ペダルのリズムの変更
    • ブレーキの適用

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

プログラミングにおけるObjectは、物体と似ています。
Javaで言うところ、「状態」がFieldであり、「動作」がMethodになります。

この「状態」は「動作」によって変更され、それ以外の影響によって変更されません。
「ギアの変更」という「動作」を実行しないことには、「ギア」の「状態」を変更することはできません。
そりゃそうですね。

この外部から隠蔽することを、オブジェクト指向ではカプセル化といい、オブジェクト指向の基本原則です。

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

「状態」を変更するMethodを提供することで、Objectは外部世界がどのようにそのObjectを使用できるかを制御できます。
例えば、自転車に6つのギアしかない場合、ギアを変更するMethodは、1より小さいか6より大きい値を拒否することができます。
カプセル化をしていなければ、0や7といったギアを設定できてしまい、予期せぬ事象が発生してしまいかねません。

オブジェクト指向の利点

モジュール性

Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.

他のObjectのソースコードとは独立して、作成および保守できます。
Objectごとに責任を制限することによって、そのObjectで何をすることができるのか理解し、バグ修正や機能追加などし易くなります。

情報の隠蔽

Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

先ほどのカプセル化によって、「状態」を隠蔽・制御することができます。

コードの再利用

Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.

既に存在しているObjectを他のプログラムで使用できます。
例えば自動車やバイクをこれから作成しなければならない場合、タイヤのObjectが既にあれば、自動車を全て1から作る必要は無く、そのObjectを使い回すことができます。
※継承やインターフェースといった概念が関係します。

プラグ可能でデバッグが簡単

Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

特定のObjectに問題が発覚した場合、それをアプリケーションから削除し、代わりに別のObjectをプラグインするだけです。
これは、現実世界で機械の問題を解決することに似ています。
ボルトが壊れた場合、機械全体を交換するのではなく、ボルトを交換します。

参考記事

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?