Last active
May 17, 2020 09:13
-
-
Save SiAust/c2c02998dd8b7d4b498d0f2f3ef5c893 to your computer and use it in GitHub Desktop.
A example decorator design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
JavaTeamLead javaTeamLead = new JavaTeamLead | |
(new SeniorJavaDeveloper( | |
new JavaDeveloper())); | |
System.out.println(javaTeamLead.makeJob()); | |
/* Write Java code. Make code review. Send emails to clients. */ | |
} | |
} | |
/* Developer interface */ | |
interface Developer { | |
public String makeJob(); | |
} | |
/* Concrete Developer */ | |
class JavaDeveloper implements Developer { | |
@Override | |
public String makeJob() { | |
return "Write Java code."; | |
} | |
} | |
/* Developer Decorator to add functionality */ | |
class DeveloperDecorator implements Developer { | |
private Developer developer; | |
public DeveloperDecorator(Developer developer) { | |
this.developer = developer; | |
} | |
@Override | |
public String makeJob() { | |
return developer.makeJob(); | |
} | |
} | |
/* Concrete decorator, senior java dev */ | |
class SeniorJavaDeveloper extends DeveloperDecorator { | |
public SeniorJavaDeveloper(Developer developer) { | |
super(developer); | |
} | |
public String makeCodeReview() { | |
return "Make code review."; | |
} | |
public String makeJob() { | |
return super.makeJob() + " " + makeCodeReview(); | |
} | |
} | |
class JavaTeamLead extends DeveloperDecorator { | |
public JavaTeamLead(Developer developer) { | |
super(developer); | |
} | |
public String clientReview() { | |
return "Send emails to clients."; | |
} | |
public String makeJob() { | |
return super.makeJob() + " " + clientReview(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment