package shiyan24;

import java.util.Scanner;
public class Client {
    public static void main(String[] args) {
        DBOperator db = null;
        System.out.println("请选择:1、SQLServer 2、Oracle");
        Scanner input=new Scanner(System.in);
        int i=input.nextInt();
        if(i==1) {
            db=new SQLServerDB();
        }else if(i==2) {
            db=new OracleDB();
        }
        db.process();
    }
}
Client
package shiyan24;

public abstract class DBOperator {
    public void process(){
        connDB();
        openDB();
        useDB();
        closeDB();
    }
    public abstract void connDB();
    public void openDB(){
        System.out.println("打开数据库");
    }
    public void useDB(){
        System.out.println("使用数据库");
    }
    public void closeDB(){
        System.out.println("关闭数据库");
    }
}
DBOperator
package shiyan24;

public class OracleDB extends DBOperator{

    @Override
    public void connDB() {
        // TODO 自动生成的方法存根
        System.out.println("连接Oracle数据库");
    }

}
OracleDB
package shiyan24;

public class SQLServerDB extends DBOperator{

    @Override
    public void connDB() {
        // TODO 自动生成的方法存根
        System.out.println("连接SQLServer数据库");
    }

}
SQLServerDB

 

实验25

package shiyan25;

public class Apple implements Product{

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

public class Book implements Product{

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

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 product) {
        list.add(product);
    }

    public void removeProduct(Product product) {
        list.remove(product);
    }
}
BuyBasket
package shiyan25;

public class Client {

    public static void main(String[] args) {
        Product b1 = new Book();
        Product a1 = new Apple();
        Visitor visitor;
        
        BuyBasket basket = new BuyBasket();
        basket.addProduct(b1);
        basket.addProduct(a1);
        
        visitor = new Customer();
        visitor.setName("张三");
        basket.accept(visitor);
        System.out.println("**************************************************");

        visitor = new Packer();
        visitor.setName("李四");
        basket.accept(visitor);
        System.out.println("**************************************************");

        visitor = new Saler();
        visitor.setName("王五");
        basket.accept(visitor);
    }

}
Client
package shiyan25;

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 + "买书");
    }
    
}
Customer
package shiyan25;

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 + "给书打包");
    }
}
Packer
package shiyan25;

public interface Product {
    void accept(Visitor visitor);
}
Product
package shiyan25;

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 + "计算书的价格");
    }

}
Saler
package shiyan25;

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);
}
Visitor

 

 

posted on 2023-11-30 08:42  夜的第七章i  阅读(3)  评论(0编辑  收藏  举报