# [Java] ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ ๋‹คํ˜•์„ฑ๊ณผ ํ™œ์šฉ
Study Repository

[Java] ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ ๋‹คํ˜•์„ฑ๊ณผ ํ™œ์šฉ

by rlaehddnd0422

์ธํ„ฐํŽ˜์ด์Šค์˜ ๋‹ค์ค‘ ์ƒ์† 

์•ž์„œ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์ค‘์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. 

public interface Attackable {
	void attack(int power);
}
public interface Movable {
	void move(int m);
}
public interface Fightable extends Movable, Attackable {
    void attackAndMove(int power, int m);
}
  • ํด๋ž˜์Šค ์ƒ์†๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ธํ„ฐํŽ˜์ด์Šค๋„ ๋ถ€๋ชจ ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜๋œ ๋ฉค๋ฒ„๋ฅผ ๋ชจ๋‘ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.
  • Fightable์€ ๋ณธ์ธ์˜ ๋ฉค๋ฒ„ attackAndMove()์™€ ๋ถ€๋ชจ ๋ฉค๋ฒ„์˜ attack(), move()๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ–๊ฒŒ ๋จ.
public class Fighter implements Fightable {

    @Override
    public void attack(int power) {

    }

    @Override
    public void attackAndMove(int power, int m) {

    }

    @Override
    public void move(int m) {

    }
}
  • ์ด ๋•Œ ๋ถ€๋ชจ์˜ ์ ‘๊ทผ ์ œ์–ด์ž๋Š” public abstract๊ฐ€ ์ƒ๋žต๋œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— (์ธํ„ฐํŽ˜์ด์Šค์˜ ํŠน์ง•) ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•  ๋•Œ ๋ถ€๋ชจ์˜ ์ œ์–ด์ž๋ฅผ ๋ฐ˜๋“œ์‹œ public์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” ๋‹ค์ค‘์ƒ์†์œผ๋กœ ์ธํ•œ ๋ฌธ์ œ๊ฐ€ ์—†์„๊นŒ?

ํด๋ž˜์Šค์—์„œ ๋‹ค์ค‘์ƒ์†์ด ์•ˆ๋˜๋Š” ์ด์œ ๋Š” ๋‘ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๋™์ผํ•œ ์‹œ๊ทธ๋‹ˆ์ฒ˜์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ–๊ณ  ์ž์‹์ด ์ด ๋‘ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ๊ฒฝ์šฐ, ์ž์‹ ์ž…์žฅ์—์„œ๋Š” ์ƒ์†๋ฐ›์„ ๋•Œ ์–ด๋–ค ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•˜๋Š” ์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

 

ํ•˜์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค์—์„œ๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ”ผํ•ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. why ? ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ 

 

  • ์–ด์ฐจํ”ผ ๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ๋‘˜ ์ค‘ ์–ด๋Š ๊ฒƒ์„ ์ƒ์† ๋ฐ›์•„๋„ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ, ์–ด๋–ค ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์•Œ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
public interface Mom {
    void giveMoney(int money);
}
public interface Father {
    void giveMoney(int money);
}
public class Child implements Father, Mom {
    @Override
    public void giveMoney(int money) {

    }
}

 

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ ๋‹คํ˜•์„ฑ

๋‹คํ˜•์„ฑ์— ๋Œ€ํ•ด ๋‹ค๋ฃฐ ๋•Œ ์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ถ€๋ชจํƒ€์ž…์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๋กœ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

public class Tv {
	protected String model;
    
}

public class SmartTv extends Tv {
	protected String smartFunction;
    
   	private void printSmartFunction(){
    	System.out.println(this.smartFuntion);
    }
}

public class SmartTvTest {
	public static void main(String[] args){
 		Tv t = new SmartTv();   
    }
}

 

์ธํ„ฐํŽ˜์ด์Šค ์—ญ์‹œ ์ด๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์กฐ์ƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์˜ ์ฐธ์กฐ๋ณ€์ˆ˜๋กœ ์ด๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค(์ž์‹)์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์œผ๋กœ ํ˜•๋ณ€ํ™˜ ๋˜ํ•œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

public interface Fightable {
    void attack(int power);
}
public class Fighter implements Fightable{

    @Override
    public void attack(int power) {
        System.out.println(power + "์˜ ๋ฐ๋ฏธ์ง€๋กœ ๊ณต๊ฒฉ");
    }

}
public class FighterTest {
    public static void main(String[] args) {
        Fightable f = new Fighter();
        f.attack(50);
    }
}

 

์‹คํ–‰ ๊ฒฐ๊ณผ

50์˜ ๋ฐ๋ฏธ์ง€๋กœ ๊ณต๊ฒฉ
  • ๋”ฐ๋ผ์„œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ Up-casting(๋ถ€๋ชจ → ์ž์‹)๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
public class FighterTest {
    public static void main(String[] args) {
        Fightable f = (Fightable) new Fighter();
        f.attack(50);
    }
}
  • ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public interface Fightable {
    void attack(int power, Fightable f);

    String getName();
}
public class Fighter implements Fightable{
    private String name;

    public Fighter(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void attack(int power, Fightable f) {
        System.out.println("Fighter attack " + f.getName() + " with " + power);
    }

}
public class Boxer implements Fightable {

    private String name;

    public Boxer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void attack(int power, Fightable f) {
        System.out.println("Boxer attack " + f + " with " + power);
    }
}
public class FighterTest {
    public static void main(String[] args) {
        Fightable f = new Fighter("fighter1");
        f.attack(50, new Boxer("boxer1"));
    }
}

 

์‹คํ–‰ ๊ฒฐ๊ณผ

Fighter attack boxer1 with 50

 

๋ฆฌํ„ดํƒ€์ž…์ด ์ธํ„ฐํŽ˜์ด์Šค์ผ ๋•Œ 

๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ดํƒ€์ž…์ด ์ธํ„ฐํŽ˜์ด์Šค โ–ถ๏ธŽ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ฝ”๋“œ

public interface Parseable {
    void parse(String fileName);
}
public class HTMLParser implements Parseable {

    @Override
    public void parse(String fileName) {
        System.out.println(fileName + " HTML Parsing");
    }
}
public class XMLParser implements Parseable {

    @Override
    public void parse(String fileName) {
        System.out.println(fileName + " XML Parsing");
    }
}
public class ParserManager {

    public static Parseable getParser(String type) {
        if (type.equals("XML")) {
            return new XMLParser();
        } else{
            return new HTMLParser();
        }
    }
}
public class ParserTest {
    public static void main(String[] args) {
        Parseable pm = ParserManager.getParser("XML");
        pm.parse("doc.xml");
    }
}

์œ„ ์ฝ”๋“œ์—์„œ ์ถ”ํ›„ ์ƒˆ๋กœ์šด ์ข…๋ฅ˜์˜ XML Parser ํด๋ž˜์Šค๊ฐ€ ๋‚˜์™€๋„ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๋ฆฌํ„ด ๋ถ€๋ถ„๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

if (type.equals("XML")) {
//    return new XMLParser();
  	return new NewXMLParser();  
}

 

์ธํ„ฐํŽ˜์ด์Šค์˜ ์žฅ์ 

  • ๊ฐœ๋ฐœ ์‹œ๊ฐ„ ๋‹จ์ถ•
    • ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ž‘์„ฑ๋˜๋ฉด ์„ ์–ธ๋ถ€๋งŒ ์•Œ๋ฉด ์ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅ
  • ํ‘œ์ค€ํ™” ๊ฐ€๋Šฅ
    • ํ”„๋กœ์ ํŠธ์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ํ‹€์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ž‘์„ฑํ•œ ๋‹ค์Œ, ๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋„๋ก ํ•จ์œผ๋กœ์จ ๋ณด๋‹ค ์ผ๊ด€๋˜๊ณ  ์ •ํ˜•ํ™”๋œ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅ
  • ์„œ๋กœ ๊ด€๊ฒŒ์—†๋Š” ํด๋ž˜์Šค๋“ค์—๊ฒŒ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ค„ ์ˆ˜ ์žˆ์Œ
    • ์ƒ์†๊ด€๊ณ„์— ์žˆ์ง€๋„ ์•Š๊ณ , ๊ฐ™์€ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์€ ์„œ๋กœ ์•„๋ฌด ๊ด€๊ณ„๋„ ์—†๋Š” ํด๋ž˜์Šค๋“ค์—๊ฒŒ ํ•˜๋‚˜์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ณตํ†ต์ ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•˜์—ฌ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ธํ„ฐํŽ˜์ด์Šค - ์ถ”์ƒํด๋ž˜์Šค - ๊ตฌํ˜„ ํด๋ž˜์Šค ๋””์ž์ธ ํŒจํ„ด

์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„ ํด๋ž˜์Šค ์‚ฌ์ด์— ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ๋‘๊ณ  ์ด ์ถ”์ƒ ํด๋ž˜์Šค์— ๊ตฌํ˜„ ํด๋ž˜์Šค์˜ ๊ณตํ†ต, ์ค‘๋ณต ๋ถ€๋ถ„๋“ค์„ ๋ชจ์•„๋‘๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

์˜ˆ์‹œ

public interface Animal {
    void walk();
    void run();
    void sleep();
    void eat();
}
public class Lion implements Animal {

    private String sex; // ์ค‘๋ณต
    private String color; // ์ค‘๋ณต

    public String getSex() { // ์ค‘๋ณต
        return sex;
    }

    public String getColor() { // ์ค‘๋ณต
        return color;
    }

    @Override
    public void walk() {
        System.out.println("Lion WALK");
    }

    @Override
    public void run() {
        System.out.println("Lion RUN");
    }
    
    @Override
    public void sleep() {
        System.out.println("Lion SLEEP");
    }

    @Override
    public void eat() {
        System.out.println("Lion EAT");
    }
}
public class Tiger implements Animal{

    private String sex; // ์ค‘๋ณต
    private String color; // ์ค‘๋ณต

    public String getSex() { // ์ค‘๋ณต
        return sex;
    }

    public String getColor() { // ์ค‘๋ณต
        return color;
    }

    @Override
    public void walk() {
        System.out.println("Tiger WALK");
    }

    @Override
    public void run() {
        System.out.println("Tiger RUN");

    }

    @Override
    public void sleep() {
        System.out.println("TIGER SLEEP");
    }

    @Override
    public void eat() {
        System.out.println("TIGER EAT");
    }
}

 

Tiger์™€ Lion ํด๋ž˜์Šค๋ฅผ ๋ณด๋ฉด sex, color,  getSex(), getColor() ์ฝ”๋“œ์˜ ์ค‘๋ณต์ด ๋ฐœ์ƒํ•˜๋Š”๋ฐ ์ด ๋•Œ ์ค‘๋ณต ๋ฉค๋ฒ„๋“ค์„ ์ถ”์ƒํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

package aboutInterface.test3;

public abstract class Mammal implements Animal{

    private String sex; // ์ค‘๋ณต
    private String color; // ์ค‘๋ณต

    public String getSex() { // ์ค‘๋ณต
        return sex;
    }

    public String getColor() { // ์ค‘๋ณต
        return color;
    }
}

 

์ด ๋””์ž์ธ ํŒจํ„ด์€ ์ค‘๋ณต ์ œ๊ฑฐ์— ํšจ๊ณผ์ ์ด์ง€๋งŒ, ํด๋ž˜์Šค ์ƒ์†์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํด๋ž˜์Šค๋Š” ๋‹ค์ค‘์ƒ์†์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 


<์ฐธ๊ณ  ์ž๋ฃŒ>

 

 

โ˜• ์ธํ„ฐํŽ˜์ด์Šค vs ์ถ”์ƒํด๋ž˜์Šค ์šฉ๋„ ์ฐจ์ด์  - ์™„๋ฒฝ ์ดํ•ด

์ธํ„ฐํŽ˜์ด์Šค vs ์ถ”์ƒํด๋ž˜์Šค ๋น„๊ต ์ด ๊ธ€์„ ์ฐพ์•„๋ณด๋Š” ๋…์ž๋ถ„๋“ค์€ ์•„๋งˆ๋„ ์ด๋ฏธ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ถ”์ƒํด๋ž˜์Šค ๊ฐœ๋…์„ ํ•™์Šตํ•œ ๋’ค์— ์ด ๋‘˜์— ๋Œ€ํ•˜์—ฌ ์ฐจ์ด์˜ ๋ชจํ˜ธํ•จ ๋•Œ๋ฌธ์— ๋ฐฉ๋ฌธ ํ–ˆ๊ฒ ์ง€๋งŒ, ๊ทธ๋ž˜๋„ ๋‹ค์‹œํ•œ๋ฒˆ

inpa.tistory.com

 

 

[Java] ์ธํ„ฐํŽ˜์ด์Šค(interface) ์ด ์ •๋ฆฌ

์„œ๋ก  Spring ๊ณต๋ถ€ํ•˜๋˜ ์ค‘ interface๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™์•„ ์ž์„ธํ•˜๊ฒŒ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ชฉ์ฐจ 1. ์ธํ„ฐํŽ˜์ด์Šค๋ž€? 2. ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ž‘์„ฑ ๋ฐฉ๋ฒ• 3. ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ƒ์† 4. ์ธํ„ฐํŽ˜์ด์Šค์˜

dkswnkk.tistory.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ