Sunday, August 18, 2013

PerfSpy 3 -- PerfSpyDemo

I like dolphin! It is such an intelligent, playful and social animal. It can learn so many tricks, tail-walking, dancing, jumping, you name it. It can even innovate – creating new tricks on their own! Of course, if you have the world’s cutest bottom as a panda does, you do not have to learn tricks to please the crowd – hey, life is not fair and beauty is not just skip-deep.

I am going to use dolphin and panda in my demo, if you think it is silly, bear with me.

In this demo:

Animal is an abstract class with an abstract method playTricks(),Panda and Dolphin extends from it. Show invokes Animal.playTricks() to startAShow(). Animals have a Trainer. Notice how they are inside different packages, it is going to play a role in your Aspect.

public abstract class Animal {
        private String name;
        private Trainer trainer;

        public Animal(String name, Trainer trainer) {
                super();
                this.name = name;
                this.trainer = trainer;
        }

        public String getName() {
                return name;
        }

        public Trainer getTrainer() {
                return trainer;
        }

        public abstract void playTricks() throws Exception;
}
Dolphin does a lot of things in a show:
public class Dolphin extends Animal {
        private List friends = new ArrayList();

        public Dolphin(String name, Trainer trainer) {
                super(name, trainer);
                friends.add(trainer);
                //a bit narcissistic, friends with himself 100 times
                for (int i = 0; i < 100; i++) {
                        friends.add(this);
                }
        }

        @Override
        public void playTricks() throws Exception {
                shakeHands(this.getTrainer());

                tailWalk(10);

                for (int i = 0; i < 5; i++) {
                        backflip(i);
                }
        }

        private void addFriend(Object friend) {
                this.friends.add(friend);
        }

        private void tailWalk(int length) {
                System.out.println(this.getName() + " is tail walking " + length + " meters ...");
        }

        private void backflip(int index) throws Exception {
                System.out.println(this.getName() + " is backflipping " + index + " time ...");
//mimic slow methods
                Thread.sleep(1 * 1000);
        }

        private void shakeHands(Trainer trainer) {
                System.out.println(this.getName() + " is shaking hands with " + trainer.getName() + " ...");
                trainer.award(this);
        }
}
On the other hand, Panda simply needs to be a Panda:
public class Panda extends Animal{

        public Panda(String name, Trainer trainer) {
                super(name, trainer);          
        }

        @Override
        public void playTricks() {     
                beingCute();
        }
       
        private String beingCute(){
                return "being cute...";
        }
       
}
Here is a Show:
public class Show {
        public void startAShow(String type) throws Exception {
                Trainer trainer;
                Animal animal;

                if (type.equals("dolphin")) {
                        trainer = new Trainer("Lucy");
                        animal = new Dolphin("Noodle", trainer);
                } else {
                        trainer = new Trainer("xiaowang");
                        animal = new Panda("lingling", trainer);
                }

                animal.playTricks();
        }

        public static void main(String[] args) throws Exception {
                new Show().startAShow(args[0]);
        }
} 

1 comment:

  1. Hello penguin cute,
    I am facing issues while running the PerfSpyAdmin. The UI layout is displaying properly but with no data. I can share you the perfspy-ui-config.xml through email. Please help me to solve my problem. I spent lot of hours/days to figure out the cause but unable to figure it out.

    ReplyDelete