工厂模式有三种:简单工厂,工厂方法和抽象工厂。
简单工厂:为有某种共同特性的对象创建工厂,把这种特性声明为接口中的方法,再创建一个工厂类实现这个接口。
比如:
public interface Moveable{
void run();
}
public class Car implements Moveable{
public void run(){
system.out.println("汽车跑的飞快");
}
}
public class Plane implements Moveable{
public void run(){
System.out.println("飞机在天上飞");
}
}
public class MoveFactory{
public static Moveable getInstance(int type){
if(type == 1){
return new Car();
}else if(type == 2){
return new Plane();
}
}
}
public class test{
public static void main(String []args){
Moveable m = MoveFactory.getInstance(1);
m.run();
Moveable m = MoveFactory.getInstance(2);
m.run();
}
}
根据工厂的定义,单例模式也是工厂模式的一种表现形式。
工厂方法:在简单工厂的基础上,为每一种对象都创建一个工厂,再创建一个接口或者抽象类,该抽象类中有一个创建对象的方法,所有的工厂类都继承这个类,并且通过这个方法创建对象。
public interface Moveable {
void run();
}
public class Car implements Moveable {
@Override
public void run() {
System.out.println("百米加速5S");
}
}
public class Plane implements Moveable {
@Override
public void run() {
System.out.println("G3战斗机...............");
}
}
public abstract class BeanFactory {
abstract Moveable create();
}
public class CarFactory extends BeanFactory {
@Override
public Moveable create() {
return new Car();
}
}
public class PlaneFactory extends BeanFactory {
@Override
Moveable create() {
return new Plane();
}
}
public class Test {
public static void main(String[] args) {
BeanFactory factory = new PlaneFactory();
Moveable m = factory.create();
m.run();
}
}
抽象工厂:是之当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客服端提供一个接口,是客服端在不必指定产品的具体情况下,创建多个产品族中的产品对象。
public abstract class BeanFactory {
public abstract Food createFood();
public abstract Weapon createWeapon();
public abstract Vehicle createVehicle();
}
public abstract class Food {
public abstract void eat();
}
public abstract class Weapon {
public abstract void shoot();
}
public abstract class Vehicle {
public abstract void printName();
}
public class Cheng extends BeanFactory {
@Override
public Food createFood() {
return new Supian();
}
@Override
public Weapon createWeapon() {
return new AK47();
}
@Override
public Vehicle createVehicle() {
return new Car();
}
}
public class Car extends Vehicle {
@Override
public void printName() {
System.out.println("fun fast.********........");
}
}
public class AK47 extends Weapon {
@Override
public void shoot() {
// TODO Auto-generated method stub
System.out.println("dadada........");
}
}
public class Supian extends Food {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("gajigajigaji...........");
}
}
public class MagicFactory extends BeanFactory {
@Override
public Food createFood() {
// TODO Auto-generated method stub
return new MushRoom();
}
@Override
public Weapon createWeapon() {
// TODO Auto-generated method stub
return new IceTool();
}
@Override
public Vehicle createVehicle() {
// TODO Auto-generated method stub
return new YuSu();
}
}
public class MushRoom extends Food {
@Override
public void eat() {
System.out.println("mushroot,....");
}
}
public class IceTool extends Weapon {
@Override
public void shoot() {
System.out.println("huhuhu....");
}
}
public class YuSu extends Vehicle {
@Override
public void printName() {
System.out.println("魔法师");
}
}