Post

Object oriented, Procedural Oridented, Encapsulation

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๋ฅผ ๋ฌถ์–ด์„œ ์™ธ๋ถ€์— ์ œ๊ณตํ•˜๋Š” ๊ฒƒ
  • limit access with access modifiers
This post is licensed under CC BY 4.0 by the author.