Apply an aspect to a component that has multiple methods.

← Design Patterns and Idioms · Ref: Q1118

An aspect proxies ALL component methods:

  service.start()
  service.status()
  service.stop()

Every method call goes through the aspect proxy. A silent aspect forwards all calls unchanged.

See Q1115 for basic aspect. See Q1119 for aspect ordering.

Example

defines module qa.designpatterns.aspectmultiplemethods

  defines component

    Service as abstract
      start()
        <- rtn as String: "abstract-start"

      stop()
        <- rtn as String: "abstract-stop"

      status()
        <- rtn as String: "abstract-status"

    ServiceImpl is Service
      override start()
        <- rtn as String: "started"

      override stop()
        <- rtn as String: "stopped"

      override status()
        <- rtn as String: "running"

  defines class

    SilentAspect extends Aspect
      default operator ?

  defines application

    MultiMethodApp
      register ServiceImpl() as Service with aspect of SilentAspect()

  defines program

    AspectMultipleMethodsDemo() with application of MultiMethodApp
      stdout <- Stdout()
      service as Service!

      //All three methods go through the aspect
      stdout.println(service.start())
      stdout.println(service.status())
      stdout.println(service.stop())
Other ways to ask this
  • I have a component with start, stop, and status methods — apply an aspect to all of them
  • In AspectJ a pointcut can match multiple methods. Show the EK9 multi-method aspect
  • Given a service with several methods, wrap all of them with a single aspect
  • Verify that an aspect proxies every method on a component, not just one

Coming from another language?

Spring AOP: pointcut expressions select methods. AspectJ: wildcards in pointcuts. EK9: aspects apply to ALL methods on the registered component automatically.

Keywords: proxy, multiple, methods, wrap, aspect, all