如果我要满足您的要求,我将有以下阵容:
interface Calendar
class DefaultCalendar implements Calendar //<- default implementation for calendar. will hold the implementation for the common behavior for all the calendars
class PersonalCalendar implements Calendar //<- implementation for calendar that holds personal events, decorator for DefaultCalendar
class SportsCalendar implements Calendar //<- implementation for calendar that holds sports events, decorator for DefaultCalendar
class WorkCalendar implements Calendar //<- implementation for calendar that holds work events, decorator for DefaultCalendar
class Event //<- the event object, setters & getters
class CalendarManager //<- object for adding, removing calendars
我将使用TreeMap
which 是SortedMap
利用要放置的事件的自然顺序的实现。
Map<String, Calendar> calendars = new TreeMap<String, Calendar>();
我还将在事件名称前加上特定于日历的单词,以便在显示时按日历类型(因此,事件类型)对事件进行分组
@Override public void addEvent(Event event){
//event.setName("Work Event : " + event.getEventName()); for work event
//event.setName("Sports Event : " + event.getEventName()); for sports event
//event.setName("Personal Event : " + event.getEventName()); for personal event
calendar.addEvent(event);
}
因此,当要求经理显示所有事件时,将导致
Personal Event : blah blah blah abc 123
Personal Event : blah blah blah abc 123
Sports Event : blah blah blah abc 123
Work Event : blah blah blah abc 123
现在,为了证明我在说什么,这是我的可运行代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
/* the demo class */
public class CalendarDemo {
public static void main(String[] args) throws ParseException{
CalendarManager manager = new CalendarManager();
Calendar mySportsCalendar = new SportsCalendar(new DefaultCalendar());
Calendar myWorkCalendar = new WorkCalendar(new DefaultCalendar());
Calendar myPersonalCalendar = new PersonalCalendar(new DefaultCalendar());
Event myBirthday = new Event("My 23rd Birthday Celebration", "September 17, 2013", "This marks my 23rd birthday celebration!");
Event myMothersBirthday = new Event("My Mother's 50th Birthday Celebration", "December 19, 2013", "This marks my mothers 50th birthday celebration!");
Event tennisGame = new Event("Tennis Game With Denver", "August 11, 2013", "3rd tennis game with my nephew.");
Event newProjectMeeting = new Event("New Project Meeting", "August 11, 2013", "Meeting with our new project.");
myPersonalCalendar.addEvent(myBirthday);
myPersonalCalendar.addEvent(myMothersBirthday);
myWorkCalendar.addEvent(newProjectMeeting);
mySportsCalendar.addEvent(tennisGame);
manager.addCalendar("sportsCalendar", mySportsCalendar);
manager.addCalendar("workCalendar", myWorkCalendar);
manager.addCalendar("personalCalendar", myPersonalCalendar);
System.out.println("Before the removal of personal calendar : ");
System.out.println("--------------------------------------------------------------------------------");
System.out.println(manager.displayAllCalendarEvents());
manager.removeCalendar("personalCalendar"); //<- will delete all the events in personalCalendar;
System.out.println("After the removal of personal calendar : ");
System.out.println("--------------------------------------------------------------------------------");
System.out.println(manager.displayAllCalendarEvents());
}
}
/* the calendar interface */
interface Calendar{
Event getEvent(String eventName);
List<Event> getEvents();
void addEvent(Event event);
String getDescription();
void setDescription(String desc);
}
/* the default implementation of calendar */
class DefaultCalendar implements Calendar{
private List<Event> events;
private String description;
public DefaultCalendar(){
events = new ArrayList<Event>();
}
@Override public Event getEvent(String eventName) {
for(Event event : events){
if(event.getEventName().equals(eventName))
return event;
}
throw new IllegalArgumentException("Event name does not exist in the event list.");
}
@Override public void addEvent(Event event) {
events.add(event);
}
@Override public String getDescription(){
return description;
}
@Override public void setDescription(String desc){
description = desc;
}
@Override public List<Event> getEvents() {
return events;
}
}
/* decorators for defaultcalendar. If you want
* specific behavior for a specific calendar, you can
* specify it in these classes.
* ---------------------------------------------- */
class SportsCalendar implements Calendar{
private Calendar calendar;
public SportsCalendar(Calendar baseCalendar){
calendar = baseCalendar;
}
@Override public String getDescription(){
return calendar.getDescription();
}
@Override public void addEvent(Event event){
event.setEventName("Sports Event : " + event.getEventName());
calendar.addEvent(event);
}
@Override public Event getEvent(String eventName) {
return calendar.getEvent("Sports Event : " + eventName);
}
@Override public void setDescription(String desc) {
//do something to the passed desc
//...
//then pass it to the wrapped calendar;
calendar.setDescription(desc);
}
@Override public List<Event> getEvents() {
return calendar.getEvents();
}
}
/* another decorator */
class WorkCalendar implements Calendar{
private Calendar calendar;
public WorkCalendar(Calendar baseCalendar){
calendar = baseCalendar;
}
@Override public String getDescription(){
return calendar.getDescription();
}
@Override public void addEvent(Event event){
event.setEventName("Work Event : " + event.getEventName());
calendar.addEvent(event);
}
@Override public Event getEvent(String eventName) {
return calendar.getEvent("Work Event : " + eventName);
}
@Override public void setDescription(String desc){
//do something to the passed desc
//...
//then pass it to the wrapped calendar;
calendar.setDescription(desc);
}
@Override public List<Event> getEvents() {
return calendar.getEvents();
}
}
/* another decorator */
class PersonalCalendar implements Calendar{
private Calendar calendar;
public PersonalCalendar(Calendar baseCalendar){
calendar = baseCalendar;
}
@Override public String getDescription(){
return calendar.getDescription();
}
@Override public void addEvent(Event event){
event.setEventName("Personal Event : " + event.getEventName());
calendar.addEvent(event);
}
@Override public Event getEvent(String eventName) {
return calendar.getEvent("Personal Event : " + eventName);
}
@Override public void setDescription(String desc) {
//do something to the passed desc
//...
//then pass it to the wrapped calendar;
calendar.setDescription(desc);
}
@Override public List<Event> getEvents() {
return calendar.getEvents();
}
}
/* the calendar manager, for adding & removing calendars */
class CalendarManager{
private Map<String, Calendar> calendars;
public CalendarManager(){
calendars = new TreeMap<String, Calendar>();
}
public void addCalendar(String key, Calendar calendar){
calendars.put(key, calendar);
}
public void removeCalendar(String key){
calendars.remove(key);
}
public String displayAllCalendarEvents(){
StringBuilder bdr = new StringBuilder();
for(String key : calendars.keySet()){
for(Event event : calendars.get(key).getEvents()){
bdr.append(event.getEventName())
.append("\t")
.append(event.getEventDate())
.append("\t")
.append(event.getEventDescription())
.append("\n");
}
}
return bdr.toString();
}
}
/* the event object */
class Event{
private String name;
private Date date;
private String description;
public Event(String name, String date, String description){
this.name = name;
try {
this.date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("December 19, 2013");
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date format!");
}
this.description = description;
}
public String getEventName() {
return name;
}
public Date getEventDate() {
return date;
}
public String getEventDescription() {
return description;
}
public void setEventName(String name) {
this.name = name;
}
public void setEventDate(String date) {
try {
this.date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("December 19, 2013");
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid date format!");
}
}
public void setEventDescription(String description) {
this.description = description;
}
}