20220303 º¹½À³ëÆ®


ÇÁ·ÎÁ§Æ® ¾È¿¡ ( main ¸Þ¼­µå ) ´Â ´Ü Çϳª¸¸ Á¸Àç

< ½Ì±ÛÅæ ¸¸µé±â >
°¡²û Àüü ÇÁ·Î±×·¥¿¡¼­ ´Ü ÇϳªÀÇ °´Ã¼¸¸ ¸¸µéµµ·Ï º¸ÀåÇØ¾ß ÇÑ´Ù.
°´Ã¼°¡ ´Ü Çϳª¸¸ »ý¼ºµÈ´Ù°í Çؼ­ À̸¦ ½Ì±ÛÅæ(Singleton)À̶ó°í ÇÑ´Ù.
½Ì±ÛÅæ ¸¸µå´Â ¹æ¹ý

1. ¿ÜºÎ Ŭ·¡½º¿¡¼­ new ¿¬»êÀÚ·Î »ý¼ºÀÚ¸¦ È£ÃâÇÒ ¼ö ¾øµµ·Ï ¸·¾Æ¾ß ÇÔ.
2. private Á¢±Ù ÁöÁ¤ÀÚ(Á¦ÇÑÀÚ) »ç¿ë
3. ÀÚ½ÅÀÇ Å¸ÀÔÀÎ Á¤Àû Çʵå Çϳª ¼±¾ð ¹× ÀÚ½ÅÀÇ °´Ã¼ »ý¼º
4. getInstance()¸¦ ¼±¾ðÈÄ Á¤Àû Çʵ忡¼­ ÂüÁ¶ÇÏ°í ÀÖ´Â ÀÚ½ÅÀÇ °´Ã¼¸¦ ¸®ÅÏÇϵµ·Ï ÇÔ.

¿¹(¹«Á¶°Ç ¾Ï±â)

public class B {
     private static B singleton = new B();

     private B() {

     }
     static B getInstance() {
          return singleton;
     }

}

< Ä÷º¼Ç ÇÁ·¹ÀÓ¿öÅ© >
Ä÷º¼ÇÀº °´Ã¼ÀÇ ÀúÀå
ÇÁ·¹ÀÓ¿öÅ©´Â »ç¿ë ¹æ¹ýÀ» Á¤ÇØ ³õÀº ¶óÀ̺귯¸®


AŬ·¡½º(main) , BŬ·¡½º(½Ì±ÛÅæ) , CŬ·¡½º(ÀÎÅÍÆäÀ̽º) , DŬ·¡½º(select) , EŬ·¡½º(insert) , FŬ·¡½º(update), GŬ·¡½º(delete)

ÀÎÅÍÆäÀ̽º = ºÒ¾ÈÁ¤ÇÑ Å¬·¡½º ( º¯¼ö + ºÒ¾ÈÁ¤ÇÑ ¸Þ¼­µå )

public class D implements C { = ÀÎÅÍÆäÀ̽ºC¸¦ »ó¼Ó¹ÞÀº D Ŭ·¡½º

}


public class A {

     public static void main(String[] args) {
          String command1="select";
          String command2="insert";
          String command3="update";
          String command4="delete";
          String[] command = {command1,command2,command3,command4};

          B b = B.getInstance(); //getInstance()·Î Á¢±ÙÇÏ¿© privateÁ¦ÇÑµÈ BŬ·¡½ºÀÇ ÀνºÅϽº¸¦ »ý¼ºÇÔ.
          B b2 = B.getInstance();
          for(String co : command) {

               C c = b.getAction(co); //BŬ·¡½º¿¡ ÀÖ´Â getAction ¸Þ¼­µå¸¦ ÀÌ¿ëÇÏ¿© D~GŬ·¡½ºÀÇ ¸Þ¼­µå¸¦ C¿¡ ´ëÀÔÇÏ¿© c°´Ã¼ »ý¼º.
               c.execute(); //ÀÎÅÍÆäÀ̽º C ¾È¿¡ ÀÖ´Â ºÒ¿ÏÀüÇÑ execute()ÇÔ¼ö´Â ½ÇÁúÀûÀ¸·Î D~F(»ó¼Ó°ü°è)¿¡ µé¾îÀÖ´Â ¸Þ¼­µå.

          }

          System.out.println(b.a +", "+ b2.a);
          b.a=2;
          System.out.println(b.a +", "+ b2.a);
          b2.a=100;
          System.out.println(b.a +", "+ b2.a); //BŬ·¡½ºÀÇ ¸â¹öº¯¼ö a¿¡ ¹«½¼ °ªÀ» ´ëÀÔÇϵç, b ¿Í b2 °´Ã¼ÀÇ ¸â¹öº¯¼ö a´Â µ¿ÀÏÇÑ °ªÀ» Áö¸ñÇÏ´Â °ÍÀ» È®ÀÎ.

     }

     /*
          ½ÇÇà°á°ú :
          select?
          Select OK!!!
          insert?
          Select OK!!!
          update?
          Select OK!!!
          delete?
          Select OK!!!
          1, 1
          2, 2
          100, 100
     */

}

public class B {
     int a = 1;

     //½Ì±ÛÅæ »ý¼º
     private static B bInstance = new B();

     private B() {

     }

     public static B getInstance() {

          return bInstance;
     }

     public C getAction(String command) { // D~GŬ·¡½º¸¦ °´Ã¼c¿¡ ´ëÀÔÇÏ¿© ¸®ÅÏ

               C c=null;

               switch (command) {
               case "select" :

                    c=new D();
                    System.out.println("select?");
                    break;
               case "insert" :
                    c=new E();
                    System.out.println("insert?");
                    break;
               case "update" :
                    c=new F();
                    System.out.println("update?");
                    break;
               case "delete" :
                    c=new G();
                    System.out.println("delete?");
                    break;
               }

          return c;
     }
}

public interface C { //ÀÎÅÍÆäÀ̽º = ºÒ¾ÈÁ¤ÇÑ Å¬·¡½º ( º¯¼ö + ºÒ¾ÈÁ¤ ¸Þ¼­µå )
                          // D~G¸¦ »ó¼ÓÇÔ
     void execute();
}

public class D implements C{
     // Override = ÀÎÅÍÆäÀ̽º CÀÇ ºÒ¾ÈÁ¤ÇÑ ¸Þ¼­µåµéÀ» ¿©±â¼­ ¿Ï¼º½ÃÄÑÁÜ
     @Override
     public void execute() {
          System.out.println("Select OK!!!");
     }
}

public class E implements C{
     @Override
     public void execute() {
          System.out.println("Insert OK!!!");
     }
}

public class F implements C{
     @Override
     public void execute() {
          System.out.println("Update OK!!!");
     }
}

public class G implements C{
     @Override
     public void execute() {
          System.out.println("Delete OK!!!");
     }
}