ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA μžλ°” μΈν„°νŽ˜μ΄μŠ€, λ””μ»€ν”Œλ§
    JAVA 2020. 8. 8. 17:50

    🎯 μžλ°” μΈν„°νŽ˜μ΄μŠ€, λ””μ»€ν”Œλ§ 섀계λ₯Ό μ•Œμ•„λ³Έλ‹€.


     

     

     

    μΈν„°νŽ˜μ΄μŠ€

    public interface μΈν„°νŽ˜μ΄μŠ€λͺ… {
    	// public static final둜 μ§€μ •ν•œ μƒμˆ˜
    	// public abstract μ§€μ •μžλ₯Ό μ΄μš©ν•œ μΆ”μƒλ©”μ†Œλ“œ
    	// public default μ§€μ •μžλ₯Ό μ΄μš©ν•œ μΌλ°˜λ©”μ†Œλ“œ
    	// public static μ§€μ •μžλ₯Ό μ΄μš©ν•œ μΌλ°˜λ©”μ†Œλ“œ
    }

    μΈν„°νŽ˜μ΄μŠ€λŠ” μΆ”μƒν΄λž˜μŠ€μ™€ λΉ„μŠ·ν•œ μš©λ„λ‘œ λ©”μ†Œλ“œ μ‚¬μš©μ„ κ°•μ œν•  수 μžˆλŠ” νŠΉμ§•μ„ κ°€μ‘Œλ‹€. μƒμˆ˜, μΆ”μƒλ©”μ†Œλ“œ, defualt λ©”μ†Œλ“œ, staticλ©”μ†Œλ“œλ§Œ κ°€μ§ˆ 수 μžˆλ‹€. λΆˆμ™„μ „ν•œ μΆ”μƒλ©”μ†Œλ“œλ₯Ό κ°–κ³  있기 λ•Œλ¬Έμ— 객체생성이 λΆˆκ°€λŠ₯ν•˜λ©° κ΅¬ν˜„μ„ ν•  λ•ŒλŠ” μΌλ°˜ν΄λž˜μŠ€μ— extendsκ°€ μ•„λ‹Œ κ΅¬ν˜„ν•œλ‹€λŠ” 뜻의 implementsν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. μΈν„°νŽ˜μ΄μŠ€μ™€ κ΅¬ν˜„κ΄€κ³„λ₯Ό 가진 ν΄λž˜μŠ€λŠ” 쀀상속관계이닀.

     

     

    μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš©ν•˜κΈ°

    public interface Flyer{
    	public abstract void takeOff();
    	public abstract void fly();
    	public void land();
    }
    class Bird implements Flyer{
    	@Override
    	public void takeOff() {System.out.println("Bird 이λ₯™");}
    	@Override
    	public void fly() {System.out.println("Bird λΉ„ν–‰");}
    	@Override
    	public void land() {System.out.println("Bird μ°©λ₯™");}
    }
    class Airplane implements Flyer{
    	@Override
    	public void takeOff() {System.out.println("Airplane 이λ₯™");}
    	@Override
    	public void fly() {System.out.println("Airplane λΉ„ν–‰");}
    	@Override
    	public void land() {System.out.println("Airplane μ°©λ₯™");}
    }
    public class Ex07_2 {
    	public static void main(String[] args) {
    		 Bird bird = new Bird();
    		 bird.takeOff();
    		 bird.fly();
    		 bird.land();
    		 Flyer flyer = new Airplane();
    		 flyer.takeOff();
    		 flyer.fly();
    		 flyer.land();
    	}
    }

    1) Bird와 Airplane ν΄λž˜μŠ€λŠ” λ‚ μ•„λ‹€λ‹Œλ‹€λŠ” 곡톡 κΈ°λŠ₯을 κ°€μ‘Œλ‹€. 곡톡 κΈ°λŠ₯을 뽑아 FlyerλΌλŠ” μΈν„°νŽ˜μ΄μŠ€μ— κ΅¬ν˜„λ˜μ§€ μ•Šμ€ μΆ”μƒλ©”μ†Œλ“œλ₯Ό 3개 λ§Œλ“€μ—ˆλ‹€. μΆ”μƒν΄λž˜μŠ€μ™€ λ‹€λ₯΄κ²Œ μΈν„°νŽ˜μ΄μŠ€μ˜ μΆ”μƒλ©”μ†Œλ“œλŠ” abstract ν‚€μ›Œλ“œλ₯Ό μƒλž΅ν•  수 μžˆμ§€λ§Œ μ¨μ£ΌλŠ” 게 μ’‹λ‹€.

    2) 이제 Bird와 Airplane ν΄λž˜μŠ€λŠ” Flyer μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œλ‹€λŠ” 뜻의 implements ν‚€μ›Œλ“œλ‘œ 상속받고 μΈν„°νŽ˜μ΄μŠ€μ˜ μΆ”μƒλ©”μ†Œλ“œλ₯Ό κ·ΈλŒ€λ‘œ μ˜€λ²„λΌμ΄λ”© ν•΄μ„œ κ΅¬ν˜„ν•˜κ³  μžˆλ‹€.

    3) λ©”μΈν•¨μˆ˜ μ•ˆμ—μ„œ 일반클래슀의 객체λ₯Ό μƒμ„±ν•˜κ³  λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•œλ‹€.

    4) μΈν„°νŽ˜μ΄μŠ€λ„ λ‹€ν˜•μ„±μ΄ 적용되기 λ•Œλ¬Έμ— Airplane을 λΆ€λͺ¨νƒ€μž…인 Flyer둜 κ°μ²΄μƒμ„±ν•΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.

     

     

    default λ©”μ†Œλ“œμ™€ static λ©”μ†Œλ“œ μΆ”κ°€

    interface Vehicle{
    	public abstract void speed();
    	public default void start() {
    		System.out.println("start~");
    	}
    	public static void stop() {
    		System.out.println("stop~");
    	}
    }
    class Car implements Vehicle{
    	@Override
    	public void speed() {
    		System.out.println("100km");
    	}
    }
    public class Ex07_3 {
    	public static void main(String[] args) {
    		Car c = new Car();
    		c.start();
    		c.speed();
    		Vehicle.stop();
    	}
    }
    
    -----------
    start~
    100km
    stop~

    Vehicle μΈν„°νŽ˜μ΄μŠ€μ— μΆ”μƒλ©”μ†Œλ“œ speed()와 default μΌλ°˜λ©”μ†Œλ“œ, static μΌλ°˜λ©”μ†Œλ“œλ₯Ό λ§Œλ“ λ‹€. μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•΄μ„œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄ Car 클래슀λ₯Ό λ§Œλ“€κ³  μΆ”μƒλ©”μ†Œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”© ν•΄μ„œ ꡬ체적인 μ‹€ν–‰μ½”λ“œλ₯Ό κ΅¬ν˜„ν•œλ‹€. λ§ˆμ§€λ§‰μœΌλ‘œ λ©”μΈν•¨μˆ˜ μ•ˆμ— Car 객체λ₯Ό μƒμ„±ν•˜κ³  ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€. static λ©”μ†Œλ“œλŠ” μΈν„°νŽ˜μ΄μŠ€λͺ…μœΌλ‘œ μ ‘κ·Όν•΄μ„œ ν˜ΈμΆœν•  수 μžˆλ‹€.

     

    interface A{}
    interface B{}
    
    interface C extends A,B{}
    class My implements A,B{}
    
    public class Test {
    	public static void main(String[] args) {
    		A a = new A();  //객체생성 λΆˆκ°€
    		A a2 = new My();  //λ‹€ν˜•μ„±
    	}
    }

    μΈν„°νŽ˜μ΄μŠ€κ°„ 닀쀑상속이 κ°€λŠ₯ν•˜λ©° ν΄λž˜μŠ€λŠ” λ‹€μ€‘κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ‹€.

     

     

     

    λ””μ»€ν”Œλ§

    μ»€ν”Œλ§μ΄λž€ μ˜μ‘΄μ„±, ν•œ μŒμ„ μ˜λ―Έν•œλ‹€. 객체듀 κ°„ μ˜μ‘΄μ„±μ΄ κ°•ν•  경우, ν•œ ν”„λ‘œκ·Έλž¨μ˜ κ΅¬μ„±μš”μ†Œλ₯Ό μˆ˜μ •ν•˜λ©΄ 그와 μ—°κ΄€λœ λͺ¨λ“  κ΅¬μ„±μš”μ†Œλ₯Ό μƒˆλ‘œ μˆ˜μ •ν•΄μ•Ό ν•œλ‹€. μ—¬κΈ°μ„œ λ””μ»€ν”Œλ§μ΄λž€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•΄ λͺ¨λ“ˆ μ‚¬μ΄μ˜ μ˜μ‘΄μ„±μ„ κ°μ†Œν•˜κ³  μœ μ§€λ³΄μˆ˜ 효과λ₯Ό λ†’μ΄λŠ” 방법을 λ§ν•œλ‹€.

    μžλ°”μ™€ DBλ₯Ό μ—°κ²°ν•  λ•Œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜κ²Œ λ˜λŠ”λ°, μ•„λž˜ μ½”λ“œμ— λ“±μž₯ν•˜λŠ” DAO(Data Access Object)λŠ” λ°μ΄ν„°λ² μ΄μŠ€μ— μ ‘κ·Όν•΄μ„œ μž‘μ—…ν•˜κΈ° μœ„ν•œ 객체λ₯Ό λœ»ν•œλ‹€.

     

    μœ μ§€λ³΄μˆ˜κ°€ μ–΄λ €μš΄ 섀계

    package com.dao;
    
    public class MySQLDAO {
    	public void connectMySQL() {
    		System.out.println("MySQLDAO.connect");
    	}
    }
    package com.dao;
    
    public class OracleDAO {
    	public void connectOracle() {
    		System.out.println("OracleDAO.connect");
    	}
    }

    μ‹œν€„ ν΄λž˜μŠ€μ™€ 였라클 클래슀λ₯Ό λ§Œλ“ λ‹€. λ‘˜ λ‹€ connectλ₯Ό ν”„λ¦°νŠΈν•˜λŠ” 곡톡 κΈ°λŠ₯을 κ°–κ³  μžˆμ§€λ§Œ 상속 클래슀λ₯Ό λ§Œλ“€μ§€ μ•Šκ³  각자 μ‚¬μš©ν•˜κ³  μžˆλ‹€.

    package com.service;
    import com.dao.MySQLDAO;
    
    public class DBService {
    	public void setDAO(MySQLDAO dao) {
    		dao.connectMySQL();
    	}
    }

    μ„œλΉ„μŠ€ ν΄λž˜μŠ€λŠ” νŒŒλΌλ―Έν„°λ‘œ μ‹œν€„ 클래슀λ₯Ό λ°›μ•„μ„œ 컀λ„₯트 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” λ©”μ†Œλ“œλ₯Ό κ°–κ³ μžˆλ‹€. μ—¬κΈ°μ„œλ„ λΆˆνŽΈν•¨μ΄ λ°œμƒν•˜λŠ”λ°, μ„œλΉ„μŠ€ ν΄λž˜μŠ€μ—μ„œ OracleDAOλ₯Ό μ‚¬μš©ν•˜κ³ μž ν•œλ‹€λ©΄ 이 μ•ˆμ— 적힌 λͺ¨λ“  λ‚΄μš©μ„ μˆ˜μ •ν•΄μ•Ό ν•œλ‹€.

    import com.dao.MySQLDAO;
    import com.service.DBService;
    
    public class DBMain {
    	public static void main(String[] args) {	
    		DBService service = new DBService();
    		//OracleDAO μ‚¬μš©
    		//service.setDAO(new OracleDAO());
    		//MySQLDAO μ‚¬μš©
    		service.setDAO(new MySQLDAO());
    	}
    }

    main ν•¨μˆ˜μ—μ„œ μ„œλΉ„μŠ€ 클래슀의 객체λ₯Ό μƒμ„±ν•˜κ³  setDAO ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ©΄μ„œ 인자둜 μ‹œν€„ 클래슀λ₯Ό λ„£μ—ˆλ‹€. 호좜된 setDAOν•¨μˆ˜κ°€ 또 μ‹œν€„μ˜ 컀λ„₯트 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.

     

     

    μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•œ λ””μ»€ν”Œλ§ 섀계

    package com.dao;
    
    public interface DBDAO {
    	public abstract void connect();
    }
    

    μΆ”μƒλ©”μ†Œλ“œ connectλ₯Ό 가진 μΈν„°νŽ˜μ΄μŠ€ DBDAOλ₯Ό λ§Œλ“ λ‹€.

    package com.dao;
    
    public class MySQLDAO implements DBDAO {
    	@Override
    	public void connect() {
    		System.out.println("MySQLDAO.connect");
    	}
    }
    package com.dao;
    
    public class OracleDAO implements DBDAO {
    	@Override
    	public void connect() {
    		System.out.println("OracleDAO.connect");
    	}
    }
    import com.dao.DBDAO;
    
    public class MariaDAO implements DBDAO {
    	@Override
    	public void connect() {
    		System.out.println("MariaDAO.connect");
    	}
    }

    μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†λ°›λŠ” μ„Έ ν•˜μœ„ν΄λž˜μŠ€μ— μΆ”μƒλ©”μ†Œλ“œ connectλ₯Ό μ˜€λ²„λΌμ΄λ”© ν•΄μ„œ λ‚΄μš©μ„ κ΅¬ν˜„ν•œλ‹€.

    package com.service;
    import com.dao.DBDAO;
    
    public class DBService {
    
    	public void setDAO(DBDAO dao) {
    		dao.connect();
    	}
    }

    μ„œλΉ„μŠ€ 클래슀의 setDAO λ©”μ†Œλ“œμ—λŠ” νŒŒλΌλ―Έν„°λ‘œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ„£κ³  μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•œλ‹€.

    import com.dao.MySQLDAO;
    import com.dao.OracleDAO;
    import com.service.DBService;
    
    public class DBMain {
    	public static void main(String[] args) {		
    		DBService service = new DBService();
    		//OracleDAO μ‚¬μš©
    		service.setDAO(new OracleDAO());
    		//MySQLDAO μ‚¬μš©
    		service.setDAO(new MySQLDAO());
    		// MariaDAO μ‚¬μš©
    		service.setDAO(new MariaDAO());
    	}
    }

    λ©”μΈν•¨μˆ˜ μ•ˆμ— μ„œλΉ„μŠ€ 객체λ₯Ό μƒμ„±ν•˜κ³  setDAOλ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄ 3개의 클래슀의 컀λ„₯트 μ‹€ν–‰κ²°κ³Όλ₯Ό 좜λ ₯ν•œλ‹€. μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•΄μ„œ μœ μ§€λ³΄μˆ˜κ°€ κ°„λ‹¨ν•΄μ‘Œλ‹€.

     

     

     

    😜 NEXT : μžλ°” μ€‘μ²©ν΄λž˜μŠ€μ™€ 핡심 클래슀 κ³΅λΆ€ν•˜κΈ°

    λ°˜μ‘ν˜•

    λŒ“κΈ€

κ°œλ°œκ³΅λΆ€