Merge remote-tracking branch 'origin/main' into main
This commit is contained in:
commit
b94f1254e0
35
src/main/java/feature/Book.java
Normal file
35
src/main/java/feature/Book.java
Normal file
@ -0,0 +1,35 @@
|
||||
package feature;
|
||||
|
||||
public class Book {
|
||||
private String title;
|
||||
private String author;
|
||||
private int pages;
|
||||
|
||||
public Book(String title, String author, int pages) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.pages = pages;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public int getPages() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book{" +
|
||||
"title='" + title + '\'' +
|
||||
", author='" + author + '\'' +
|
||||
", pages=" + pages +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
15
src/main/java/feature/BookDemo.java
Normal file
15
src/main/java/feature/BookDemo.java
Normal file
@ -0,0 +1,15 @@
|
||||
package feature;
|
||||
|
||||
/**
|
||||
* create singleton using enum
|
||||
*/
|
||||
public class BookDemo {
|
||||
public static void main(String[] args) {
|
||||
Book book = new Book("Java Programming", "John Doe", 300);
|
||||
System.out.println(book);
|
||||
|
||||
// Using the singleton pattern
|
||||
BookEnum.INSTANCE.setBook(book);
|
||||
System.out.println(BookEnum.INSTANCE.getBook());
|
||||
}
|
||||
}
|
14
src/main/java/feature/BookEnum.java
Normal file
14
src/main/java/feature/BookEnum.java
Normal file
@ -0,0 +1,14 @@
|
||||
package feature;
|
||||
|
||||
public enum BookEnum {
|
||||
INSTANCE;
|
||||
|
||||
private Book book;
|
||||
|
||||
public Book getBook() {
|
||||
return book;
|
||||
}
|
||||
public void setBook(Book book) {
|
||||
this.book = book;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user