Post

Object oriented, Procedural Oridented

Procedural Oridented πŸ†š Object oriented

  • Procedural Oridented: μˆœμ„œ μ€‘μš”, how, λ©”μ†Œλ“œ 뢄리
  • Object oriented: 객체, what, κ°μ²΄μ•ˆμ— λ©”μ†Œλ“œ 포함

βœ… example of procedural programming

  • example of Music Player

  • βœ”οΈ music player class

1
2
3
4
public class MusicPlayerData {
    int volume;
    boolean isOn;
}
  • βœ”οΈ class with method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MusicPlayerMain {
    public static void main(String[] args) {
        MusicPlayerData data = new MusicPlayerData();

        turnOn(data);
        raiseVolume(data);
    }

    static void turnOn(MusicPlayerData data){
        data.isOn = true;
        System.out.println("music player is on");
    }

    static void raiseVolume(MusicPlayerData data){
        data.volume++;
        System.out.println("volume: " + data.volume);
    }
  • more focused on the order (turn on, then raise volume)
  • πŸ‘ŽπŸ» music player field and method are in different classes
  • one in music player class and other in main class
  • field and method of music player are very very relevent
  • can we find a way to put field and method in one class?

βœ… change into OOP

  • βœ”οΈ music player class
  • has both field and method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MusicPlayer {
    int volume; //field
    boolean isOn; //field

    void turnOn(){ //method
        isOn = true;
        System.out.println("music player is on");
    }

    void raiseVolume(){ //method
        volume++;
        System.out.println("volume: " + volume);
    }
}
  • βœ”οΈ main class
  • only create instance, calls the method
1
2
3
4
5
6
7
8
public class MusicPlayerMain4 {
    public static void main(String[] args) {
        MusicPlayer player = new MusicPlayer();

        player.turnOn();
        player.raiseVolume();
    }
}
  • more focused on how the music player works

  • πŸ‘πŸ» field and method are in one class, easier for the programmer to understand
  • πŸ‘πŸ» programming looks more like real life
  • πŸ‘πŸ» easier to change code
    • only has to change music player class, main class does not change

βœ… Encapsulation

  • OOPμ—μ„œμ²˜λŸΌ field and methodλ₯Ό ν•œ ν΄λž˜μŠ€μ— 캑슐둜 감싸듯이 λͺ¨μ€ 것을 μΊ‘μŠν™”λΌκ³  ν•œλ‹€.
  • μΊ‘μŠν™”: field and methodλ₯Ό λ¬Άμ–΄μ„œ 외뢀에 μ œκ³΅ν•˜λŠ” 것
This post is licensed under CC BY 4.0 by the author.