728x90
๐ข Singleton Pattern
์ฑ๊ธํด ํจํด: ๊ฐ์ฒด๋ฅผ ํ๋๋ง ์์ฑ ํ๋๋ก ํ๋ฉฐ, ์์ฑ๋ ํ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์ด๋์์๋ ์ง ์ฐธ์กฐํ ์ ์๋๋ก ํ๋ ํจํด์ด๋ค.
๊ฐ์ฒด ์์ฑ์ ํด๋์ค์์์ ์ํํ๋ฉฐ ์ธ๋ถ์๋ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
๋ฐ๋ผ์ ์์ฑ์๋ private๋ก ์ ์ธํด์ผํ๋ค.
public class DuckManager {
private static DuckManager manager = new DuckManager();
private Duck[] ducks = new Duck[20];
private DuckManager() {
makeDucks();
}
public static DuckManager getDuckManager() {
return manager;
}
}
๐จ ์ฌ์ฉ๋ฒ
package mycom.duck.frame;
public class MyFrame extends Frame{
public static int FRAME_WIDTH=800;
public static int FRAME_HEIGHT=600;
DuckManager duckManager;
public MyFrame(){
duckManager = DuckManager.getDuckManager();
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.setVisible(true);
}
public void paint(Graphics g){
duckManager.displayAllDucks(g);
duckManager.flyAllDucks(g);
duckManager.swimAllDucks(g);
duckManager.quackAllDucks(g);
}
}
728x90
'๐.๋์ธํ๋ > ๐.์นด์นด์คํด๋ผ์ฐ๋์ค์ฟจ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DB] DML ์ฐ์ต๋ฌธ์ (1) | 2023.12.28 |
---|---|
[JAVA] ์์ธ Exception (0) | 2023.12.20 |
[JAVA] ์๋ฐ ์ธํฐํ์ด์ค (0) | 2023.12.18 |
[JAVA] ์๋ฐ ๋ฐฐ์ด (0) | 2023.12.16 |
[JAVA] ํ ๋ณํ (0) | 2023.12.16 |