싱글톤 ( Singleton pattern )이란 싱글톤 패턴을 따르는 클래스의 생성자가 여러 차례 호출되더라도 실제로 생성되는 객체는 하나이고 최초 생성 이후에 호출된 생성자는 최초의 생성자가 생성한 객체를 리턴하는 것이다.
간단히 말하자면 싱글톤 패턴이 되어 있는 클래스에서 생성자를 받으면 그 자기 자신을 메모리 변수에 저장을 해서 다시 데이터 가져올 수 있도록 하는 기능임
dart 언어
class User{
/// [User]
String id;
String type;
String name;
String nickname;
String email;
String phone;
String birthday;
String age;
String agree;
User({
@required this.id,
@required this.type,
@required this.name,
@required this.nickname,
@required this.email,
@required this.phone,
@required this.birthday,
@required this.age,
@required this.agree
}){
/// 메모리 변수에 자기 자신 저장
sign = this;
}
/// 자기 자신을 저장할 메모리 변수
static User sign;
/// 다시 정보를 다져올떄
factory User.instance() => sign;
Java
public class User{
private static User instance;
}