vendredi 12 mai 2017

Could you help provide a solution to applying the decorator design pattern to this code?

I have an exam coming up and in the one of the past papers this question, comes up i'm hoping someone can help provide a solution.

Thanks.

Question: Describe the decorator design pattern in about three sentences.Then apply the decorator design pattern to the classes below. The aim is to make the class Student a decorator of the class Person. Do not copy the code but describe what changes are needed to use the decorator design pattern.

public class Person
{
    private String givenName;
    private String familyName;
    public Person(String given, String family)
    {
        givenName = given;
        familyName = family;
    }
    public String getGivenName()
    {
        return givenName;
    }
    public String getFamilyName()
    {
        return familyName;
    }
}
public class Student extends Person
{
    private int id;
    public Student(String given, String family, int id)
    {
        super(given,family);
        this.id = id;
    }
    public Person getPerson()
    {
        return new Person(getGivenName(),getFamilyName());
    }
}

Aucun commentaire:

Enregistrer un commentaire