/** * 访问者模式:就是不同的人(访问者)对同一种资源(衣服)下的不同分类的表现不一致:举个例子,小孩,男人,女人,去服装店里逛,对童装,男装,女装的感兴趣是不一样的; * 这里小孩,男孩,女人就是访问者,男装,女装,童装就是资源或者elment,服装店就是对象结构: * * 小孩逛商店:对童装感兴趣 男人逛商店:对男装感兴趣,也会给小朋友买衣服,女人对童装,女装,男装都会感兴趣,因为可能会给子女,老公买衣服: * 所以访问者模式的结构: * * 1.一个访问者接口Visistor和多个实现类(如本例子中的男人,女人,小孩),该接口会有访问各种资源的方法,如本例中,每个访问者都会对男装,女装,童装进行访问 * 2.资源接口Elemnt和多个实现类,如本例的童装,男装,女装,每个资源类都会提供一个接受访问者的接口,在该接口调用访问者访问该资源的方法 * 3.一个对象结构:如本例的服装店,简单来说就是存储资源集合的对象 * * * * * */ //访问者 public interface Visistor { //访问者可以访问所有的资源子类(这里是重点,有多少个子类,就添加多少个访问方法) void buyChildrenCloth(ChildrenCloth cloth); void buyManCloth(ManCloth cloth); void buyWomenCloth(WomenCloth cloth); //统计每个访问者买衣服总花费金额 void cost(); } //访问者---小孩 public class Children implements Visistor { private BigDecimal amount=BigDecimal.ZERO; @Override public void buyChildrenCloth(ChildrenCloth cloth) { System.out.println("我是小朋友,买衣服只会买小朋友的衣服"); amount=amount.add(cloth.getAmount()); } @Override public void buyManCloth(ManCloth cloth) { System.out.println("我是小朋友,不会买妈妈的衣服"); } @Override public void buyWomenCloth(WomenCloth cloth) { System.out.println("我是小朋友,不会买爸爸的衣服"); } @Override public void cost() { System.out.println("我是小朋友这次买衣服总花费:"+amount.toString()); } } //访问者--男孩 public class Man implements Visistor { private BigDecimal amount=BigDecimal.ZERO; @Override public void buyChildrenCloth(ChildrenCloth cloth) { System.out.println("我是爸爸,我会帮小朋友买衣服"); amount=amount.add(cloth.getAmount()); } @Override public void buyManCloth(ManCloth cloth) { System.out.println("我是爸爸,我不会买老婆的衣服"); } @Override public void buyWomenCloth(WomenCloth cloth) { System.out.println("我是爸爸,我会买自己的衣服"); amount=amount.add(cloth.getAmount()); } @Override public void cost() { System.out.println("我是爸爸,这次买衣服总花费:"+amount.toString()); } } //访问者--女孩 public class Women implements Visistor { private BigDecimal amount=BigDecimal.ZERO; @Override public void buyChildrenCloth(ChildrenCloth cloth) { System.out.println("我是妈妈,我会给小朋友买衣服"); amount=amount.add(cloth.getAmount()); } @Override public void buyManCloth(ManCloth cloth) { System.out.println("我是妈妈,我会给老公买衣服"); amount=amount.add(cloth.getAmount()); } @Override public void buyWomenCloth(WomenCloth cloth) { System.out.println("我是妈妈,我会给自己买衣服"); amount=amount.add(cloth.getAmount()); } @Override public void cost() { System.out.println("我是妈妈,这次买衣服总花费:"+amount.toString()); } } //资源Element public abstract class Cloth { protected BigDecimal amount; //资源接受的访问者 abstract void accept(Visistor visistor); } //资源--童装 public class ChildrenCloth extends Cloth{ public ChildrenCloth(BigDecimal amount) { this.amount = amount; } public BigDecimal getAmount() { return amount; } @Override public void accept(Visistor visistor) { visistor.buyChildrenCloth(this); } } //资源--男装 public class ManCloth extends Cloth { public ManCloth(BigDecimal amount) { this.amount = amount; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } @Override public void accept(Visistor visistor) { visistor.buyManCloth(this); } } //资源女装 public class WomenCloth extends Cloth { public WomenCloth(BigDecimal amount) { this.amount = amount; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } @Override public void accept(Visistor visistor) { visistor.buyWomenCloth(this); } } //结构对象,存储资源 public class ClothShop { private List<Cloth> list=new ArrayList<>(); public void addCloth(Cloth cloth){ list.add(cloth); } //不同访问者逛商店 public void show(Visistor visistor){ for (Cloth cloth : list) { cloth.accept(visistor); } } } public class Test { public static void main(String[] args) { //创建一个服装店,并进货童装,男装,女装 ClothShop clothShop = new ClothShop();//服装店 ChildrenCloth childrenCloth = new ChildrenCloth(new BigDecimal("100"));//童装 ManCloth manCloth = new ManCloth(new BigDecimal("200"));//男装 WomenCloth womenCloth = new WomenCloth(new BigDecimal("300"));//女装 clothShop.addCloth(childrenCloth);//进货童装 clothShop.addCloth(manCloth);//进货男装 clothShop.addCloth(womenCloth);//进货女装 //儿童逛服装店 Children children = new Children();//儿童 clothShop.show(children);//逛店 children.cost();//总花费 //老公逛服装店 Man man = new Man(); //老公 clothShop.show(man);//逛店 man.cost();//总花费 //老婆逛服装店 Women women = new Women(); //老婆 clothShop.show(women);//逛店 women.cost();//总花费 } }