菜鸟的博客

纵有疾风起,人生不言弃。

导航

软件设计-Tutorial25

类图:

```mermaid
classDiagram
    class Item {
        <<interface>>
        +accept(Visitor visitor)
    }
    class Book {
        -String title
        +getTitle()
        +accept(Visitor visitor)
    }
    class Electronic {
        -String name
        +getName()
        +accept(Visitor visitor)
    }
    class Visitor {
        <<interface>>
        +visit(Book book)
        +visit(Electronic electronic)
    }
    class PriceCalculator {
        +visit(Book book)
        +visit(Electronic electronic)
    }
    class Packer {
        +visit(Book book)
        +visit(Electronic electronic)
    }
    class ShoppingCart {
        -List~Item~ items
        +addItem(Item item)
        +accept(Visitor visitor)
    }

    Item <|.. Book
    Item <|.. Electronic
    Visitor <|.. PriceCalculator
    Visitor <|.. Packer
    ShoppingCart --> Item
    Item --> Visitor

```

源码:

package Tutorial25;

import java.util.ArrayList;
import java.util.List;

// 商品接口
interface Item {
    void accept(Visitor visitor);
}

// 具体商品类:书
class Book implements Item {
    private String title;

    public Book(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

// 具体商品类:电子产品
class Electronic implements Item {
    private String name;

    public Electronic(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

// 访问者接口
interface Visitor {
    void visit(Book book);
    void visit(Electronic electronic);
}

// 具体访问者:价格计算器
class PriceCalculator implements Visitor {
    @Override
    public void visit(Book book) {
        System.out.println("Calculating price for book: " + book.getTitle());
    }

    @Override
    public void visit(Electronic electronic) {
        System.out.println("Calculating price for electronic: " + electronic.getName());
    }
}

// 具体访问者:打包员
class Packer implements Visitor {
    @Override
    public void visit(Book book) {
        System.out.println("Packing book: " + book.getTitle());
    }

    @Override
    public void visit(Electronic electronic) {
        System.out.println("Packing electronic: " + electronic.getName());
    }
}

// 购物车类
class ShoppingCart {
    private List<Item> items = new ArrayList<>();

    public void addItem(Item item) {
        items.add(item);
    }

    public void accept(Visitor visitor) {
        for (Item item : items) {
            item.accept(visitor);
        }
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 创建商品
        Item book1 = new Book("Java Programming");
        Item electronic1 = new Electronic("Smartphone");

        // 添加到购物车
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(book1);
        cart.addItem(electronic1);

        // 使用价格计算器访问者
        System.out.println("Price Calculation:");
        Visitor priceCalculator = new PriceCalculator();
        cart.accept(priceCalculator);

        System.out.println();

        // 使用打包员访问者
        System.out.println("Packing:");
        Visitor packer = new Packer();
        cart.accept(packer);
    }
}

 

posted on 2024-11-27 09:54  hhmzd233  阅读(1)  评论(0编辑  收藏  举报