java设计模式进阶_mediator

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : PartyMember.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * PartyMember
**/
public interface PartyMember {
    public void joinedParty(Party party);
    public void partyAction(Action action);
    public void act(Action action);
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : PartyMemberBase.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * PartyMemberBase
**/
public abstract class PartyMemberBase implements PartyMember {
    protected Party party;

    public void joinedParty(Party party) {
        System.out.println(this + " joins the party");
        this.party = party;
    }

    public void partyAction(Action action) {
        System.out.println(this + " " + action.getDescription());
    }

    public void act(Action action) {
        if(party != null)
        {
            System.out.println(this + " " + action.toString());
            party.act(this,action);
        }
    }

    public abstract String toString();
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Hobbit.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * Hobbit
**/
public class Hobbit extends PartyMemberBase {

    public String toString() {
        return "Hobbit";
    }

}
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Rogue.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * Rogue
**/
public class Rogue extends PartyMemberBase {

    public String toString() {
        return "Rogue";
    }

}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Wizard.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * Wizard
**/
public class Wizard extends PartyMemberBase {

    public String toString() {
        return "Wizard";
    }

}
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Hunter.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * Hunter
**/
public class Hunter extends PartyMemberBase {

    public String toString() {
        return "Hunter";
    }

}
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Party.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * Party
**/
public interface Party {
    public void addMember(PartyMember partyMember);
    public void act(PartyMember partyMember, Action action);
}

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

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : PartyImpl.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




/**
 * PartyImpl
**/
public class PartyImpl implements Party {

    private final List<PartyMember> members;

    public PartyImpl() {
        members = new ArrayList<>();
    }

    public void act(PartyMember actor, Action action) {
        for(PartyMember member : members)
        {
            if(member != actor)
            {
                member.partyAction(action);
            }
        }
    }

    public void addMember(PartyMember member) {
        members.add(member);
        member.joinedParty(this);
    }

}

public enum Action {

    HUNT("hunted a rabbit","arrives for dinner"),
    TALE("tells a tale","comes to listen"),
    GOLD("found gold","takes his share of the gold"),
    ENEMY("spotted enemies","runs for cover"),
    NONE("","");

    private String title;
    private String description;

    Action(String title,String description)
    {
        this.title = title;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return title;
    }

}
public class App {

    public static void main(String[] args) {

        //create party and members
        Party party = new PartyImpl();
        Hobbit hobbit = new Hobbit();
        Wizard wizard = new Wizard();
        Rogue rogue = new Rogue();
        Hunter hunter = new Hunter();

        //add party members
        party.addMember(hobbit);
        party.addMember(wizard);
        party.addMember(rogue);
        party.addMember(hunter);

        //perform actions -> the other party members
        //are notified by the party
        hobbit.act(Action.ENEMY);
        wizard.act(Action.TALE);
        rogue.act(Action.GOLD);
        hunter.act(Action.HUNT);

    }

}
/*
Hobbit joins the party
Wizard joins the party
Rogue joins the party
Hunter joins the party
Hobbit spotted enemies
Wizard runs for cover
Rogue runs for cover
Hunter runs for cover
Wizard tells a tale
Hobbit comes to listen
Rogue comes to listen
Hunter comes to listen
Rogue found gold
Hobbit takes his share of the gold
Wizard takes his share of the gold
Hunter takes his share of the gold
Hunter hunted a rabbit
Hobbit arrives for dinner
Wizard arrives for dinner
Rogue arrives for dinner

*/






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值