关闭

java设计模式进阶_mediator

标签: java设计模式
1346人阅读 评论(0) 收藏 举报
分类:

这里写图片描述

//
//
//  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

*/






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55336次
    • 积分:1626
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:42篇
    • 译文:42篇
    • 评论:8条