一个依赖注入(DI)的小例子

前端之家收集整理的这篇文章主要介绍了一个依赖注入(DI)的小例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原型是head first中的Duck例子,我根据我的理解又写了一遍

这是包的结构:


Demo.java

package cn.cdut.demo;

import cn.cdut.entity.LittleDuck;
import cn.cdut.entity.MechanicalDuck;
import cn.cdut.entity.RedDuck;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        RedDuck rd=new RedDuck();
        LittleDuck ld=new LittleDuck();
        MechanicalDuck md=new MechanicalDuck();
        System.out.println("a redduck says:");
        rd.swim();
        rd.fly();
        rd.quack();
        System.out.println();
        System.out.println("a litteduck says:");
        ld.swim();
        ld.fly();
        ld.quack();
        System.out.println();
        System.out.println("a mechanicalduck says:");
        md.swim();
        md.fly();
        md.quack();
    }

}

Duck.java

package cn.cdut.entity;

import cn.cdut.interfaces.FlyBehavior;
import cn.cdut.interfaces.QuackBehavior;

public abstract class Duck {
    public FlyBehavior flyBehavior;
    public QuackBehavior quackBehavior;

    public void swim(){
        System.out.println("Duck can swim");
    }

}

RedDuck.java

package cn.cdut.entity;

import cn.cdut.utils.FlyNormally;
import cn.cdut.utils.NormalQuack;

public class RedDuck extends Duck {

    public RedDuck(){
        quackBehavior=new NormalQuack();
        flyBehavior=new FlyNormally();
    }
    public void fly(){
        flyBehavior.fly();
    }
    public void quack(){
        quackBehavior.quack();
    }


}

LittleDuck.java

package cn.cdut.entity;

import cn.cdut.utils.NoWayFly;
import cn.cdut.utils.NormalQuack;

public class LittleDuck extends Duck {
    public LittleDuck(){
        flyBehavior=new NoWayFly();
        quackBehavior=new NormalQuack();
    }
    public void fly(){
        flyBehavior.fly();
    }
    public void quack(){
        quackBehavior.quack();
    }
}

MechanicalDuck.java

package cn.cdut.entity;

import cn.cdut.utils.FlyWithRocket;
import cn.cdut.utils.MechanicalQuack;

public class MechanicalDuck extends Duck {
    public MechanicalDuck(){
        flyBehavior=new FlyWithRocket();
        quackBehavior=new MechanicalQuack();
    }
    public void fly(){
        flyBehavior.fly();
    }
    public void quack(){
        quackBehavior.quack();
    }

}

FlyBehavior.java

package cn.cdut.interfaces;

public interface FlyBehavior {

        public void fly();
}

QuackBehavior.java

package cn.cdut.interfaces;

public interface QuackBehavior {
    public void quack();
}

FlyNormally.java

package cn.cdut.utils;

import cn.cdut.interfaces.FlyBehavior;

public class FlyNormally implements FlyBehavior {

    @Override
    public void fly() {
        // TODO Auto-generated method stub
        System.out.println("I can fly just like a normal duck");

    }

}

FlyWithRocket.java

package cn.cdut.utils;

import cn.cdut.interfaces.FlyBehavior;

public class FlyWithRocket implements FlyBehavior {

    @Override
    public void fly() {
        // TODO Auto-generated method stub
        System.out.println("I can fly fast,cuz i got a rocket");
    }

}

MechanicalQuack.java

package cn.cdut.utils;

import cn.cdut.interfaces.QuackBehavior;

public class MechanicalQuack implements QuackBehavior {

    @Override
    public void quack() {
        // TODO Auto-generated method stub
        System.out.println("quackeee quackee quackee");
    }

}

NormalQuack .java

package cn.cdut.utils;

import cn.cdut.interfaces.QuackBehavior;

public class NormalQuack implements QuackBehavior {

    @Override
    public void quack() {
        // TODO Auto-generated method stub
        System.out.println("quack quack quack");

    }

}

NoWayFly.java

package cn.cdut.utils;

import cn.cdut.interfaces.FlyBehavior;

public class NoWayFly implements FlyBehavior {

    @Override
    public void fly() {
        System.out.println("I can't fly now");

    }

}

猜你在找的设计模式相关文章