GOF23设计模式(三)

GOF设计模式之工厂模式(Factory)

实例化对象,用工厂方法代替new操作。
将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。

工厂模式之简单工厂模式

接口实现

1
2
3
public interface Animal {
void run() ;
}

继承接口实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Cat implements Animal{
@Override
public void run() {
System.out.println("猫在叫");
}
}

public class Dog implements Animal{
@Override
public void run() {
System.out.println("狗在叫");
}
}

工厂

1
2
3
4
5
6
7
8
9
10
public class Animalfactory {
public static Animal get(String st) {
if(st.equals("猫")) {
return new Cat();
}else if(st.equals("狗")) {
return new Dog();
}else
return null;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 简单工厂模式
*
*/
public class Client { //调用者
public static void main(String[] args) {
Animal c1=Animalfactory.get("猫");
Animal c2=Animalfactory.get("狗");
c1.run();
c2.run();
}
}

GOF设计模式之工厂方法模式

接口实现

Animal接口

1
2
3
public interface Animal {
void run() ;
}

Creatfactory接口

1
2
3
public interface Creatfactory {
Animal creatAnimal();
}

继承接口实现方法

继承Animal

1
2
3
4
5
6
7
8
9
10
11
12
public class Cat implements Animal{
@Override
public void run() {
System.out.println("猫在叫");
}
}
public class Dog implements Animal{
@Override
public void run() {
System.out.println("狗在叫");
}
}

继承Creatfactory

1
2
3
4
5
6
7
8
9
10
11
12
public class Catfactory implements Creatfactory{
@Override
public Animal creatAnimal() {
return new Cat();
}
}
public class Dogfactory implements Creatfactory{
@Override
public Animal creatAnimal() {
return new Dog();
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 工厂方法模式
*
*/
public class Client {
public static void main(String[] args) {
Animal c1=new Catfactory().creatAnimal();
Animal c2=new Dogfactory().creatAnimal();
c1.run();
c2.run();
}
}

GOF设计模式之抽象工厂模式

接口实现

Tyre接口

1
2
3
public interface Tyre {
void revolve();
}

Seat接口

1
2
3
public interface Seat {
void massage();
}

Engine接口

1
2
3
4
public interface Engine {
void run();
void start();
}

CarFactory接口

1
2
3
4
5
public interface CarFactory {
Engine creatEngine();
Seat creatSeat();
Tyre creatTyre();
}

继承接口实现方法

继承Tyre

1
2
3
4
5
6
7
8
9
10
11
12
class LuxuryTyre implements Tyre{
@Override
public void revolve() {
System.out.println("磨损小");
}
}
class LowTyre implements Tyre{
@Override
public void revolve() {
System.out.println("磨损大");
}
}

继承Seat

1
2
3
4
5
6
7
8
9
10
11
12
class LuxurySeat implements Seat{
@Override
public void massage() {
System.out.println("可以按摩");
}
}
class LowSeat implements Seat{
@Override
public void massage() {
System.out.println("不可以按摩");
}
}

继承Engine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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("启动慢");
}
}

继承CarFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class LuxuryCarFactory implements CarFactory{
@Override
public Engine creatEngine() {
return new LuxuryEngine();
}
@Override
public Seat creatSeat() {
return new LuxurySeat();
}
@Override
public Tyre creatTyre() {
return new LuxuryTyre();
}
}
class LowCarFactory implements CarFactory{
@Override
public Engine creatEngine() {
return new LowEngine();
}
@Override
public Seat creatSeat() {
return new LowSeat();
}
@Override
public Tyre creatTyre() {
return new LowTyre();
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
public class Client {
public static void main(String[] args) {
CarFactory factory=new LuxuryCarFactory();
Engine e=factory.creatEngine();
CarFactory factory1=new LowCarFactory();
Engine e1=factory1.creatEngine();
e.run();
e.start();
e1.run();
e1.start();
}
}

总结

·核心本质

– 实例化对象,用工厂方法代替new操作。
– 将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。

·工厂模式

– 简单工厂模式
·用来生产同一等级结构中的任意产品。(对于增加新的产品,需要修改已有代码)
– 工厂方法模式
·用来生产同一等级结构中的固定产品。(支持增加任意产品)
– 抽象工厂模式
·用来生产不同产品族的全部产品。(对于增加的新产品,无能为力:支持增加产品族)

·工厂模式要点

– 简单工厂模式(静态工厂模式)
·虽然某种程度不符合设计原则,但实际应用最多。
– 工厂方法模式
·不修改已有类的前提下,通过增加新的工厂类实现扩展。
– 抽象工厂模式
·不可以增加产品,可以增加产品族。

原创技术分享,您的支持将鼓励我继续创作
0%