标签:package 每日 System 博客 public println void out
实验 24:模板方法模式
package rjsj.no24;
public abstract class Operator {
public abstract void connDB();
public void openDB(){
System.out.println("---打开数据库---");
}
public void useDB(){
System.out.println("---使用数据库---");
}
public void closeDB(){
System.out.println("---关闭数据库---");
}
public void display(){
this.connDB();
this.openDB();
this.useDB();
this.closeDB();
}
}
package rjsj.no24;
public class Oracle extends Operator{
@Override
public void connDB() {
System.out.println("连接---Oracle---数据库");
}
}
package rjsj.no24;
public class SQLServer extends Operator{
@Override
public void connDB() {
System.out.println("连接---SQLServer---数据库");
}
}
package rjsj.no24;
public class Client {
public static void main(String[] args) {
Operator dbOperator;
dbOperator = new SQLServer();
dbOperator.display();
System.out.println("————————————————————");
dbOperator = new Oracle();
dbOperator.display();
}
}
实验 25:访问者模式
package com.gazikel;
public interface Product {
void accept(Visitor visitor);
}
package com.gazikel;
public class Apple implements Product {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
package com.gazikel;
public class Book implements Product {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
package com.gazikel;
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);
}
package com.gazikel;
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 + "买书");
}
}
package com.gazikel;
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 + "给书打包");
}
}
package com.gazikel;
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 + "直接计算书的价格");
}
}
package com.gazikel;
import java.util.ArrayList;
import java.util.List;
public class BuyBasket {
private List<Product> products = new ArrayList();
public void accept(Visitor visitor) {
for (Product product : products) {
product.accept(visitor);
}
}
public void addProduct(Product p) {
products.add(p);
}
public void removeProduct(Product p) {
products.remove(p);
}
}
package com.gazikel;
public class Client {
public static void main(String[] args) {
BuyBasket buyBasket = new BuyBasket();
buyBasket.addProduct(new Apple());
buyBasket.addProduct(new Book());
Visitor saler = new Saler();
saler.setName("李华");
System.out.println("----收银员----");
buyBasket.accept(saler);
Visitor packer = new Packer();
packer.setName("赵强");
System.out.println("----打包员----");
buyBasket.accept(packer);
Visitor customer = new Customer();
customer.setName("王超");
System.out.println("----顾客----");
buyBasket.accept(customer);
}
}
标签:package,
每日,
System,
博客,
public,
println,
void,
out
From: https://www.cnblogs.com/LiuLyuancn/p/17870842.html