In the command pattern, all requests for an object are passed to a common command interface. Each implementing class of the command interface executes a particular method of the passed-in object when called.
//The command interface (interface or abstract class)
public interface Command {
public void execute();
}
//The object to be requested for
public BookProcessor {
String name;
public BookProcessor(String n) {
name = n;
}
public void borrowBook() {
System.out.println("Borrowing book: " + name);
}
public void returnBook() {
System.out.println("Returning book: " + name);
}
}
//The concrete command classes
public class BorrowBook implements Command {
private BookProcessor bp;
public BorrowBook(String name) {
bp = new BookProcessor(name)
}
//the command method
public void execute() {
bp.borrowBook();
}
}
public class ReturnBook implements Command {
private BookProcessor bp;
public ReturnBook(String name) {
bp = new BookProcessor(name)
}
//the command method
public void execute() {
bp.returnBook();
}
}
//The Runner
public class CommandDemo {
public static void main(String[] args) {
Command borrow = new BorrowBook("Readers are Lucky");
borrow.execute();
Command return = new ReturnBook("Happy to finish");
return.execute();
}
}
----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
//The command interface (interface or abstract class)
public interface Command {
public void execute();
}
//The object to be requested for
public BookProcessor {
String name;
public BookProcessor(String n) {
name = n;
}
public void borrowBook() {
System.out.println("Borrowing book: " + name);
}
public void returnBook() {
System.out.println("Returning book: " + name);
}
}
//The concrete command classes
public class BorrowBook implements Command {
private BookProcessor bp;
public BorrowBook(String name) {
bp = new BookProcessor(name)
}
//the command method
public void execute() {
bp.borrowBook();
}
}
public class ReturnBook implements Command {
private BookProcessor bp;
public ReturnBook(String name) {
bp = new BookProcessor(name)
}
//the command method
public void execute() {
bp.returnBook();
}
}
//The Runner
public class CommandDemo {
public static void main(String[] args) {
Command borrow = new BorrowBook("Readers are Lucky");
borrow.execute();
Command return = new ReturnBook("Happy to finish");
return.execute();
}
}
----------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment