[Thực hành] Triển khai Singleton

Mục đích

Luyện tập triển khai Singleton

Mô tả bài toán

Tạo chương trình thực hiện việc mượn sách. Mỗi đầu sách chỉ có một quyển. Mỗi người khi mượn sách, hệ thống kiểm tra xem quyển sách cần mượn có ai đó đã mượn chưa, nếu có người mượn rồi thì hiện thị thông báo “I don’t have the book”, còn lại sẽ thực hiện được việc mượn sách.

Vậy trong trường hợp này, đối tượng sách được tạo ra là duy nhất. Sử dụng mẫu thiết kế Singleton để minh hoạ ví dụ trên.

Các bước thực hiện

Bước 1: Tạo lớp BookSingleton với các thuộc tính: author, title, book, isLoanedOut

public class BookSingleton {
    private String author;
    private String title;
    private static BookSingleton book;
    private static boolean isLoanedOut;
}

Bước 2: Xây dựng phương thức khởi tạo cho lớp BookSingleton

private BookSingleton(){
    author = "Gamma, Helm, Johnson, and Vlissides";
    title  = "Design Patterns";
    book = null;
    isLoanedOut = false;
}

Bước 3: Xây dựng phương thức thực hiện mượn sách

public static BookSingleton borrowBook() {
    if (!isLoanedOut) {
        if (book == null) {
            book = new BookSingleton();
        }
        isLoanedOut = true;
        return book;
    }
    return null;
}

Bước 4: Xây dựng phương thức thực hiện trả sách

public void returnBook(BookSingleton bookReturned){
    isLoanedOut = false;
}

Bước 5: Một số phương thức get để lấy thông tin về sách

public String getAuthor() {
    return author;
}

public String getTitle() {
    return title;
}

public String getAuthorAndTitle(){
    return getTitle() + " by " + getAuthor();
}

Bước 6: Tạo lớp BookBorrower là người mượn sách.

Lớp này có 2 thuộc tính là đối tượng sách cần mượn và trạng thái mượn sách, mặc đinh lúc đầu là false.

private BookSingleton borrowedBook;
private boolean haveBook = false;

Bước 7: Tạo phương thức mượn sách borrowBook()

public void borrowBook(){
    borrowedBook = BookSingleton.borrowBook();

    if(borrowedBook == null){
        haveBook = false;
    }else {
        haveBook = true;
    }
}

Bước 8: Xây dựng phương thức lấy về thông tin sách mượn

public String getAuthorAndTitle(){
    if(haveBook){
        return borrowedBook.getAuthorAndTitle();
    }
    return "I don't have the book";
}

Bước 9: Xây dựng phương thức trả sách

public void returnBook(){
    borrowedBook.returnBook(borrowedBook);
}

Bước 10: Xây dựng lớp Test chứa phương thức main() để test ứng dụng như sau

public class Test {
    public static void main(String[] args) {
        System.out.println("BEGIN TESTING SINGLETON PATTERN");

        BookBorrower bookBorrower1 = new BookBorrower();
        BookBorrower bookBorrower2 = new BookBorrower();

        bookBorrower1.borrowBook();
        System.out.println("BookBorrower1 asked to borrow the book");
        System.out.println("BookBorrower1 Author and Title: ");
        System.out.println(bookBorrower1.getAuthorAndTitle());


        bookBorrower2.borrowBook();
        System.out.println("BookBorrower2 asked to borrow the book");
        System.out.println("BookBorrower2 Author and Title: ");
        System.out.println(bookBorrower2.getAuthorAndTitle());

        bookBorrower1.returnBook();
        System.out.println("BookBorrower1 returned the book");

        bookBorrower2.borrowBook();
        System.out.println("BookBorrower2 Author and Title: ");
        System.out.println(bookBorrower1.getAuthorAndTitle());

        System.out.println("END TESTING SINGLETON PATTERN");
    }
}

Bước 11: Chạy chương trình quan sát kết quả

BEGIN TESTING SINGLETON PATTERN
BookBorrower1 asked to borrow the book
BookBorrower1 Author and Title: 
Design Patterns by Gamma, Helm, Johnson, and Vlissides
BookBorrower2 asked to borrow the book
BookBorrower2 Author and Title: 
I don't have the book
BookBorrower1 returned the book
BookBorrower2 Author and Title: 
Design Patterns by Gamma, Helm, Johnson, and Vlissides
END TESTING SINGLETON PATTERN

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai.

Tài liệu + Khóa học lập trình FREE
Tài liệu + Khóa học lập trình FREE

DMCA.com Protection Status