重构代码,以反映继承层次

问题描述 投票:0回答:1

我试图重构代码时遇到困难,知道从哪里开始,因为我很新的节目。

我试图把方法的评论,喜欢和不同类CommentedPost下,但不知道该怎么办了。

post.Java

public class Post {
private String username;  // username of the post's author
private long timestamp;
private int likes;
private ArrayList<String> comments;

/**
 * Constructor for objects of class Post.
 * 
 * @param author    The username of the author of this post.
 */
public Post(String author)
{
    username = author;
    timestamp = System.currentTimeMillis();
    likes = 0;
    comments = new ArrayList<String>();
}

/**
 * Record one more 'Like' indication from a user.
 */
public void like()
{
    likes++;
}

/**
 * Record that a user has withdrawn his/her 'Like' vote.
 */
public void unlike()
{
    if (likes > 0) {
        likes--;
    }
}

/**
 * Add a comment to this post.
 * 
 * @param text  The new comment to add.
 */
public void addComment(String text)
{
    comments.add(text);
}

/**
 * Return the time of creation of this post.
 * 
 * @return The post's creation time, as a system time value.
 */
public long getTimeStamp()
{
    return timestamp;
}

/**
 * Display the details of this post.
 * 
 * (Currently: Print to the text terminal. This is simulating display 
 * in a web browser for now.)
 */
public void display()
{
    System.out.println(username);
    System.out.print(timeString(timestamp));

    if(likes > 0) {
        System.out.println("  -  " + likes + " people like this.");
    }
    else {
        System.out.println();
    }

    if(comments.isEmpty()) {
        System.out.println("   No comments.");
    }
    else {
        System.out.println("   " + comments.size() + " comment(s). Click here to view.");
    }
}

/**
 * Create a string describing a time point in the past in terms 
 * relative to current time, such as "30 seconds ago" or "7 minutes ago".
 * Currently, only seconds and minutes are used for the string.
 * 
 * @param time  The time value to convert (in system milliseconds)
 * @return      A relative time string for the given time
 */

private String timeString(long time)
{
    long current = System.currentTimeMillis();
    long pastMillis = current - time;      // time passed in milliseconds
    long seconds = pastMillis/1000;
    long minutes = seconds/60;
    if(minutes > 0) {
        return minutes + " minutes ago";
    }
    else {
        return seconds + " seconds ago";
    }
}
}

commented post.Java

public class CommentedPost extends Post {

public CommentedPost(String author) {
    super(author);
    // TODO Auto-generated constructor stub
}
}

event post.Java

public class EventPost extends Post{

public EventPost(String author) {
    super(author);
    // TODO Auto-generated constructor stub
}

}

message post.Java

public class MessagePost extends Post{
private String message;  // an arbitrarily long, multi-line message

/**
 * Constructor for objects of class MessagePost.
 * 
 * @param author    The username of the author of this post.
 * @param text      The text of this post.
 */
public MessagePost(String author, String text)
{
    super(author);
    message = text;
}

/**
 * Return the text of this post.
 * 
 * @return The post's message text.
 */
public String getText()
{
    return message;
}

/**
 * Display the details of this post.
 * 
 * (Currently: Print to the text terminal. This is simulating display 
 * in a web browser for now.)
 */
public void display()
{
    super.display();
    System.out.println(message);
}
}

start network.Java

public class StartNetwork{

/**
 * @param args
 */
public static void main( String[] args )
{
    MessagePost message = new MessagePost("White Rabbit", "Oh dear, oh dear, I shall be late!");
    PhotoPost photo = new PhotoPost("Alice Wonderland", "RabbitHole.jpg" ,"Down the rabbit hole :)");

    message.addComment( "Your watch is exactly two days slow." );
    photo.like();

    NewsFeed news = new NewsFeed();

    news.addPost( message );
    news.addPost( photo );
    news.show();
}   
}

photo post.Java

public class PhotoPost extends Post{
private String filename;  // the name of the image file
private String caption;   // a one line image caption

/**
 * Constructor for objects of class PhotoPost.
 * 
 * @param author    The username of the author of this post.
 * @param filename  The filename of the image in this post.
 * @param caption   A caption for the image.
 */
public PhotoPost(String author, String filename, String caption)
{
    super(author);
    this.filename = filename;
    this.caption = caption;
}

/**
 * Return the file name of the image in this post.
 * 
 * @return The post's image file name.
 */
public String getImageFile()
{
    return filename;
}

/**
 * Return the caption of the image of this post.
 * 
 * @return The image's caption.
 */
public String getCaption()
{
    return caption;
}

/**
 * Display the details of this post.
 * 
 * (Currently: Print to the text terminal. This is simulating display 
 * in a web browser for now.)
 */
public void display()
{
    super.display();
    System.out.println("  [" + filename + "]");
    System.out.println("  " + caption);
}
}

news feed.Java

public class NewsFeed{
private ArrayList<Post> posts;

/**
 * Construct an empty news feed.
 */
public NewsFeed()
{
    posts = new ArrayList<Post>();
}

/**
 * Add a post to the news feed.
 * 
 * @param post  The post to be added.
 */
public void addPost(Post post)
{
    posts.add(post);
}

/**
 * Show the news feed. Currently: print the news feed details
 * to the terminal. (To do: replace this later with display
 * in web browser.)
 */
public void show()
{
    // display all posts
    for(Post post : posts) {
        post.display();
        System.out.println();   // empty line between posts
    }
}
}

重构后的代码应该具有与以下的类图:

邮政瓦特/域用户名和时间戳

CommentedPost(箭头指向后,即从它继承)W /场喜欢和评论

EventPost(箭头指向邮报)W /场EVENTTYPE

消息后(箭头指向CommentedPost)W /场消息

PhotoPost(箭头指向CommentedPost)W /字段的文件名和字幕

java inheritance class-hierarchy
1个回答
1
投票

帖子包含的喜好和意见。你应该将这些字段CommentedPost。

我不知道现场EVENTTYPE是什么,但EventPost没有这样的字段。您应该创建它。

根据你的描述,MessagePost应该扩展CommentedPost。它不是;相反,它扩展后。如果你想改变使得它延伸CommentedPost。

根据你的描述,PhotoPost应该扩展CommentedPost。它不是;相反,它扩展后。如果你想改变使得它延伸CommentedPost。

© www.soinside.com 2019 - 2024. All rights reserved.