首页 > 其他分享 >20241127

20241127

时间:2024-11-28 21:14:18浏览次数:10  
标签:java void public Book org 20241127 example

软件设计                  石家庄铁道大学信息学院

 

实验 25:访问者模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解访问者模式的动机,掌握该模式的结构;

2、能够利用访问者模式法解决实际问题。

 

[实验任务一]:打包员

在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。

实验要求:

1. 画出对应的类图;

2. 提交源代码;

3. 注意编程规范。

 

 

 

 

  1. Apple.java

package org.example;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Apple implements Product{

    @Override

    public void accept(Visitor visitor) {

        visitor.visit(this);

    }

}

 

  1. Book.java

package org.example;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Book implements Product{

    @Override

    public void accept(Visitor visitor) {

        visitor.visit(this);

    }

}

 

  1. BuyBasket.java

package org.example;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

 

import java.util.ArrayList;

import java.util.Iterator;

 

public class BuyBasket {

    private ArrayList list = new ArrayList();

 

    public void accept(Visitor visitor) {

        Iterator i = list.iterator();

        while (i.hasNext()) {

            ((Product) i.next()).accept(visitor);

        }

    }

 

    public void addProduct(Product p) {

        list.add(p);

    }

 

    public void removeProduct(Product p) {

        list.remove(p);

    }

}

 

  1. Customer.java

package org.example;

 

import org.example.Book;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Customer extends Visitor{

    @Override

    public void visit(Apple apple) {

        System.out.println("顾客"+name+"付钱买苹果");

    }

 

    @Override

    public void visit(Book book) {

        System.out.println("顾客"+name+"付钱买书");

    }

}

 

  1. Packer.java

package org.example;

 

import org.example.Book;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Packer extends Visitor{

    @Override

    public void visit(Apple apple) {

        System.out.println("装包员"+name+"打包苹果");

    }

 

    @Override

    public void visit(Book book) {

        System.out.println("装包员"+name+"打包书");

    }

}

 

  1. Product.java

package org.example;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public interface Product {

    void accept(Visitor visitor);

}

 

  1. Saler.java

package org.example;

 

import org.example.Book;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Saler extends Visitor {

    @Override

    public void visit(Apple apple) {

        System.out.println("收银员" + name + "计算苹果的价格");

    }

 

    @Override

    public void visit(Book book) {

        System.out.println("收银员" + name + "计算书的价格");

    }

}

 

  1. Visitor.java

package org.example;

 

import org.example.Book;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public abstract class Visitor {

    protected String name;

 

    public void setName(String name) {

        this.name = name;

    }

 

    public abstract void visit(Apple apple);

 

    public abstract void visit(Book book);

}

  1. Client.java

package org.example;

 

/**

 * @author IronBro

 * @version 1.0

 * @description: TODO

 * @date 2024/11/27

 */

public class Client {

    public static void main(String[] args) {

        Product p1, p2;

        Visitor v1, v2, v3;

        p1 = new Apple();

        p2 = new Book();

        v1 = new Saler();

        v1.setName("小郭");

        v2 = new Customer();

        v2.setName("小王");

        v3 = new Packer();

        v3.setName("小张");

        BuyBasket basket = new BuyBasket();

        basket.addProduct(p1);

        basket.addProduct(p2);

        basket.accept(v1);

        basket.accept(v2);

        basket.accept(v3);

    }

}

 

 

标签:java,void,public,Book,org,20241127,example
From: https://www.cnblogs.com/gyg1222/p/18575178

相关文章