简单工厂模式:
建立一个工厂类,对某一个接口的实现类,或这某一父类的子类创建实例。
基类:Animal.java
package entity;
public class Animal {
public void eat() {
System.out.println("吃");
}
}
子类: Cat.java
package entity;
public class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
}
子类:Dog.java
package entity;
public class Dog extends Animal{
public void eat() {
System.out.println("狗吃肉");
}
}
工厂类:Factory.java
package factory;
import entity.Animal;
import entity.Cat;
import entity.Dog;
public class Factory {
public static Animal createAnimal() {
return new Animal();
}
public static Cat createCat() {
return new Cat();
}
public static Dog createDog() {
return new Dog();
}
/**
* 0代表猫
* 1代表狗
* else 动物
* @param type
* @return
*/
public static Animal create(int type) {
if(type==0) {
return new Cat();
}else if(type==1) {
return new Dog();
}else {
return new Animal();
}
}
}
这里使用静态工厂方法,上面有两种写法,第一种写法是单独创建每个实例。第二种写法是通过一个方法,传入不同参数统一创建。
运行:
/**
* 简单工厂模式
*/
private static void demo1() {
Animal a=Factory.createAnimal();
a.eat();
Animal b=Factory.createCat();
b.eat();
Animal c=Factory.createDog();
c.eat();
System.out.println("-----------------------------------\n按照type进行创建:");
Animal d=Factory.create(0);
d.eat();
}
工厂方法模式:
对每个类分别创建工厂
工厂类接口:
package factory;
import entity.Animal;
/**
* 工厂方法模式
* @author jack
*
*/
public interface IFactory {
public Animal getAnimal();
}
子类:AnimalFactory.java
package factory;
import entity.Animal;
public class AnimalFactory implements IFactory{
@Override
public Animal getAnimal() {
return new Animal();
}
}
子类:CatFactory.java
package factory;
import entity.Animal;
import entity.Cat;
public class CatFactory implements IFactory {
@Override
public Animal getAnimal() {
return new Cat();
}
}
子类:DogFactory.java
package factory;
import entity.Animal;
import entity.Dog;
public class DogFactory implements IFactory{
@Override
public Animal getAnimal() {
return new Dog();
}
}
运行:
/**
* 工厂方法模式
*/
private static void demo2() {
IFactory factory=new AnimalFactory();
factory.getAnimal().eat();
IFactory factory2=new CatFactory();
factory2.getAnimal().eat();
IFactory factory3=new DogFactory();
factory3.getAnimal().eat();
}
抽象工厂模式
与工厂方法模式不同,一个工厂也可以创建多个产品,这里我们新增‘主人类’
IOwner.java
package entity;
public interface IOwner {
public void feed();
}
CatOwner.java
package entity;
public class CatOwner implements IOwner {
@Override
public void feed() {
System.out.println("喂猫");
}
}
DogOwner.java
package entity;
public class DogOwner implements IOwner {
@Override
public void feed() {
System.out.println("喂狗");
}
}
工厂接口:
package factory;
import entity.Animal;
import entity.IOwner;
public interface IAbstractFactory {
public Animal getAnimal();
public IOwner getOwner();
}
实现类:
package factory;
import entity.Animal;
import entity.Cat;
import entity.CatOwner;
import entity.IOwner;
public class AbstractCatFactory implements IAbstractFactory {
@Override
public Animal getAnimal() {
return new Cat();
}
@Override
public IOwner getOwner() {
return new CatOwner();
}
}
package factory;
import entity.Animal;
import entity.Dog;
import entity.DogOwner;
import entity.IOwner;
public class AbstractDogFactory implements IAbstractFactory {
@Override
public Animal getAnimal() {
return new Dog();
}
@Override
public IOwner getOwner() {
return new DogOwner();
}
}
运行:
/**
* 抽象工厂模式 此模式可以实现多个接口,一个工厂可以有多个产品
*/
private static void demo3() {
IAbstractFactory factory=new AbstractCatFactory();
factory.getAnimal().eat();
factory.getOwner().feed();
System.out.println("----------------------");
IAbstractFactory factory2=new AbstractDogFactory();
factory2.getAnimal().eat();
factory2.getOwner().feed();
System.out.println("----------------------");
}