モノトーン
11595 ワード
シングルトーン(SingleTon)
SingleTonクラスの構成方法
例
モノトーンテスト
public class MySingleton {
private static MySingleton single;
private MySingleton() {
// cf. enum도 접근제어자가 private이었음..
}
public static MySingleton getInstance() {
if (single==null) {
single = new MySingleton();
}
return single;
}
// 나머지 내용들은 이 클래스로 처리할 내용들을 기술한다.
public void displayText() {
System.out.println("난 싱글톤패턴의 객체!");
}
}
public class SingletonTest {
public static void main(String[] args) {
// MySingleton test1 = new MySingleton(); //new명령 사용 불가
// getInstance() 이용하여 객체 생성
MySingleton test2 = MySingleton.getInstance();
test2.displayText();
MySingleton test3 = MySingleton.getInstance();
System.out.println("test2 = "+test2);
System.out.println("test3 = "+test3);
}
}
同じオブジェクトを参照!
メンバーをモノトーンとして使用
public class MemberDaoImpl implements IMemberDAO{
private static IMemberDAO memDAO;
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rs;
//외부에서 new 못함
private MemberDaoImpl() {
// TODO
}
public static IMemberDAO getInstance() {
if(memDAO == null) {
memDAO = new MemberDaoImpl();
}
return memDAO;
}
public class MemberServiceImpl implements IMemberService{
private static IMemberService memService;
private IMemberDAO memDAO;
private MemberServiceImpl() {
memDAO = MemberDaoImpl.getInstance();
}
public static IMemberService getInstance() {
if(memService == null) {
memService = new MemberServiceImpl();
}
return memService;
}
public class MemberInfoMain {
private IMemberService memService;
private Scanner scan = new Scanner(System.in);
public MemberInfoMain() {
memService = MemberServiceImpl.getInstance();
}
Reference
この問題について(モノトーン), 我々は、より多くの情報をここで見つけました
https://velog.io/@cheshirehyun/싱글톤
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
public class MySingleton {
private static MySingleton single;
private MySingleton() {
// cf. enum도 접근제어자가 private이었음..
}
public static MySingleton getInstance() {
if (single==null) {
single = new MySingleton();
}
return single;
}
// 나머지 내용들은 이 클래스로 처리할 내용들을 기술한다.
public void displayText() {
System.out.println("난 싱글톤패턴의 객체!");
}
}
public class SingletonTest {
public static void main(String[] args) {
// MySingleton test1 = new MySingleton(); //new명령 사용 불가
// getInstance() 이용하여 객체 생성
MySingleton test2 = MySingleton.getInstance();
test2.displayText();
MySingleton test3 = MySingleton.getInstance();
System.out.println("test2 = "+test2);
System.out.println("test3 = "+test3);
}
}
public class MemberDaoImpl implements IMemberDAO{
private static IMemberDAO memDAO;
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rs;
//외부에서 new 못함
private MemberDaoImpl() {
// TODO
}
public static IMemberDAO getInstance() {
if(memDAO == null) {
memDAO = new MemberDaoImpl();
}
return memDAO;
}
public class MemberServiceImpl implements IMemberService{
private static IMemberService memService;
private IMemberDAO memDAO;
private MemberServiceImpl() {
memDAO = MemberDaoImpl.getInstance();
}
public static IMemberService getInstance() {
if(memService == null) {
memService = new MemberServiceImpl();
}
return memService;
}
public class MemberInfoMain {
private IMemberService memService;
private Scanner scan = new Scanner(System.in);
public MemberInfoMain() {
memService = MemberServiceImpl.getInstance();
}
Reference
この問題について(モノトーン), 我々は、より多くの情報をここで見つけました https://velog.io/@cheshirehyun/싱글톤テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol