利用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);
}
}