首页 > 代码库 > glib实践篇:接口定义与实现

glib实践篇:接口定义与实现

前言:

  在上一篇讲解了基于glib实现抽象和继承后,当然这篇就得讲讲接口类型啦!

  在JAVA中接口更多的弥补了其单继承所带来的缺陷,使其能够扩展很多功能,同时又不破坏它的结构。其实接口就是一种协议,在A类不能直接调用B类方法、尽量降低耦合性和避免杂糅混乱等情况下便可以使用接口来建立之间的联系。同样,本篇以一个简单的C语言例子来进行接口的实现。

开发环境:

  eclipse集成开发环境、glib库、ubuntu操作系统。

设计流程:

  1、定义一个test接口类,定义4个接口方法

  2、定义一个test类实现这个接口,执行结果就是打印4个方法的命名

  3、主函数调用接口测试

以下是类的设计:

  test接口类:
  testhandler.h

 1 #ifndef __TEST_HANDLER_H__
 2 #define __TEST_HANDLER_H__
 3 
 4 #include <glib-object.h>
 5 #include<glib.h>
 6 G_BEGIN_DECLS
 7 
 8 #define TYPE_TEST_HANDLER                 (test_handler_get_type())
 9 #define TEST_HANDLER(obj)                 (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_TEST_HANDLER, TestHandlerInterface))
10 #define IS_TEST_HANDLER(obj)              (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_TEST_HANDLER))
11 #define TEST_HANDLER_INTERFACE(klass)     ((TestHandlerInterface*)g_type_interface_peek((klass),  TYPE_TEST_HANDLER))
12 #define GET_TEST_HANDLER_INTERFACE(obj)   (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TYPE_TEST_HANDLER, TestHandlerInterface))
13 
14 typedef struct _TestHandlerInterface       TestHandlerInterface;
15 
16 struct _TestHandlerInterface
17 {
18     GObjectClass parent_iface;
19 
20     void (*start)(TestHandlerInterface *self);
21     void (*pause)(TestHandlerInterface *self);
22     void (*resume)(TestHandlerInterface *self);
23     void (*stop)(TestHandlerInterface *self);
24 };
25 GType test_handler_get_type(void);
26 
27 G_END_DECLS
28 #endif /* _TEST_HANDLER_H_ */

  testhandler.c

 1 #include "testhandler.h"
 2 
 3 /************ defines ************/
 4 G_DEFINE_INTERFACE(TestHandler, test_handler, G_TYPE_OBJECT)
 5 
 6 
 7 static void test_handler_default_init(TestHandlerInterface *iface)
 8 {
 9 
10 }

  test类(实现接口的类):

  test.h

 1 #ifndef __TEST_H__
 2 #define __TEST_H__
 3 
 4 #include <glib-object.h>
 5 
 6 G_BEGIN_DECLS
 7 
 8 #define TYPE_TEST            (test_get_type())
 9 #define TEST(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_TEST, Test))
10 #define TEST_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),  TYPE_TEST, TestClass))
11 #define IS_TEST(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_TEST))
12 #define IS_TEST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),  TYPE_TEST))
13 #define GET_TEST_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TEST, TestClass))
14 
15 /************ types ************/
16 typedef struct _Test            Test;
17 typedef struct _TestClass       TestClass;
18 typedef struct _TestPrivate     TestPrivate;
19 
20 struct _Test
21 {
22     GObject parent;
23 };
24 struct _TestClass
25 {
26     GObjectClass parent_class;
27 };
28 
29 /************ globals ************/
30 
31 
32 /************ function declarations ************/
33 GType test_get_type(void);
34 
35 G_END_DECLS
36 #endif /* _TEST_H_ */

  test.c 

 1 #include"test.h"
 2 #include"testhandler.h"
 3 
 4 /************ defines ************/
 5 static void test_handler_interface_init(TestHandlerInterface *iface);
 6 G_DEFINE_TYPE_WITH_CODE(Test, test, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(TYPE_TEST_HANDLER, test_handler_interface_init))
 7 #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_TEST, TestPrivate))
 8 
 9 /************ types ************/
10 struct _TestPrivate
11 {
12 
13 };
14 /************ function declarations ************/
15 
16 static void dispose_od(GObject *object);
17 static void finalize_od(GObject *object);
18 void start_od(TestHandlerInterface *self);
19 void pause_od(TestHandlerInterface *self);
20 void resume_od(TestHandlerInterface *self);
21 void stop_od(TestHandlerInterface *self);
22 /************ function implements ************/
23 
24 static void test_class_init(TestClass *klass)
25 {
26     GObjectClass *g_object_class = G_OBJECT_CLASS(klass);
27     g_type_class_add_private(g_object_class, sizeof(TestPrivate));
28     g_object_class->dispose = dispose_od;
29     g_object_class->finalize = finalize_od;
30 
31 }
32 
33 static void test_handler_interface_init(TestHandlerInterface *iface)
34 {
35     TestHandlerInterface *testHandlerIface = iface;
36     testHandlerIface->start=start_od;
37     testHandlerIface->pause=pause_od;
38     testHandlerIface->resume=resume_od;
39     testHandlerIface->stop=stop_od;
40 }
41 
42 static void test_init(Test *self)
43 {
44     TestPrivate *priv = GET_PRIVATE(self);
45 }
46 
47 static void dispose_od(GObject *object)
48 {
49     Test *self = (Test*) object;
50     TestPrivate *priv = GET_PRIVATE(self);
51     // 释放创建的对象
52     G_OBJECT_CLASS(test_parent_class)->dispose(object);
53 }
54 
55 static void finalize_od(GObject *object)
56 {
57     Test *self = (Test*) object;
58     TestPrivate *priv = GET_PRIVATE(self);
59     // 释放创建的内存
60     G_OBJECT_CLASS(test_parent_class)->finalize(object);
61 }
62 
63 void start_od(TestHandlerInterface *self)
64 {
65     TestPrivate *priv = GET_PRIVATE(self);
66     g_print("start_od!\n");
67 }
68 void pause_od(TestHandlerInterface *self)
69 {
70     TestPrivate *priv = GET_PRIVATE(self);
71     g_print("pause_od!\n");
72 }
73 void resume_od(TestHandlerInterface *self)
74 {
75     TestPrivate *priv = GET_PRIVATE(self);
76     g_print("resume_od!\n");
77 }
78 void stop_od(TestHandlerInterface *self)
79 {
80     TestPrivate *priv = GET_PRIVATE(self);
81     g_print("stop_od!\n");
82 }

  主函数:

 1 #include<glib.h>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include"test.h"
 5 #include"testhandler.h"
 6 #include<string.h>
 7 int main(void)
 8 {
 9         Test *test = g_object_new(TYPE_TEST,NULL);
10          if (test != NULL)
11          {
12              GET_TEST_HANDLER_INTERFACE(test)->start(TEST_HANDLER(test));
13              GET_TEST_HANDLER_INTERFACE(test)->pause(TEST_HANDLER(test));
14              GET_TEST_HANDLER_INTERFACE(test)->resume(TEST_HANDLER(test));
15              GET_TEST_HANDLER_INTERFACE(test)->stop(TEST_HANDLER(test));
16          }
17     return EXIT_SUCCESS;
18 }

总结:

  从主函数中的测试代码中我们可以看出,调用接口的一方或类还是需要联系接口方法的实现类,耦合度还是较高,而在JAVA中接口则能够很好的分离两方。所以在C语言中接口主要是用于上层结构设计,同时由于gobject并未

能很好的实现接口,所以在项目中为了是两者很好的分离,其中一个办法就是通过指针调用来尽量做到低耦合,例如使用重载等方法。

glib实践篇:接口定义与实现