面向对象设计/如何设置类结构

时间:2013-07-24 02:10:37

标签: java oop

我是面向对象编程的新手,我很想知道如何解决这个问题。

如果我有不同日历的列表,即工作日历,个人日历,体育日历等,并且每个日历都有各自的事件。我假设我有一个日历类,然后嵌套类将是事件类。由于我想在一个列表中显示所有日历中的所有事件,因此它们都会进入已排序的数组。但是我想说我想删除我的日历之一,比如我的工作日历,我如何确保从列表/数组中删除所有工作事件。我很好奇这个类结构将如何建立在一个高层次。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

以下是我将如何处理它,突出显示的部分是我会接受的:

如果我有一个不同的calendars列表,即work calendarpersonal calendarsports calendars等,并且每个人都有自己的events。由于我想在一个list上显示所有日历中的所有事件,因此它们都将进入已排序的数组。但是,我想我希望delete one of my calendars像我的工作日历一样,如何确保此列表/数组中的所有work events were deleted

这让我:

emum CalendarType
{
    WORK,
    PERSONAL,
    SPORT,
}

class Event
{
    // event data, like time, place, people, etc...
    private Calendar owner;
}

class Calendar
{
    private CalendarType type; // or make this a String and get rid of the enum 
                               // if people can make their own types
    private List<Event> event;s
}

关于想要将所有事件放在一个列表中的部分,并在日历有点困难时将其删除。

我认为我要做的是在CalendarUtils类中添加这样的方法:

public static void deleteEvents(Calendar c, List<Event> events)
{
    // loop through all the events, for each one from the calendar remove it
}

答案 1 :(得分:0)

您不一定需要嵌套您的课程。只需创建一个类,就像MadProgrammer建议的那样,这是一个事件,实现到您的Calendar类并根据需要使用它。如果要删除日历,可以包含一些确保从该日历内删除所有事件的方法。但是删除日历对象应该可以解决问题。

答案 2 :(得分:0)

public abstract class BaseCalendar{//do some common functionality here or define the abstract methods so child class will provide the implementation }
public class WorkCalendar extends BaseCalendar{//do WorkCalendar specific functionality here}
public class PersonalCalendar extends BaseCalendar{//do Personalcalendar specific functionality here}
public class SportsCalendar extends BaseCalendar{//do Sportscalendar specific functionality here}
create another class for Events and create list of events for each calendar(use aggrgation here, once your calendar is destroyed your events will also destroyed)

如果您的每个日历都有不同的行为,这将是我将采取的方法。

答案 3 :(得分:0)

如果我要满足你的要求,我会有以下排队:

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这是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;
    }
}