编辑代码

import java.util.Scanner;

public class LibraryClient {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 初始化真实服务和代理类
        RealLibraryService realService = new RealLibraryService();
        BorrowProxy borrowProxy = new BorrowProxy(realService);
        ReturnProxy returnProxy = new ReturnProxy(realService);

        // 初始化调度中心
        LibraryTransactionCenter center = new LibraryTransactionCenter(borrowProxy, returnProxy);

        // 用户输入模拟流程
        System.out.print("请输入用户ID:");
        String userId = scanner.nextLine();

        System.out.print("请输入图书ID:");
        String bookId = scanner.nextLine();

        System.out.println("\n--- 开始借书流程 ---");
        center.processBorrow(userId, bookId);

        System.out.println("\n--- 开始还书流程 ---");
        center.processReturn(userId, bookId);

        scanner.close();
    }
}

// ===================== 接口定义 =====================
interface Borrowable {
    void borrow(String userId, String bookId);
}

interface Returnable {
    void returnItem(String userId, String bookId);
}

// ===================== 图书馆真实服务类 =====================
class RealLibraryService implements Borrowable, Returnable {
    @Override
    public void borrow(String userId, String bookId) {
        System.out.println("→ 正在处理图书借阅:用户 " + userId + " 借书 " + bookId);
    }

    @Override
    public void returnItem(String userId, String bookId) {
        System.out.println("→ 正在处理图书归还:用户 " + userId + " 归还书籍 " + bookId);
    }
}

// ===================== 代理类 =====================
class BorrowProxy implements Borrowable {
    private RealLibraryService realService;

    public BorrowProxy(RealLibraryService service) {
        this.realService = service;
    }

    @Override
    public void borrow(String userId, String bookId) {
        System.out.println(" 查找用户:" + userId + " 和图书:" + bookId);
        realService.borrow(userId, bookId);
        System.out.println(" 借书成功!记录已生成,短信通知已发送。");
    }
}

class ReturnProxy implements Returnable {
    private RealLibraryService realService;

    public ReturnProxy(RealLibraryService service) {
        this.realService = service;
    }

    @Override
    public void returnItem(String userId, String bookId) {
        System.out.println(" 验证归还信息:用户 " + userId + " 归还书籍 " + bookId);
        realService.returnItem(userId, bookId);
        System.out.println(" 归还完成!系统状态已更新,感谢您的配合。");
    }
}

// ===================== 事务调度中心类 =====================
class LibraryTransactionCenter {
    private Borrowable borrowService;
    private Returnable returnService;

    public LibraryTransactionCenter(Borrowable borrowService, Returnable returnService) {
        this.borrowService = borrowService;
        this.returnService = returnService;
    }

    public void processBorrow(String userId, String bookId) {
        borrowService.borrow(userId, bookId);
    }

    public void processReturn(String userId, String bookId) {
        returnService.returnItem(userId, bookId);
    }
}