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

前端之家收集整理的这篇文章主要介绍了一个依赖注入(DI)的小例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@原型是head first中的Duck例子,我根据我的理解又写了一遍

@H_502_1@这是包的结构:

@H_502_1@
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();
    }

}
@H_502_1@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");
    }

}
@H_502_1@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();
    }


}
@H_502_1@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();
    }
}
@H_502_1@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();
    }

}
@H_502_1@FlyBehavior.java

package cn.cdut.interfaces;

public interface FlyBehavior {

        public void fly();
}
@H_502_1@QuackBehavior.java

package cn.cdut.interfaces;

public interface QuackBehavior {
    public void quack();
}
@H_502_1@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");

    }

}
@H_502_1@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");
    }

}
@H_502_1@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");
    }

}
@H_502_1@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");

    }

}
@H_502_1@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");

    }

}
原文链接:https://www.f2er.com/javaschema/284421.html

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