You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pr109042.aj 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. class PlayList {
  4. private static PlayList instance;
  5. private List<Song> list;
  6. private PlayList() {
  7. list = new ArrayList<Song>();
  8. }
  9. public static PlayList instance() {
  10. if(instance==null ) {
  11. instance = new PlayList();
  12. }
  13. return instance;
  14. }
  15. public void enqueue(Song song) {
  16. list.add(song);
  17. if(Player.instance().isIdle()) {
  18. new Thread() {
  19. public void run() {
  20. System.out.println("Playing playlist...");
  21. for (Song s : list) {
  22. Player.instance().play(s.getName());
  23. }
  24. }
  25. }.start();
  26. }
  27. }
  28. }
  29. class Song {
  30. private String name;
  31. public Song(String name) {
  32. this.name = name;
  33. }
  34. public String getName() {
  35. return name;
  36. }
  37. }
  38. class Player {
  39. private static Player instance;
  40. private Player() {}
  41. public static Player instance() {
  42. if(instance==null ) {
  43. instance = new Player();
  44. }
  45. return instance;
  46. }
  47. public void play(String name) {
  48. System.out.println("Playing Song "+name+"...");
  49. }
  50. public boolean isIdle() {
  51. return true;
  52. }
  53. }
  54. class Jukebox {
  55. public void play(Song song) {
  56. Player.instance().play(song.getName());
  57. }
  58. }
  59. class Main {
  60. public static void main(String[] args) {
  61. Song song = new Song("Merry XMas");
  62. Jukebox jukebox = new Jukebox();
  63. jukebox.play(song);
  64. }
  65. }
  66. aspect PlaylistAspect {
  67. void around(Song song) :
  68. call(public void Jukebox.play(Song))
  69. && args(song) {
  70. PlayList.instance().enqueue(song);
  71. }
  72. }
  73. aspect CreditsAspect {
  74. void around() : call(public void Jukebox.play(Song)) {
  75. if(Credits.instance().enoughCredits()) {
  76. System.out.println("Withdrawing credit.");
  77. Credits.instance().withDraw();
  78. proceed();
  79. } else {
  80. throw new InsufficientCreditsException();
  81. }
  82. }
  83. }
  84. class Credits {
  85. private static final int INITIAL_CREDITS = 10;
  86. private static Credits instance;
  87. private int credits;
  88. private Credits() {
  89. credits = INITIAL_CREDITS;
  90. }
  91. public static Credits instance() {
  92. if(instance==null ) {
  93. instance = new Credits();
  94. }
  95. return instance;
  96. }
  97. public boolean enoughCredits() {
  98. return credits > 0;
  99. }
  100. public void withDraw() {
  101. credits--;
  102. }
  103. }
  104. @SuppressWarnings("serial")
  105. class InsufficientCreditsException extends RuntimeException {
  106. public InsufficientCreditsException() {
  107. super();
  108. }
  109. public InsufficientCreditsException(String message, Throwable cause) {
  110. super(message, cause);
  111. }
  112. public InsufficientCreditsException(String message) {
  113. super(message);
  114. }
  115. /**
  116. * @param cause
  117. */
  118. public InsufficientCreditsException(Throwable cause) {
  119. super(cause);
  120. }
  121. }