一:简单工厂
package com.design.pattern.factory.simplefactory;
/**
* 汽车接口
*/
public interface ICar {
void travel();
}
/** 普通商务车*/
class CommentCar implements ICar {
@Override
public void travel() {
System.out.println("普通汽车在公路上跑!");
}
}
/** 跑车*/
class SportsCar implements ICar {
@Override
public void travel() {
System.out.println("跑车在高速飙车!演绎速度激情-----");
}
}
简单工厂类 :
/**汽车制造工厂*/
public class CarFactory {
//空构造器
public CarFactory() {
}
//方法初始化
public ICar getCar(String type){
if(type.equals("跑车")){
return new SportsCar();
}else if (type.equals("商务车")){
return new CommentCar();
}
return new CommentCar();
}
}
客户端测试:
/**简单工厂测试*/
public class Client {
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
ICar car= carFactory.getCar("跑车");
car.travel();
}
}
结果:
跑车在高速飙车!演绎速度激情-----
二:工厂方法模式
package com.design.pattern.factory.factorymethod;
/**
* 汽车接口
*/
public interface ICar {
void travel();
}
/** 普通商务车*/
class CommentCar implements ICar {
@Override
public void travel() {
System.out.println("普通汽车在公路上跑!");
}
}
/** 跑车*/
class SportsCar implements ICar {
@Override
public void travel() {
System.out.println("跑车在高速飙车!演绎速度激情-----");
}
}
package com.design.pattern.factory.factorymethod;
/**抽象工厂*/
public interface ICarFactory {
public ICar getCar();
}
package com.design.pattern.factory.factorymethod;
/**普通商务车制造工厂*/
public class CommentCarFactory implements ICarFactory {
@Override
public ICar getCar() {
return new CommentCar();
}
}
package com.design.pattern.factory.factorymethod;
/**超跑汽车制造工厂 */
public class SportsCarFactory implements ICarFactory {
@Override
public ICar getCar() {
return new SportsCar();
}
}
测试:
package com.design.pattern.factory.factorymethod;
/**简单工厂测试*/
public class Client {
public static void main(String[] args) {
ICarFactory commentCarFactory = new CommentCarFactory();
ICar commentCar= commentCarFactory.getCar();
commentCar.travel();
System.out.println("\n");
ICarFactory sportsCarFactory = new SportsCarFactory();
ICar sportsCar= sportsCarFactory.getCar();
sportsCar.travel();
}
}
普通汽车在公路上跑!
跑车在高速飙车!演绎速度激情-----
简单工厂与工厂方法,工厂方法对于工厂解耦,开闭原则,把一个工厂类扩展多个工厂类,不必传参确定创建那个汽车,不同工长制造不同汽车。
三:抽象工厂
抽象工厂可以增加产品族,工厂方法可以增加产品
package com.design.pattern.factory.abstractFactory;
/**汽车引擎*/
public interface Engine {
void run();
void start();
}
class LuxuryEngine implements Engine{
@Override
public void run() {
System.out.println("跑的速度快!");
}
@Override
public void start() {
System.out.println("制动快!可以自动启停!");
}
}
class LowEngine implements Engine{
@Override
public void run() {
System.out.println("跑的速度慢!");
}
@Override
public void start() {
System.out.println("制动慢!不能自动启停!");
}
}
package com.design.pattern.factory.abstractFactory;
/**车座*/
public interface Seat {
void massage();
}
class LuxurySeat implements Seat {
@Override
public void massage() {
System.out.println("车座可以自动按摩!");;
}
}
class LowSeat implements Seat {
@Override
public void massage() {
System.out.println("车座硬,不可以按摩!");
}
}
package com.design.pattern.factory.abstractFactory;
/**轮胎*/
public interface Tyre {
void revolve();
}
class LuxuryTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转不磨损!");
}
}
class LowTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转磨损快!");
}
}
package com.design.pattern.factory.abstractFactory;
/**汽车产品组抽象工厂*/
public interface ICarFactory {
Engine creatEngine(); //制造发动机
Seat creatSeat(); //制造座椅
Tyre creatTyre(); //制造轮胎
}
package com.design.pattern.factory.abstractFactory;
/**低端车工厂--配套设施*/
public class LowCarFactory implements ICarFactory {
@Override
public Engine creatEngine() {
return new LowEngine();
}
@Override
public Seat creatSeat() {
return new LowSeat();
}
@Override
public Tyre creatTyre() {
return new LowTyre();
}
}
package com.design.pattern.factory.abstractFactory;
/**高端车-制造工厂--配套设施*/
public class LuxuryCarFactory implements ICarFactory {
@Override
public Engine creatEngine() {
return new LuxuryEngine();
}
@Override
public Seat creatSeat() {
return new LuxurySeat();
}
@Override
public Tyre creatTyre() {
return new LuxuryTyre();
}
}
package com.design.pattern.factory.abstractFactory;
public class Client {
public static void main(String[] args) {
ICarFactory factory = new LuxuryCarFactory();
Engine e = factory.creatEngine();
e.run();
e.start();
}
}