设计模式抽象工厂模式实现手枪步枪工厂

利用java语言简单实现抽象工厂模式的书上的例子——手枪和步枪工厂。代码如下,代码中有一些与枪有关的性质的名称数据是我简单想到和加上的,没有任何参考价值,如果有需要的话可以试着更改一下,再完善一下。我是一个纯新的作者,还是挺菜的,这个代码写出来其实我自己也做不到能完全理解这个抽象工厂模式,还在慢慢的学习过程中,目前就只能先把这个代码按照书上的代码照猫画虎的写出来。

1.Gun.java

public abstract class Gun {
    public abstract int getSize();
    public abstract int getWeight();
    public abstract String getName();
}

2.Bullet.java

public abstract class Bullet {
    public abstract int getRange();
    public abstract int getWeight();
    public abstract String getName();
}

3.Arsenal.java

public abstract class Arsenal {
    public abstract Gun createGun(int size,int weight);
    public abstract Bullet createBullet(int range, int weight);
}

4.Pistol.java

public class Pistol extends Gun{
    private int size;
    private int weight;
    private String name;
    Pistol(String name, int size, int weight){
        this.name=name;
        this.size=size;
        this.weight=weight;
    }
    public int getSize(){
        return size;
    }
    public int getWeight(){
        return weight;
    }
    public String getName(){
        return name;
    }
}

 5.PistolBullet.java

public class PistolBullet extends Bullet {
    private int range;
    private int weight;
    private String name;
    PistolBullet(String name, int range, int weight){
        this.name=name;
        this.range=range;
        this.weight=weight;
    }
    public int getRange(){
        return range;
    }
    public int getWeight(){
        return weight;
    }
    public String getName(){
        return name;
    }
}

6.RifleBullet.java

public class RifleBullet extends Bullet {
    private int range;
    private int weight;
    private String name;
    RifleBullet(String name,int range,int weight){
        this.name=name;
        this.range=range;
        this.weight=weight;
    }
    public int getRange(){
        return range;
    }
    public int getWeight(){return weight;}
    public String getName(){return name;}
}

7.PistolFactory.java

public class PistolFactory extends Arsenal{
    public Gun createGun(int size,int weight){
        return new Pistol("A牌手枪",size,weight);
    }
    public Bullet createBullet(int range, int weight){
        return new PistolBullet("A牌手枪子弹",range,weight);
    }
}

8.RifleFactory.java

public class RifleFactory extends Arsenal{
    public Gun createGun(int size,int weight){
        return new Pistol("B牌步枪",size,weight);
    }
    public Bullet createBullet(int range, int weight){
        return new PistolBullet("B牌步枪子弹",range,weight);
    }
}

9.Rifle.java

public class Rifle extends Gun{
    private int size;
    private int weight;
    private String name;
    Rifle(String name, int size, int weight){
        this.name=name;
        this.size=size;
        this.weight=weight;
    }
    public int getSize(){
        return size;
    }
    public int getWeight(){
        return weight;
    }
    public String getName(){
        return name;
    }
}

10.Shop.java

public class Shop {
    Gun gun;
    Bullet bullet;
    public void giveSuit(Arsenal factory,int size,int range,int weight){
        gun=factory.createGun(size,weight);
        bullet=factory.createBullet(range,weight);
        showMess();
    }
    private void showMess() {
        System.out.println("<枪支信息>");
        System.out.println(gun.getName() + ":");
        System.out.print("尺寸:" + gun.getSize());
        System.out.println("重量:" + gun.getWeight());
        System.out.println(bullet.getName() + ":");
        System.out.print("射程:" + bullet.getRange());
        System.out.println("重量:" + bullet.getWeight());
    }
    }

11.Application.java

public class Application {
    public static void main(String args[]) {
        Shop shop = new Shop();
        Arsenal factory = new PistolFactory();
        shop.giveSuit(factory, 150, 50, 1);
        factory = new RifleFactory();
        shop.giveSuit(factory, 500, 800, 4);
    }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值