Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AOP with YouAreDaBomb

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for Jan Filipowski Jan Filipowski
June 11, 2012
70

AOP with YouAreDaBomb

Avatar for Jan Filipowski

Jan Filipowski

June 11, 2012

Transcript

  1. AOP for dummies • separation of cross-cutting concerns • concerns:

    logging, authorization, security, storage etc. • aspect • advice • join point
  2. Example - logging author = // (...) writePost: (title, content)

    -> @posts.push(new Post(title, content)) Before(author, 'writePost', (title, content) -> console.log("Author#writePost(#{title}, #{content})") ) author.writePost("title", "long content") # console: Author#writePost(title,content) author.posts.length # 1
  3. Example - storage After(author, 'writePost', (title, content) -> serverSide.storePost(title, content)

    ) author.getAllPosts = (callback) -> callback(@posts) Around(author, 'getAllPosts', (proceed, args...) -> serverSide.getAllPosts((response) -> author.posts = buildPosts(response) proceed(args...) ) )
  4. AOP in JavaScript / CoffeeScript • YouAreDaChef (http://github.com/raganwald/YouAreDaChef) ◦ based

    on prototypes ◦ supports npm • YouAreDaBomb (http://github.com/gameboxed/YouAreDaBomb) ◦ inspired by YouAreDaChef ◦ based on objects ◦ supports callback advices (beforeAnyCallback, afterAnyCallback) ◦ doesn't support npm yet
  5. YouAreDaBomb API YouAreDaBomb(object, methodName) .before((args...) -> (...)) .after((args...) -> (...))

    .around((proceed, args...) -> (...)) .beforeAnyCallback((callbackArgs...) -> (...)) .afterAnyCallback((callbackArgs...) -> (...)) # chainable
  6. Our approach • use cases as flow • domain models

    • GUI components • server-side and Facebook adapters • aspects as glue
  7. Real world simple example • simple social app • write

    something - it will be posted as a picture on facebook • you can write it only once
  8. Real world simple example use case class MainUseCase constructor: (@game)

    -> enterGame: => if [email protected]() @useEditor() useEditor: => writeStatement: (statement) => @game.playerWriteStatement(statement)
  9. Real world simple example GUI aspect class MainGuiConfiguration constructor: (@useCase,

    @gui) -> execute: => After @useCase, 'useEditor', => @gui.showEditor() @gui.hideSubmittedStatement() Around(@useCase, 'writeStatement', @onWriteStatement) After(@gui.editor, 'submitStatement', (statement) => @useCase.writeStatement(statement)) onWriteStatement: (proceed, args...) => statement = proceed(args...) @gui.addStatementToList(statement) @gui.hideEditor() @gui.showSubmittedStatement()
  10. Real world simple example Game storage aspect class GameAspect constructor:

    (@game, @services) -> execute: => After(@game, 'playerWriteStatement', (statement) => @services.serverSide.playerWriteStatement(statement))
  11. Real world simple example Viral aspect class RequireLikeFanpageToEnterAspect constructor: (@usecase,

    @game, @services) -> execute: () => Around @usecase, 'enterGame', (proceed) => @services.facebookAdapter.isFan @game.player.facebookUid, @game.fanpageId, (likes) => if likes proceed() else component = new LikeFanpageToProceedPopupComponent(@services, @game) component.showPopup(proceed)
  12. Real world example - quiz game Usecase (object): 1. enter

    quiz 2. get next question 3. answer question 4. correct answer made 5. wrong answer made 6. end quiz
  13. Example: Enter quiz GUI aspect: • Display popup and wait

    for "OK" click • Render question and answers board Sound aspect: • Play intro sound
  14. Example: Get next question GUI aspect: • show question •

    show answers Storage: • get question (advice on Quiz#getNextQuestion) Sound: • play 'new question' sound
  15. Example: Correct answer made GUI: • show "yay!" Storage: •

    save players points Sound: • play "yay!"
  16. Example: Wrong answer made GUI: • show "buuu" Storage: •

    store info about wrong answer Sound: • play 'buuu'
  17. Example: End quiz GUI: • hide question and answers board

    • show popup with results Storage: • store game session info Social feature: • post to wall about results