์ ์ ๋ณ์๋ ๋ฌด์์ ๋๊น?
์ ์ ๋ณ์ ๋๋ ํด๋์ค ๋ณ์๋ ํด๋์ค์ ๋ชจ๋ ์ธ์คํด์ค์์ ๊ณต์ ๋๋ฉฐ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ์ก์ธ์คํ๊ณ ์์ ํ ์ ์์ต๋๋ค. Java์์ ์ ์ ๋ณ์ ์ ์ฌ์ฉ๊ณผ ๊ตฌํ์ ์ดํดํ๊ธฐ ์ํด ์ฌ์ธต์ ์ผ๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค .Java์ ์ ์ ๋ณ์ ์
์ ์ ๋ณ์ ์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์ฉ๋ ์ค ํ๋๋ ํด๋์ค์ ๋ชจ๋ ์ธ์คํด์ค ๊ฐ์ ๊ณต์ ํ๋ ๊ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด ์์ฑ๋ ์ธ์คํด์ค ์๋ฅผ ์ถ์ ํ๋ ์นด์ดํฐ ๋ณ์ ๋๋ ์ ๋ ๋ณ๊ฒฝ๋์ง ์๋ ๊ฐ์ ๋ณด์ ํ๋ CONSTANT ๋ณ์๊ฐ ์์ต๋๋ค. ์ํ ๊ณ์ข๋ฅผ ๋ํ๋ด๋ ํด๋์ค๋ฅผ ์๊ฐํด ๋ณด์ธ์. ๋ค์ ์ฝ๋ ์กฐ๊ฐ์ ์ ์ ๋ณ์ nextAccountNumber๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๊ฐ์ ์ ๊ณ์ ์ ๊ณ ์ ํ ๊ณ์ ๋ฒํธ๋ฅผ ํ ๋นํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.์
// BankAccount class definition
class BankAccount {
// Declare static variable nextAccountNumber
static int nextAccountNumber = 1001;
// Declare instance variables for accountNumber and balance.
int accountNumber;
double balance;
// BankAccount constructor that assigns unique account number to each instance
BankAccount() {
// Assign the current value of nextAccountNumber to accountNumber.
accountNumber = nextAccountNumber;
// Increment nextAccountNumber for the next BankAccount object.
nextAccountNumber++;
}
// Method to get the account number of this BankAccount object.
int getAccountNumber() {
return accountNumber;
}
// Method to get the balance of this BankAccount object.
double getBalance() {
return balance;
}
// Method to deposit an amount into this BankAccount object.
void deposit(double amount) {
// Increase balance by the amount deposited.
balance += amount;
}
// Method to withdraw an amount from this BankAccount object.
void withdraw(double amount) {
// Decrease balance by the amount withdrawn.
balance -= amount;
}
}
// Main method definition
class Main {
static void main(String[] args) {
// Create three BankAccount objects: account1, account2, and account3.
BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();
BankAccount account3 = new BankAccount();
// Display the value of the static variable nextAccountNumber after creating each BankAccount object.
System.out.println("Value of nextAccountNumber after creating account1: " + account1.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account2: " + account2.getAccountNumber());
System.out.println("Value of nextAccountNumber after creating account3: " + account3.getAccountNumber());
}
}
์ฐ์ถ
account1 ์์ฑ ํ nextAccountNumber ๊ฐ: 1001 account2 ์์ฑ ํ nextAccountNumber ๊ฐ: 1002 account3 ์์ฑ ํ nextAccountNumber ๊ฐ: 1003
์ด ์์ ์์ ๊ธฐ๋ณธ ๋ฉ์๋๋ BankAccount ํด๋์ค์ ์ธ์คํด์ค 3๊ฐ๋ฅผ ๋ง๋ญ๋๋ค. BankAccount ์์ฑ์๋ nextAccountNumber๋ฅผ ์ ๊ณ์ข์ ํ ๋น ํ๊ณ nextAccountNumber ๋ณ์๋ฅผ 1์ฉ ์ฆ๊ฐ์ํต๋๋ค . getAccountNumber ๋ฉ์๋๋ ๊ฐ ๊ณ์ข์ ๊ณ์ข ๋ฒํธ๋ฅผ ๋ฐํํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ธฐ๋ณธ ๋ฉ์๋๋ ๊ฐ ๊ณ์ ์ ๋ํ ๊ณ์ข ๋ฒํธ์ nextAccountNumber ๋ณ์๋ฅผ ์ธ์ํฉ๋๋ค. ๊ฐ ๊ณ์ข ๋ฒํธ๋ ๊ณ ์ ํ๋ฉฐ nextAccountNumber ๋ณ์๋ ์์ฑ๋ ์ ๊ณ์ข๋ง๋ค 1์ฉ ์ฆ๊ฐํ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค . ์ ์ ๋ณ์๋ ํด๋์ค ์ธ์คํด์ค๋ฅผ ํ๋๋ง ๋ง๋ค ์ ์๋ ์ฑ๊ธํค ๋์์ธ ํจํด์ ๊ตฌํํ๋ ๋ฐ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ฑ๊ธํค ํด๋์ค๋ Java ์ ํ๋ฆฌ์ผ์ด์
์์ ํ๋์ ์ธ์คํด์ค๋ง ๊ฐ์ง ์ ์์ผ๋ฉฐ ๋์์ ์ด ์ธ์คํด์ค์ ๋ํ ์ ์ญ ์ก์ธ์ค ์ง์ ์ ์ ๊ณตํ๋ ํด๋์ค์
๋๋ค . ๋ค์ ์ฝ๋ ์กฐ๊ฐ์ ์ ์ ๋ณ์ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ฑ๊ธํค ํด๋์ค๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์
public class Singleton {
// using static variable in the singleton class
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void displayMessage() {
System.out.println("This is a singleton class");
}
public static void main(String[] args) {
Singleton singleton1 = Singleton.getInstance();
singleton1.displayMessage();
Singleton singleton2 = Singleton.getInstance();
singleton2.displayMessage();
Singleton singleton3 = Singleton.getInstance();
singleton3.displayMessage();
}
}
์ฐ์ถ
์ด๊ฒ์ ์ฑ๊ธํค ํด๋์ค์
๋๋ค ์ด๊ฒ์ ์ฑ๊ธํค ํด๋์ค์
๋๋ค ์ด๊ฒ์ ์ฑ๊ธํค ํด๋์ค์
๋๋ค
์ด ์์ ์์ ๊ธฐ๋ณธ ๋ฉ์๋๋ getInstance() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ Singleton ํด๋์ค ์ ์ธ์คํด์ค 3๊ฐ๋ฅผ ์์ฑํฉ๋๋ค . getInstance () ๋ฉ์๋๋ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ๋ฐํํ๊ณ ์ธ์คํด์ค๊ฐ ์์ผ๋ฉด ์ ์ธ์คํด์ค๋ฅผ ๋ง๋ญ๋๋ค. displayMessage ๋ฉ์๋ ๋ ๋จ์ง ๋ฉ์์ง๋ฅผ ์ธ์ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ ๊ธฐ๋ณธ ๋ฉ์๋๋ ๊ฐ ์ธ์คํด์ค์ ๋ํด displayMessage ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค . ๋ณด์๋ค์ํผ, ์ธ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋๋ผ๋ ํ๋์ ๊ฐ์ฒด๋ง ์์ฑ๋์ด ๋ชจ๋ ์ธ์คํด์ค์์ ๊ณต์ ๋ฉ๋๋ค. ์ด๋ ํด๋์ค์ ์ธ์คํด์ค๊ฐ ํ๋๋ง ์์ฑ๋์ด ๋ชจ๋ ์ธ์คํด์ค ๊ฐ์ ๊ณต์ ๋๋ฉฐ ์ ์ ๋ณ์ 'instance'๊ฐ ๋ณด์ ํ๊ณ ์์์ ์ฆ๋ช
ํฉ๋๋ค. ์ ์ ๋ณ์ ์ ๊ฐ์ ํด๋์ค์ ๋ชจ๋ ์ธ์คํด์ค์์ ๊ณต์ ๋๋ฏ๋ก ํ ์ธ์คํด์ค์์ ๋ณ๊ฒฝํ ๋ณ์๊ฐ ๋ค๋ฅธ ๋ชจ๋ ์ธ์คํด์ค์์๋ ํ์๋๋ค๋ ์ ์ ๊ธฐ์ตํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ์ฝ๋์์ ์ ์ฌ์ ์ธ ์ถฉ๋๊ณผ ๋ฒ๊ทธ๋ฅผ ๋ฐฉ์งํ๋ ค๋ฉด ์ ์ ๋ณ์๋ฅผ ์ ์คํ๊ฒ ๊ณ ๋ คํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๊ณ์ ์์ก๊ณผ ๊ฐ์ด ํด๋์ค ์ธ์คํด์ค์ ํน์ ํ ๊ฐ์ ๋ณด์ ํ๊ธฐ ์ํด ์ ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅ๋์ง ์์ต๋๋ค. ๋ฐฐ์ด ๋ด์ฉ์ ๊ฐํํ๋ ค๋ฉด Java ๊ณผ์ ์ ๋น๋์ค ๊ฐ์๋ฅผ ์์ฒญํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
GO TO FULL VERSION