赞
踩
GOF的《设计模式》一书的副标题叫做“可复用面向对象软件的基础”,从标题就能看出面向对象是设计模式基本思想。
由于C语言并不是面向对象的语言,C语言没有直接提供封装、继承、组合、多态等面向对象的功能,但C语言有struct和函数指针。我们可以用struct中的数据和函数指针,以此来模拟对象和类的行为。
所以在正式开始设计模式前,先看看如何用C语言实现面向对象编程。
本章针对面向对象的封装、继承、组合、多态给出C语言的实现方法。
封装是指对象仅暴露必要的对外接口(这里指public方法)来和其它对象进行交互,其它的属性和行为都无需暴露,这使得对象的内部实现可以自由修改。
这也要求对象包含它能进行操作所需要的所有信息,不必依赖其它对象来完成自己的操作。
以下以电力公司的例子演示封装。
电力公司生产并提供电力。为了汇聚各种发电厂的电力并让用户获得电力,电力公司提供了两个统一接口:
1、电力公司汇聚各种发电厂的电力,无论是火力发电厂、水力发电厂、原子能发电厂等都使用一个接口。如果什么时候一家火力发电厂改造成了风力发电厂,发电厂的实现完全不一样了,但接口不变,所以电力公司感觉不到发电厂变了,不需要为了发电厂实现升级而改造电力公司的系统。
2、电力公司向用户提供电力,无论用户用电的设备是烤面包机还是洗衣机,电力公司和用户之间都使用一个接口(电源插座)。用户的用电设备可以千变万化,但接口(电源插座)不变。所以电力公司不用关系用户的什么设备在用电。
- #include <stdio.h>
-
- struct PowerCompany {
- int powerReserve;
- void (*PowerPlant)(struct PowerCompany *this, int power);
- void (*PowerUser)(struct PowerCompany *this, int power);
- };
-
-
- void PowerPlant(struct PowerCompany *this, int power)
- {
- this->powerReserve += power;
- printf("默认发电厂,发电%d瓦\n", power);
- return;
- }
-
- void PowerUser(struct PowerCompany *this, int power)
- {
- if (this->powerReserve >= power) {
- printf("用电%d瓦\n", power);
- this->powerReserve -= power;
- } else {
- printf("电力不足,用电失败\n");
- }
- return;
- }
-
- /* struct PowerCompany 的构造函数 */
- void PowerCompany(struct PowerCompany *this)
- {
- this->powerReserve = 0;
- this->PowerPlant = PowerPlant;
- this->PowerUser = PowerUser;
- return;
- }
-
- /* struct PowerCompany 的析构函数 */
- void _PowerCompany(struct PowerCompany *this)
- {
-
- }
-
- int main(void)
- {
- struct PowerCompany myPowerCompany;
- PowerCompany(&myPowerCompany);
-
- /* 发电 */
- myPowerCompany.PowerPlant(&myPowerCompany, 1000);
-
- /* 用电 */
- myPowerCompany.PowerUser(&myPowerCompany, 800);
- myPowerCompany.PowerUser(&myPowerCompany, 800);
-
- _PowerCompany(&myPowerCompany);
- return 0;
- }

从电力公司的例子中可以看出,良好的封装可以有效减少耦合性,封装内部实现可以自由修改,对系统的其它部分没有影响。
面向对象编程最强大的功能之一就是代码重用,而继承就是实现代码重用的主要手段之一。继承允许一个类继承另一个类的属性和方法。
我们可以通过识别事物之间的共性,通过抽象公共属性和行为来构造父类,而通过继承父类来构造各子类。父类,即公共属性和行为,就得到了复用。
以下哺乳动物的例子演示继承。
猫和狗都是哺乳动物,它们具有公共的属性和行为。比如,猫和狗都有眼睛,且它们都会叫。
我们把眼睛的颜色、会叫抽象出来,作为哺乳动物父类的属性,让猫类、狗类都继承哺乳动物父类,可实现对”眼睛的颜色“、”会叫“实现的复用。
- #include <stdio.h>
-
- struct Mammal {
- int eyeColor;
- void (*ShowEyeColor)(struct Mammal *this);
- int callNum;
- void (*Call)(struct Mammal *this);
- };
-
- void ShowEyeColor(struct Mammal *this)
- {
- if (this->eyeColor == 1) {
- printf("眼睛是绿色\n");
- } else {
- printf("眼睛是蓝色\n");
- }
- return;
- }
-
- void Call(struct Mammal *this)
- {
- printf("叫%d声\n", this->callNum);
- return;
- }
-
- // struct Mammal 的构造函数
- void Mammal(struct Mammal *this, int eyeColor, int callNum)
- {
- this->eyeColor = eyeColor;
- this->ShowEyeColor = ShowEyeColor;
- this->callNum = callNum;
- this->Call = Call;
- return;
- }
-
- struct Dog {
- struct Mammal mammal;
- };
-
- // struct Dog 的构造函数
- void Dog(struct Dog *this, int eyeColor, int callNum)
- {
- Mammal(this, eyeColor, callNum);
- // 狗类的其它属性,略
- return;
- }
-
- // struct Dog 的析构函数
- void _Dog(struct Dog *this)
- {
-
- }
-
- struct Cat {
- struct Mammal mammal;
- // 猫类的其它属性,略
- };
-
- // struct Cat 的构造函数
- void Cat(struct Cat *this, int eyeColor, int callNum)
- {
- Mammal(this, eyeColor, callNum);
- return;
- }
-
- // struct Cat 的析构函数
- void _Cat(struct Cat *this)
- {
-
- }
-
- int main(void)
- {
- struct Dog myDog;
- Dog(&myDog, 1, 3);
- myDog.mammal.ShowEyeColor(&myDog);
- myDog.mammal.Call(&myDog);
- _Dog(&myDog);
-
- struct Cat myCat;
- Cat(&myCat, 2, 5);
- myCat.mammal.ShowEyeColor(&myCat);
- myCat.mammal.Call(&myCat);
- _Cat(&myCat);
-
- return 0;
- }

多态与继承是紧耦合的关系,但它通常作为面向对象技术中最强大的优点之一。
子类从继承父类的接口,每个子类是单独的实体,每个子类需要对同一消息有单独的应答。
每个子类对同一消息的应答采用继承的相同接口,但每个子类可以有不同的实现,这就是多态。
在猫和狗的例子中,猫类、狗类都继承了哺乳动物父类的“叫”的方法,但猫类、狗类的叫声并不一样,所以猫类、狗类可以采用不同的“叫”的实现。
以下代码演示了多态。
- #include <stdio.h>
-
- struct Mammal {
- int eyeColor;
- void (*ShowEyeColor)(struct Mammal *this);
- int callNum;
- void (*Call)(struct Mammal *this);
- };
-
- void ShowEyeColor(struct Mammal *this)
- {
- if (this->eyeColor == 1) {
- printf("眼睛是绿色\n");
- } else {
- printf("眼睛是蓝色\n");
- }
- return;
- }
-
- void Call(struct Mammal *this)
- {
- printf("叫%d声\n", this->callNum);
- return;
- }
-
- /* struct Mammal 的构造函数 */
- void Mammal(struct Mammal *this, int eyeColor, int callNum)
- {
- this->eyeColor = eyeColor;
- this->ShowEyeColor = ShowEyeColor;
- this->callNum = callNum;
- this->Call = Call;
- return;
- }
-
- struct Dog {
- struct Mammal mammal;
- };
-
- void Bark(struct Dog *this)
- {
- int i;
- for (i = 0; i < this->mammal.callNum; i++) {
- printf("汪 ");
- }
- printf("\n");
- return;
- }
-
- /* struct Dog 的构造函数 */
- void Dog(struct Dog *this, int eyeColor, int callNum)
- {
- Mammal(this, eyeColor, callNum);
- this->mammal.Call = Bark;
- return;
- }
-
- // struct Dog 的析构函数
- void _Dog(struct Dog *this)
- {
-
- }
-
- struct Cat {
- struct Mammal mammal;
- };
-
- void Meow(struct Cat *this)
- {
- int i;
- for (i = 0; i < this->mammal.callNum; i++) {
- printf("喵 ");
- }
- printf("\n");
- return;
- }
-
- /* struct Cat 的构造函数 */
- void Cat(struct Cat *this, int eyeColor, int callNum)
- {
- Mammal(this, eyeColor, callNum);
- this->mammal.Call = Meow;
- return;
- }
-
- // struct Cat 的析构函数
- void _Cat(struct Cat *this)
- {
-
- }
-
- int main(void)
- {
- struct Dog myDog;
- Dog(&myDog, 1, 3);
-
- struct Cat myCat;
- Cat(&myCat, 2, 5);
-
- struct Mammal *myMammal;
- myMammal = &myDog;
- myMammal->Call(myMammal);
- myMammal = &myCat;
- myMammal->Call(myMammal);
-
- _Dog(&myDog);
- _Cat(&myCat);
-
- return 0;
- }

组合与继承都是面向对象中代码复用的方式,也只有通过组合和继承两种方式能够实现使用其他类构建新类。
在前面讲的继承关系中,我们把通用属性和行为抽象出来作为父类。
例如:猫、狗都是哺乳动物,它们具有哺乳动物通用的属性和行为。猫、狗与哺乳动物的关系是“is-a”,即猫、狗(is-a)哺乳动物。而组合关系体现的是“has-a”。以房子和窗户的关系举例。
我们可以单独构建窗户类,然后把窗户类应用到各种房子类上。此时房子(has-a)窗户,但绝不是窗户(is-a)房子。
以下UML和代码演示了组合。
- #include <stdio.h>
-
- struct Window {
- int length;
- int width;
- void (*ShowWindow)(struct Window *this);
- };
-
- void ShowWindow(struct Window *this)
- {
- printf("这是长%d厘米、宽%d厘米的窗户\n", this->length, this->width);
- return;
- }
-
- void Window(struct Window *this, int length, int width)
- {
- this->length = length;
- this->width = width;
- this->ShowWindow = ShowWindow;
- return;
- }
-
- void _Window(struct Window *this)
- {
-
- }
-
- struct House {
- struct Window *window;
- int livingRoomNum;
- int bedRoomNum;
- int bathRoomNum;
- void (*ShowHouse)(struct House *this);
- };
-
- void ShowHouse(struct House *this)
- {
- printf("这是%d室%d厅%d卫的房子\n", this->bedRoomNum, this->livingRoomNum, this->bathRoomNum);
- return;
- }
-
-
- void House(struct House *this, int livingRoomNum, int bedRoomNum, int bathRoomNum)
- {
- this->livingRoomNum = livingRoomNum;
- this->bedRoomNum = bedRoomNum;
- this->bathRoomNum = bathRoomNum;
- this->ShowHouse = ShowHouse;
- return;
- }
-
- void _House(struct House *this)
- {
-
- }
-
- void main()
- {
- struct House myHouse;
- House(&myHouse, 2, 3, 2);
-
- /* 组合是一种低耦合,如果不初始化,子类只是存放了一个空指针来占位关联。
- 此处是与继承关系的区别。继承是一种强耦合,在继承关系中,无论如何子类拥有父类全部的信息。*/
- struct Window myWindow1;
- myHouse.window = &myWindow1;
- Window(myHouse.window, 100, 50);
-
- /* 通过获得其它对象的引用而在“运行时”动态定义 */
- myHouse.ShowHouse(&myHouse);
- myHouse.window->ShowWindow(myHouse.window);
-
- _Window();
- _House();
-
- return;
- }

组合和继承的区别有以下几点:
组合关系体现的是“has-a”。继承关系体现的是“is-a”。
由于微信公众号近期改变了推送规则,如果您想经常看到我们的文章,可以在每次阅读后,在页面下方点一个「赞」或「在看」,这样每次推送的文章才会第一时间出现在您的订阅列表里。
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。