๐Ÿ”Ž [Flutter&Dart] Singleton ? Static ? Class ? .. ์ฐจ์ด์ ์„ Instance ๊ด€์ ์—์„œ ์•Œ์•„๋ณด์ž

Tygerยท2024๋…„ 3์›” 10์ผ
4

Flutter

๋ชฉ๋ก ๋ณด๊ธฐ
55/64

๐Ÿ”Ž Singleton ? Static ? Class ? .. ์ฐจ์ด์ ์„ Instance ๊ด€์ ์—์„œ ์•Œ์•„๋ณด์ž

[Dart] Class ์ด ์ •๋ฆฌ (์ƒ์† / ์žฌ์ •์˜ / ์ถ”์ƒํ™”)
Singleton Pattern

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Singleton? Static? Class? ์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ์ด ๋“ค์˜ ์ฐจ์ด์ ์€ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฅธ ๊ฐœ๋…๋“ค์ด๊ณ , ์ •ํ™•ํ•˜๊ฒŒ ๊ฐœ๋…๋“ค์„ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Class, Static์€ Flutter ๊ฐœ๋ฐœ์„ ํ•˜์ง€ ์•Š์•˜๋”๋ผ๋„ ์ž์ฃผ ์ ‘ํ•˜๊ณ  ์‚ฌ์šฉํ•ด๋ณธ ๊ฐœ๋…๋“ค์ด๋‹ค.
Singleton์— ๋Œ€ํ•ด์„œ๋Š” ๋‚ฏ์„  ๊ฐœ๋…์ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด๋ฏธ Flutter ๊ฐœ๋ฐœ์‹œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค ์ค‘์— Singleton์˜ ๊ฐœ๋…์œผ๋กœ ๊ฐœ๋ฐœ๋œ ๊ธฐ๋Šฅ๋“ค์ด ๋‹ค์ˆ˜์žˆ์–ด ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๊ฐœ๋…๋“ค์„ ์ฐจ์ด์  ์œ„์ฃผ๋กœ ๋น„๊ตํ•ด ๋ณด๋ฉด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

Classes
Dart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes except Null descend from Object. Mixin-based inheritance means that although every class (except for the top class, Object?) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods are a way to add functionality to a class without changing the class or creating a subclass. Class modifiers allow you to control how libraries can subtype a class.

Instance

๋จผ์ € ์ด ๋“ค์˜ ์ฐจ์ด์ ์„ ์•Œ์•„๋ณด๊ธฐ์— ์•ž์„œ Instance๋ผ๋Š” ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

Instance๋ž€ ๋ฌด์—‡์ผ๊นŒ ?

์ธ์Šคํ„ด์Šค๋ผ๋Š” ๋ง์€ ์ž์ฃผ ๋“ฃ๊ณ  ์‚ฌ์šฉํ•ด ๋ดค์„ ๊ฒƒ์ด๋‹ค.

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

์„ค๋ช…์ด ์ž˜ ์ดํ•ด๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์‰ฝ๊ฒŒ ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜์ž๋ฉด Class๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ง€๊ณ , ์ƒ์„ฑ๋œ Class๊ฐ€ ์‚ฌ์šฉ๋˜์–ด ์ง€๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น ๋˜์—ˆ์„ ๋•Œ, ์ด๋ฅผ Object(๊ฐ์ฒด)๋ผ๊ณ  ํ•œ๋‹ค.
์œ„ ์ „๋ฐ˜์˜ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜๋Š” ๊ณผ์ •์„ ์ธ์Šคํ„ด์Šคํ™”๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.

์ด์ฒ˜๋Ÿผ Instance๋ผ๋Š” ๊ฐœ๋…์€ Class๊ฐ€ Object(๊ฐ์ฒด)๋กœ ์„ ์–ธ๋˜๊ณ , ์„ ์–ธ๋˜์–ด์ง„ Object(๊ฐ์ฒด)๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น ๋˜์–ด ์‹ค์ œ ์‚ฌ์šฉ๋  ๋•Œ๊ฐ€ ๋ฐ”๋กœ Instance(์ธ์Šคํ„ด์Šค)์ด๋‹ค.
์ฆ‰, Object(๊ฐ์ฒด)๋ฅผ ์‹ค์ฒดํ™”ํ•œ ๊ฐœ๋…์ด๋‹ค

Class

Class๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๋„๋ก ํ•˜์ž.

๋ฌธ์ž์—ด์˜ name, ์ •์ˆ˜ํ˜• age ๋ณ€์ˆ˜๋ฅผ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ Person ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค.

class Person {
  final String name;
  final int age;
  
  const Person(this.name, this.age);
}

์ด์ œ Person ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ด๋ณด๋„๋ก ํ•˜์ž.

Constructor(์ƒ์„ฑ์ž)๋ฅผ ํ†ตํ•ด Person ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค ํ•˜์˜€๋‹ค. ์ด์ œ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— Person ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์ด๋‹ค.

void main() {
  Person _person = Person("tyger", 100);
}

Static

Static์€ ์ •์ . ์ฆ‰, ๊ณ ์ • ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

๊ณ ์ •๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ด ๋ฌด์Šจ ์˜๋ฏธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฑธ๊นŒ ?

Static์€ Static Method, Static Variable์ด ์žˆ๋‹ค.

Static์œผ๋กœ ์„ ์–ธ๋˜๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์‹คํ–‰๋ถ€ํ„ฐ ์ข…๋ฃŒ๊นŒ์ง€์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ์ฃผ๊ธฐ ๋™์•ˆ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๊ณ ์ • ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ผ๋ฐ˜์ ์ธ Class๊ฐ€ ์ƒ์„ฑ๋˜์–ด์งˆ ๋•Œ์˜ ์ธ์Šคํ„ด์Šค ๊ณผ์ •์„ ๊ฑฐ์น˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ Static ๊ตฌ๋ฌธ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์‹คํ–‰๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์˜ฌ๋ ค์กŒ์„ ๋•Œ ๋‹จ ํ•œ๋ฒˆ๋งŒ ๋ณ„๋„์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ํ• ๋‹น๋˜์–ด ์ง„๋‹ค.
์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ Static ๊ตฌ๋ฌธ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ณด ๋˜ํ•œ ๊ณต์œ ๋œ ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค.

Static ๊ตฌ๋ฌธ์€ GC(Garbage Collector)์˜ ๋Œ€์ƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ ๊ตฌ๋ฌธ์„ Static์œผ๋กœ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์„ ์ ์œ ํ•˜๊ฒŒ ๋˜์–ด ํผํฌ๋จผ์Šค๋ฉด์—์„œ ์ข‹์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ์—, ์ด ์ ์„ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

GC(Garbage Collector)
๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์„ ์‹œ์Šคํ…œ์ด ์ž๋™์ ์œผ๋กœ ํ•ด์ œํ•ด ์ฃผ๊ณ , ๊ฐ€์šฉ์ƒํƒœ๋กœ ๋Œ๋ ค์ฃผ๋Š” ๊ธฐ๋Šฅ. ํ•˜์ง€๋งŒ, ์‹คํ–‰ ์กฐ๊ฑด์€ ์ผ์ •ํ•˜์ง€ ์•Š์Œ.

๋ฌธ์ž์—ด name ๋ณ€์ˆ˜๋ฅผ Static Variable(์ •์ ๋ณ€์ˆ˜)๋กœ ์ƒ์„ฑํ•˜์˜€๊ณ , name์„ ์ถœ๋ ฅํ•˜๋Š” printName ๋ฉ”์†Œ๋“œ๋ฅผ Static Method(์ •์ ๋ฉ”์†Œ๋“œ)๋กœ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

class Person {
  static final String name = "tyger";
  
  static void printName() {
    print(name);
  }
}

Static ๊ตฌ๋ฌธ์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ ๋ณ„๋„์˜ ์ธ์Šคํ„ด์Šคํ™” ๊ณผ์ • ์—†์ด ์ด๋ฏธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์‹คํ–‰๋  ๋•Œ์— ๋ณ„๋„์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ํ• ๋‹น๋˜์–ด ์กŒ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ”๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

void main() {
  Person.printName();
  String _name = Person.name;
}

Singleton

"In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one โ€œsingleโ€ instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton."

์ด๋ฒˆ์—” Singleton์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

Singleton์€ ์ƒ์†Œํ•˜๊ณ  ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์ง€๋งŒ ์–ด๋ ค์šด ๊ฐœ๋…์ด ์•„๋‹ˆ๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋œ๋‹ค.

Singleton Pattern

Flutter๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐœ๋ฐœ์„ ํ•ด๋ณธ ๊ฒฝ์šฐ, shared_preferences, firebase ๋“ฑ์˜ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ ๊ฒฝํ—˜์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

ํ•ด๋‹น ํŒจํ‚ค์ง€๋Š” ๋ชจ๋‘ Singleton Pattern์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐœ๋ฐœ๋˜์—ˆ๋‹ค.

Singleton Pattern์€ ๋‹จ ํ•˜๋‚˜์˜ Instance๋งŒ ์ƒ์„ฑํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด ๊ธฐ๋ฒ•์ด๋‹ค.

๋จผ์ € Singleton์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

Singleton์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

_(์–ธ๋”๋ฐ”)๋ฅผ ์‚ฌ์šฉํ•œ private์œผ๋กœ Constructor(์ƒ์„ฑ์ž)๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

class Person {
  static final Person instance = Person._internal();
  factory Person() => instance;
  Person._internal();
}

Singleton์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ Instance ๋ฐฉ๋ฒ•์€ Static ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ณ , Class๋ฅผ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šคํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.

void main() {
  Person _person1 = Person.instance;
  Person _person2 = Person();
}

์ด๋ฒˆ์—๋Š” Named Constructor๋กœ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜์ž.

class Person {
  static final Person instance = Person.init();
  factory Person() => instance;
  Person.init();
}

์ฐจ์ด์ 

์ง€๊ธˆ๊นŒ์ง€ Singleton, Static, Class์— ๋Œ€ํ•ด์„œ Instance ๊ด€์ ์—์„œ ๊ฐ๊ฐ์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์‚ดํŽด ๋ณด์•˜๋‹ค.

Class๋Š” ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•  ๋•Œ๋งˆ๋‹ค Instance๊ฐ€ ๋งค๋ฒˆ ์ƒ์„ฑ์ด ๋œ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๋ฐ˜๋ฉด Singleton์€ ๋‹จ ํ•˜๋‚˜์˜ Instance๋งŒ์„ ์ƒ์„ฑํ•˜๋„๋ก ๊ฐ•์ œํ•˜๋Š” ํŒจํ„ด ๊ธฐ๋ฒ•์ด๋ผ๊ณ  ํ–ˆ๋‹ค.
Static์€ Class, Singleton๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ธ์Šคํ„ด์Šคํ™” ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์‹คํ–‰์‹œ ๋ณ„๋„์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ํ• ๋‹น์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•ด๋ณด์ž.

Person์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ธ๋ฐ, ๋ณ€์ˆ˜๋กœ๋Š” ๊ฐ๊ฐ name์„ ๋„ฃ์–ด์„œ ์ƒ์„ฑํ•ด ์ค„ ๊ฒƒ์ด๋‹ค.

Class

class PersonClass {
  final String name = "tyger";
}

Singleton

class PersonSingleton {
  static final PersonSingleton instance = PersonSingleton._internal();
  factory PersonSingleton() => instance;
  PersonSingleton._internal();
    
  final String name = "tyger";
}

์ด์ œ PersonClass๋ฅผ ์‚ฌ์šฉํ•ด 3๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๋„๋ก ํ•˜์ž.

void main() {
  PersonClass _personClass1 = PersonClass();  
  PersonClass _personClass2 = PersonClass();  
  PersonClass _personClass3 = PersonClass();  
}

์ด์ œ ๊ฐ์ฒด๋ฅผ ์‹๋ณ„ํ•  ์ •์ˆ˜ํ˜• ๊ณ ์œ  ๊ฐ’์ธ hashCode๋ฅผ ์ถœ๋ ฅํ•ด ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋™์ผํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž.

hashCode ๊ฐ’์ด ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด ๋œปํ•˜๋Š” ์˜๋ฏธ๋Š” ๊ฐ๊ฐ์˜ ๊ฐ์ฒด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ Instance๋กœ ์ƒ์„ฑ์ด ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

Person ํด๋ž˜์Šค๋Š” 3๊ฐœ์˜ ์ธ์Šคํ„ด์Šคํ™” ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด์„œ ๊ฐ๊ฐ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์„ ํ• ๋‹น ๋ฐ›๊ฒŒ ๋œ๋‹ค.

void main() {
  print(_personClass1.hashCode);
  print(_personClass2.hashCode);
  print(_personClass3.hashCode);
  // 160990205
  // 519856303
  // 1035798228
}

์ด๋ฒˆ์—” ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ƒ์„ฑํ•œ PersonSingleton ํด๋ž˜์Šค๋ฅผ 3๊ฐœ์˜ ๊ฐ์ฒด๋กœ ์„ ์–ธํ•ด๋ณด์ž.

void main() {  
  PersonSingleton _personSingleton1 = PersonSingleton.instance;
  PersonSingleton _personSingleton2 = PersonSingleton.instance;
  PersonSingleton _personSingleton3 = PersonSingleton.instance;
}

์ด๋ฒˆ์—๋„ hashCode๋ฅผ ์ถœ๋ ฅํ•ด ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•œ ๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ƒ์„ฑ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

๋ชจ๋‘ ๋™์ผํ•œ hashCode ๊ฐ’์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ์ด ๋œ ๊ฒƒ์ด๋‹ค.

void main() {
  print(_personSingleton1.hashCode);
  print(_personSingleton2.hashCode);
  print(_personSingleton3.hashCode);
  // 1050882430
  // 1050882430
  // 1050882430

}

Static

๋งˆ์ง€๋ง‰์œผ๋กœ Static ๋ณ€์ˆ˜๋กœ name์„ ์ƒ์„ฑํ•ด ์ฃผ๋„๋ก ํ•˜์ž.

Person ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค์—์„œ๋„ name์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด getter๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.

changed() ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ static ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด ์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค.

class Person {
  String get nameGetter => name;
  
  static String name = "tyger";
  
  void changed(String data){
    name = data;
  }
}

์ƒ์„ฑํ•œ Person ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ•œ ํ›„ getter๋ฅผ ์‚ฌ์šฉํ•ด name ๊ฐ’์„ ์ถœ๋ ฅํ•ด ๋ณด๋„๋ก ํ•˜์ž.

_person1, _person2๋Š” ๋™์ผํ•œ Class๋ฅผ ์ธ์Šคํ„ด์Šค ํ•˜์˜€์ง€๋งŒ, ์ธ์Šคํ„ด์Šค๋Š” 2๊ฐœ๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์ด๋‹ค.

๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค์— ์žˆ๋Š” name ๊ฐ’์€ ๋‹น์—ฐํžˆ ๋™์ผํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ์ฃผ๊ณ  ์žˆ๋‹ค.

void main() {
  Person _person1 = Person();
  Person _person2 = Person();
  
  print(_person1.nameGetter);
  print(_person2.nameGetter);
  // tyger
  // tyger
}

์ž ์ด๋ฒˆ์—” _person1 ์ธ์Šคํ„ด์Šค์—์„œ๋งŒ name ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ๋ณด์ž.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ ?

_person1์˜ name ๋ณ€์ˆ˜๋งŒ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์ธ _person2 ์ธ์Šคํ„ด์Šค๋„ ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Static์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹น์—ฐํžˆ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋„ ๊ณต์œ ํ•˜๊ฒŒ ๋œ๋‹ค.

void main() {
  Person _person1 = Person();
  Person _person2 = Person();
  _person1.changed("alice");
  
  print(_person1.nameGetter);
  print(_person2.nameGetter);
  // alice
  // alice
}

๋งˆ๋ฌด๋ฆฌ

Class, Singleton, Static์„ ์ธ์Šคํ„ด์Šค์˜ ๊ฐœ๋…๊ณผ ๊ด€์ ์œผ๋กœ ๋ฐ”๋ผ๋ณธ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

๋ถˆํ•„์š”ํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŽ์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ๋„ ์ข‹์ง€ ์•Š์ง€๋งŒ, ์•ฑ์˜ ์‹คํ–‰ ์ฃผ๊ธฐ ๊ณผ์ •์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ ์œ ๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜์—ฌ ํผํฌ๋จผ์Šค์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ ์ €ํ•˜ ๋ถ€๋ถ„๋„ ๊ณ ๋ คํ•ด์„œ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์ด๋‚˜ ์ถ”๊ฐ€ํ•˜๋ฉด ์ข‹์„ ๋‚ด์šฉ ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š” !

profile
Flutter Developer

0๊ฐœ์˜ ๋Œ“๊ธ€