首页 Flutter 设计模式之迪米特原则
文章
取消

Flutter 设计模式之迪米特原则

什么是迪米特法则?

迪米特法则(law of Demeter)也被称为最少知识原则,它提出一个模块对其他模块应该知之甚少,或者说模块之间应该彼此保持陌生,甚至意识不到对方的存在,以此最小化、简单化模块间的通信,并达到松耦合的目的。

迪米特法则有什么作用?

1、降低了类之间的耦合度,提高了模块的相对独立性。

2、由于藕合度降低,从而提高了类的可复用率和系统的扩展性。

案例分析

我们以租房为例,当客户需要租房子住,我们直接建立一个房子类以及一个客户类,客户直接找房子即可

方案一:初始设计

1、定义房子的抽象类,并添加一个住房子的方法

2、定义房子实现类并重写方法

3、定义客户类,名称以及找房子的方法

1、定义房子抽象类

1
2
3
4
5
6
7
abstract class IHouse {
  // 房子名称
  String name;
  IHouse(this.name);
  // 租房
  IHouse housing();
}

2、定义房子实现类

1
2
3
4
5
6
7
class House extends IHouse {
  House(String name):super(name);
  @override
  IHouse housing() {
    return this;
  }
}

3、定义客户抽象类

1
2
3
4
5
6
7
abstract class ICustomer {
  // 客户名称
  String name;
  ICustomer(this.name);
  // 找房子
  IHouse findHourse(IHouse house);
}

4、定义客户实现类

1
2
3
4
5
6
7
8
9
class Customer extends ICustomer {
  String name;
  Customer(String name);
  // 找房子
  @override
  IHouse findHourse(IHouse house) {
    return house.housing();
  }
}

5、使用

1
2
3
4
5
6
7
8
9
void main(List<String> args) {
  // 房子
  IHouse house = House("别墅");
  // 客户
  Customer customer = Customer("小明");
  // 租到的房子
  IHouse customerHouse = customer.findHourse(house);
  print(customer.name + "租到了" + customerHouse.name);
}

6、运行结果

1
2
3
Connecting to VM Service at http://127.0.0.1:64764/OfVO5cZrGg0=/
小明租到了别墅
Exited

7、完整代码

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void main(List<String> args) {
  // 房子
  IHouse house = House("别墅");
  // 客户
  Customer customer = Customer("小明");
  // 租到的房子
  IHouse customerHouse = customer.findHourse(house);
  print(customer.name + "租到了" + customerHouse.name);
}

abstract class IHouse {
  // 房子名称
  String name;
  IHouse(this.name);
  // 租房
  IHouse housing();
}

class House extends IHouse {
  House(String name):super(name);
  @override
  IHouse housing() {
    return this;
  }
}

abstract class ICustomer {
  // 客户名称
  String name;
  ICustomer(this.name);
  // 找房子
  IHouse findHourse(IHouse house);
}

class Customer extends ICustomer {
  Customer(String name):super(name);
  // 找房子
  @override
  IHouse findHourse(IHouse house) {
    return house.housing();
  }
}

8、分析

通过上面的代码我们实现了功能,客户租到了房子住,逻辑也非常简单,但违背了迪米尔法则,我们来对它进行优化。

方案二:中介模式

相信大家一定经历过自己租房子,没有房源、只能自己亲自到某个地方打电话去看房,而且需要找很多家去做对比,这样就显得很费劲。那这样的还不如交给中介,中介手里有很多房源,房东把房子交给中介,不需要关心租户是谁。而租户也不需要知道房东是谁,租户找房的事交给了中介。租户和房东都很省事,只需要支付一定的中介费用。

1、定义房子抽象类

1
2
3
4
5
6
7
abstract class IHouse {
  // 房子名称
  late String name;
  IHouse(this.name);
  // 租房
  IHouse housing();
}

2、定义房子实现类

1
2
3
4
5
6
7
8
9
class House extends IHouse {

  House(String name):super(name);

  @override
  IHouse housing() {
    return this;
  }
}

3、定义客户抽象类

1
2
3
4
5
6
7
abstract class ICustomer {
  // 客户名称
  late String name;
  ICustomer(this.name);
  // 找房子
  IHouse findHourse(IHouse house);
}

4、定义客户实现类

1
2
3
4
5
6
7
8
9
10
class Customer extends ICustomer {

  Customer(String name): super(name);

  // 找房子
  @override
  IHouse findHourse(IHouse house) {
    return house.housing();
  }
}

5、定义中介类

1
2
3
4
5
6
7
// 中介
class Intermediary {
  IHouse findHouser(ICustomer customer) {
    IHouse house = House("普通住宅");
    return customer.findHourse(house);
  }
}

6、使用

1
2
3
4
5
6
7
8
9
10
11
void main(List<String> args) {
  // 客户
  ICustomer customer = Customer("小红");

  // 中介
  Intermediary intermediary = Intermediary();

  // 租到了房子
  IHouse house = intermediary.findHouser(customer);
  print(customer.name + "租到了" + house.name);
}

7、运行结果

1
2
3
Connecting to VM Service at http://127.0.0.1:49894/5qfmFaENW1M=/
小红租到了普通住宅
Exited

8、完整代码

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void main(List<String> args) {
  // 客户
  ICustomer customer = Customer("小红");

  // 中介
  Intermediary intermediary = Intermediary();

  // 租到了房子
  IHouse house = intermediary.findHouser(customer);
  print(customer.name + "租到了" + house.name);

}

abstract class IHouse {
  // 房子名称
  late String name;
  IHouse(this.name);
  // 租房
  IHouse housing();
}

class House extends IHouse {

  House(String name):super(name);

  @override
  IHouse housing() {
    return this;
  }
}

abstract class ICustomer {
  // 客户名称
  late String name;
  ICustomer(this.name);
  // 找房子
  IHouse findHourse(IHouse house);
}

class Customer extends ICustomer {

  Customer(String name): super(name);

  // 找房子
  @override
  IHouse findHourse(IHouse house) {
    return house.housing();
  }
}

// 中介
class Intermediary {
  IHouse findHouser(ICustomer customer) {
    IHouse house = House("普通住宅");
    return customer.findHourse(house);
  }
}

9、分析

我们通过定义一个中介类来作为租客和房东沟通的桥梁,这样房东也不用关心租客是谁,租客也不需要关心房东是谁。

总结

我们通过一个租房的案例来说明了迪米尔法则作用,这样能有效的降低类之间的耦合度,提高了模块的相对独立性。

本文由作者按照 CC BY 4.0 进行授权