工厂模式
面向对象设计的基本原则
定义一个接口Car
public interface Car {
void run();
}
实现类Audi
public class Audi implements Car {
@Override
public void run() {
System.out.println("奥迪再跑!");
}
}
实现类Byd
public class Byd implements Car {
@Override
public void run() {
System.out.println("比亚迪再跑!");
}
}
测试类Client01,没有工厂模式的时候:
public class Client01 { //调用者
public static void main(String[] args) {
Car c1 = new Audi();
Car c2 = new Byd();
c1.run();
c2.run();
}
}
添加一个工厂类CarFactory
/**
* 根据要的类型进行创建
*/
public static Car createCar(String type) {
if (type.equals("奥迪")) {
return new Audi();
} else if (type.equals("比亚迪")) {
return new Byd();
}
return null;
}
工厂类也可以这样
public class CarFactory2 {
public static Audi getAudi() {
return new Audi();
}
public static Byd getByd() {
return new Byd();
}
}
测试类
public class testCa {
@Test
public void testCar() {
Car car = new Audi();
Car car1 = new Byd();
car.run();
car1.run();
}
@Test
public void testCar1() {
Car car = CarFactory.createCar("奥迪");
Car car1 = CarFactory.createCar("比亚迪");
car.run();
car1.run();
}
@Test
public void testCar2() {
Car car = CarFactory2.getAudi();
Car car1 = CarFactory2.getByd();
car.run();
car1.run();
}
}
工厂模式
面向对象设计的基本原则
定义一个接口Car
public interface Car {
void run();
}
实现类Audi
public class Audi implements Car {
@Override
public void run() {
System.out.println("奥迪再跑!");
}
}
实现类Byd
public class Byd implements Car {
@Override
public void run() {
System.out.println("比亚迪再跑!");
}
}
测试类Client01,没有工厂模式的时候:
public class Client01 { //调用者
public static void main(String[] args) {
Car c1 = new Audi();
Car c2 = new Byd();
c1.run();
c2.run();
}
}
添加一个工厂类CarFactory
/**
* 根据要的类型进行创建
*/
public static Car createCar(String type) {
if (type.equals("奥迪")) {
return new Audi();
} else if (type.equals("比亚迪")) {
return new Byd();
}
return null;
}
工厂类也可以这样
public class CarFactory2 {
public static Audi getAudi() {
return new Audi();
}
public static Byd getByd() {
return new Byd();
}
}
测试类
public class testCa {
@Test
public void testCar() {
Car car = new Audi();
Car car1 = new Byd();
car.run();
car1.run();
}
@Test
public void testCar1() {
Car car = CarFactory.createCar("奥迪");
Car car1 = CarFactory.createCar("比亚迪");
car.run();
car1.run();
}
@Test
public void testCar2() {
Car car = CarFactory2.getAudi();
Car car1 = CarFactory2.getByd();
car.run();
car1.run();
}
}
public interface Car {
void run();
}
public interface CarFactory {
Car createCar();
}
public class Byd implements Car {
@Override
public void run() {
System.out.println("比亚迪在跑!");
}
}
public class Audi implements Car {
@Override
public void run() {
System.out.println("奥迪在跑!");
}
}
public class AudioFactory implements CarFactory {
@Override
public Car createCar() {
return new Audi();
}
}
public class BydFactory implements CarFactory {
@Override
public Car createCar() {
return new Byd();
}
}
Car car1=new AudioFactory().createCar();
Car car2=new BydFactory().createCar();
car1.run();
car2.run();
注意:按照六大原则,简单工厂不符合开闭原则,因此, 根据设计理论建议:工厂方法模式,但是实际上,我们一般都是使用简单工厂模式!
多个接口,构成了产品族的时候使用抽象工厂模式!
public interface Engine {
void run();
void start();
}
class HeightEngine 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("启动慢!");
}
}
public interface Seat {
void message();
}
class HeightSeat implements Seat {
@Override
public void message() {
System.out.println("可以自动按摩!");
}
}
class LowSeat implements Seat {
@Override
public void message() {
System.out.println("不能按摩!");
}
}
public interface Tyre {
void revolve();
}
class HeightTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转不磨损!");
}
}
class LowTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转磨损快!");
}
}
public interface CarFactory {
Engine createEngine();
Seat createSeat();
Tyre createTyre();
}
public class HeightCarFactory implements CarFactory {
@Override
public Engine createEngine() {
return new HeightEngine();
}
@Override
public Seat createSeat() {
return new HeightSeat();
}
@Override
public Tyre createTyre() {
return new HeightTyre();
}
}
public class LowCarFactory implements CarFactory {
@Override
public Engine createEngine() {
return new LowEngine();
}
@Override
public Seat createSeat() {
return new LowSeat();
}
@Override
public Tyre createTyre() {
return new LowTyre();
}
}
Engine engine = new HeightCarFactory().createEngine();
engine.run();
engine.start();
总结:
1. 简单工厂:有两种表达方式,第一种就好比万能建车厂,客户要什么类型,如果有,就直接new car;第二种方式就好比建车厂现在只有这两种车(Byd,Audi),客户只能买这两种车
2.工厂方法模式:就是Audi有专门的Audi建造厂,Byd有专门的Byd建造厂,而每一个建造厂都有一个共同的建造厂,实现创建车
3.抽象工厂模式:多个接口,构造成的产品族,比如车有座椅,轮子,镜子,发动机