介绍:
工厂模式是Java中最常用的设计模式,比如Spring中IOC容器就是通过工厂模式管理创建对象。这种类型的设计模式属于创建型模式,提供了一种创建对象的最佳方式。本文将介绍简单工厂模式与抽象工厂模式。
简单工厂模式: 由一个工厂类来管理对象的创建
1.新建接口类:Bird
interface Bird {
void getKind();
}
2.新建实现类:Woodpecker、Swallow、Pigeon
class Woodpecker implements Bird{
@Override
public void getKind() {
System.out.println("啄木鸟");
}
}
class Swallow implements Bird{
@Override
public void getKind() {
System.out.println("燕子");
}
}
class Pigeon implements Bird{
@Override
public void getKind() {
System.out.println("鸽子");
}
}
3.创建工厂类:FactoryBird
package com.wyj.interview.designPattern.simplefactorypattern;
/**
* @description: 工厂类,用于创建对象
* @author wangyijun01_sx@qiyi.com
* @date 2018/11/29 14:25
*/
public class FactoryBird {
public Woodpecker getWoodpecker() {
return new Woodpecker();
}
public Swallow getSwallow() {
return new Swallow();
}
public Pigeon getPigeon() {
return new Pigeon();
}
}
4.测试简单工厂模式
public static void main(String[] args) {
FactoryBird factoryBird = new FactoryBird();
factoryBird.getWoodpecker().getKind();
factoryBird.getSwallow().getKind();
factoryBird.getPigeon().getKind();
}
结果:
啄木鸟
燕子
鸽子
抽象工厂模式:
1.创建接口:Shape、Color
interface Shape{
void draw();
}
interface Color {
void fill();
}
2.创建接口实现类:Circle、Square、Rectangle、Red、Blue、White
class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("长方形");
}
}
class Square implements Shape{
@Override
public void draw() {
System.out.println("正方形");
}
}
class Circle implements Shape{
@Override
public void draw() {
System.out.println("圆形");
}
}
class Red implements Color{
@Override
public void fill() {
System.out.println("红色");
}
}
class Blue implements Color{
@Override
public void fill() {
System.out.println("蓝色");
}
}
class White implements Color{
@Override
public void fill() {
System.out.println("白色");
}
}
3.创建工厂类:ShapeFactory、ColorFactory
package com.wyj.interview.designPattern.abstractfactorypttern;
/**
* @description: 形状工厂类
* @author wangyijun01_sx@qiyi.com
* @date 2018/11/29 14:51
*/
public class ShapeFactory extends AbstractFactory{
@Override
public Shape getShape(String shapeType) {
if ("Rectangle".equals(shapeType)) {
return new Rectangle();
}
if ("Square".equals(shapeType)) {
return new Square();
}
if ("Circle".equals(shapeType)) {
return new Circle();
}
return null;
}
@Override
public Color getColor(String colorType) {
return null;
}
}
package com.wyj.interview.designPattern.abstractfactorypttern;
/**
* @description: 颜色工厂类
* @author wangyijun01_sx@qiyi.com
* @date 2018/11/29 14:53
*/
public class ColorFactory extends AbstractFactory{
@Override
public Color getColor(String colorType) {
if ("Red".equals(colorType)) {
return new Red();
}
if ("Blue".equals(colorType)) {
return new Blue();
}
if ("White".equals(colorType)) {
return new White();
}
return null;
}
@Override
public Shape getShape(String shapeType) {
return null;
}
}
4.创建抽象工厂类:AbstractFactory
package com.wyj.interview.designPattern.abstractfactorypttern;
/**
* @description: 用于创建工厂对象
* @author wangyijun01_sx@qiyi.com
* @date 2018/11/29 14:54
*/
public abstract class AbstractFactory {
public abstract Shape getShape(String shapeType);
public abstract Color getColor(String colorType);
}
5.创建类:FactoryProducer
package com.wyj.interview.designPattern.abstractfactorypttern;
/**
* @description: 创建工厂对象的工厂类
* @author wangyijun01_sx@qiyi.com
* @date 2018/11/29 15:10
*/
public class FactoryProducer {
public static AbstractFactory getFactory(String factoryType) {
if ("ShapeFactory".equals(factoryType)) {
return new ShapeFactory();
}
if ("ColorFactory".equals(factoryType)) {
return new ColorFactory();
}
return null;
}
}
6.测试:
public static void main(String[] args) {
FactoryProducer.getFactory("ShapeFactory").getShape("Rectangle").draw();
FactoryProducer.getFactory("ColorFactory").getColor("Red").fill();
}
结果:
长方形
红色