main.c:
#include "abs_factory.h"#include "abs_product.h"#include "con_factoryA.h"#include "con_factoryB.h"#include "con_productA.h"#include "con_productB.h"#include intmain (void){AbsFactory *fact_B = con_factoryB_new();AbsFactory *fact_A = con_factoryA_new();AbsProduct *pro_A = abs_factory_make_product (fact_A);AbsProduct *pro_B = abs_factory_make_product (fact_B);abs_product_print (pro_A);abs_product_print (pro_B);g_object_unref (fact_A);g_object_unref (fact_B);g_object_unref (pro_A);g_object_unref (pro_B);}
输出结果为:
called print() from ConProductAcalled print() from ConProductB
有个易错点,而且很难排查。
就是__class_init( XXX *class_)
中,要获取类需要用XXX_CLASS(class_)
强制转换即可。
而其他地方,基本都是实例参数,用XXX_GET_CLASS(class_)
来访问类。
比如下面的具体工厂类的函数:
static voidcon_factoryA_class_init (ConFactoryAClass *class_){AbsFactoryClass *parent_class = ABS_FACTORY_CLASS (class_);parent_class->make_product = override_make_product;}
这里如果不小心用成ABS_FACTORY_GET_CLASS
,那么首先会导致覆盖失败,然后,在运行时可能会导致内存问题、。
使用XXX_GET_CLASS
的例子:
AbsProduct *abs_factory_make_product (AbsFactory *self){AbsFactoryClass *parent_class = ABS_FACTORY_GET_CLASS (self);if (parent_class->make_product == NULL) {g_print ("abs_factory_make_product() 未被覆盖!\n");abort();}return parent_class->make_product (self);}
一定要注意这一点,用错了也不会有任何提示。
一、产品类
1. 抽象产品类AbsProduct
abs_product.h
#ifndef ABS_PRODUCT_H#define ABS_PRODUCT_H#include #define ABS_TYPE_PRODUCT(abs_product_get_type())G_DECLARE_DERIVABLE_TYPE (AbsProduct, abs_product, ABS, PRODUCT, GObject)struct _AbsProductClass {GObjectClass base_class;void (*print) (AbsProduct *self);};voidabs_product_print (AbsProduct *self);#endif // ABS_PRODUCT_H
abs_product.c
#include "abs_product.h"G_DEFINE_ABSTRACT_TYPE (AbsProduct, abs_product, G_TYPE_OBJECT)static voidabs_product_init (AbsProduct *instance){ }static voidabs_product_class_init (AbsProductClass *class_){class_->print = NULL;}voidabs_product_print (AbsProduct *self){AbsProductClass *parent_class = ABS_PRODUCT_GET_CLASS (self);if (parent_class->print == NULL) {g_print ("abs_product_print() 未被覆盖!\n");abort();} else {parent_class->print (self);}}
2. 具体产品类ConProductA
con_productA.h
#ifndef CON_PRODUCTA_H#define CON_PRODUCTA_H#include "abs_product.h"#define CON_TYPE_PRODUCTA (con_productA_get_type())G_DECLARE_FINAL_TYPE (ConProductA, con_productA, CON, PRODUCTA, AbsProduct)AbsProduct *con_productA_new();#endif // CON_PRODUCTA_H
con_productA.c
#include "con_productA.h"struct _ConProductA {AbsProduct base_class;};G_DEFINE_FINAL_TYPE (ConProductA, con_productA, ABS_TYPE_PRODUCT)static voidoverride_print (AbsProduct *self){if (!CON_IS_PRODUCTA (self)) {g_print ("con_productA.override_print(): 类型不兼容\n");abort();}g_print ("called print() from ConProductA\n");}static voidcon_productA_init (ConProductA *instance){ }static voidcon_productA_class_init (ConProductAClass *class_){AbsProductClass *parent_class = ABS_PRODUCT_CLASS (class_);parent_class->print = override_print;}AbsProduct *con_productA_new(){return g_object_new (CON_TYPE_PRODUCTA, NULL);}
3. 具体产品类ConProductB
con_productB.h
#ifndef CON_PRODUCTB_H#define CON_PRODUCTB_H#include "abs_product.h"#define CON_TYPE_PRODUCTB (con_productB_get_type())G_DECLARE_FINAL_TYPE (ConProductB, con_productB, CON, PRODUCTB, AbsProduct)AbsProduct *con_productB_new();#endif // CON_PRODUCTB_H
con_productB.c
#include "con_productB.h"struct _ConProductB {AbsProduct base_class;};G_DEFINE_FINAL_TYPE (ConProductB, con_productB, ABS_TYPE_PRODUCT)static voidoverride_print (AbsProduct *self){if (!CON_IS_PRODUCTB (self)) {g_print ("ConProductB.override_print(): 类型不兼容\n");abort();}g_print ("called print() from ConProductB\n");}static voidcon_productB_init (ConProductB *instance){ }static voidcon_productB_class_init (ConProductBClass *class_){AbsProductClass *parent_class = ABS_PRODUCT_CLASS (class_);parent_class->print = override_print;}AbsProduct *con_productB_new(){return g_object_new (CON_TYPE_PRODUCTB, NULL);}
二、工厂类
1. 抽象工厂类AbsFactory
abs_factory.h
#ifndef ABS_FACTORY_H#define ABS_FACTORY_H#include "abs_product.h"#include #define ABS_TYPE_FACTORY(abs_factory_get_type())G_DECLARE_DERIVABLE_TYPE (AbsFactory, abs_factory, ABS, FACTORY, GObject)struct _AbsFactoryClass {GObjectClass base_class;AbsProduct * (*make_product) (AbsFactory *self);};AbsProduct *abs_factory_make_product (AbsFactory *self);#endif // ABS_FACTORY_H
abs_factory.c
#include "abs_factory.h"G_DEFINE_ABSTRACT_TYPE (AbsFactory, abs_factory, G_TYPE_OBJECT)static voidabs_factory_init (AbsFactory *instance){ }static voidabs_factory_class_init (AbsFactoryClass *class_){class_->make_product = NULL;}AbsProduct *abs_factory_make_product (AbsFactory *self){AbsFactoryClass *parent_class = ABS_FACTORY_GET_CLASS (self);if (parent_class->make_product == NULL) {g_print ("abs_factory_make_product() 未被覆盖!\n");abort();}return parent_class->make_product (self);}
2. 具体工厂类ConFactoryA
con_factoryA.h
#ifndef CON_FACTORYA_H#define CON_FACTORYA_H#include "abs_product.h"#include "abs_factory.h"#define CON_TYPE_FACTORYA (con_factoryA_get_type())G_DECLARE_FINAL_TYPE (ConFactoryA, con_factoryA, CON, FACTORYA, AbsFactory)AbsFactory *con_factoryA_new();#endif // CON_FACTORYA_H
con_factoryA.c
#include "con_factoryA.h"#include "abs_factory.h"#include "con_productA.h"struct _ConFactoryA {AbsFactory base_class;};G_DEFINE_TYPE (ConFactoryA, con_factoryA, ABS_TYPE_FACTORY)static AbsProduct *override_make_product (AbsFactory *self){if (!CON_IS_FACTORYA (self)) {g_print ("ConFactoryA.override_make_product(): 类型不兼容\n");abort();} elsereturn con_productA_new();}static voidcon_factoryA_init (ConFactoryA *instance){ }static voidcon_factoryA_class_init (ConFactoryAClass *class_){AbsFactoryClass *parent_class = ABS_FACTORY_CLASS (class_);parent_class->make_product = override_make_product;}AbsFactory *con_factoryA_new(){return g_object_new (CON_TYPE_FACTORYA, NULL);}
.3. 具体工厂类ConFactoryB
con_factoryB.h
#ifndef CON_FACTORYB_H#define CON_FACTORYB_H#include "abs_product.h"#include "abs_factory.h"#define CON_TYPE_FACTORYB (con_factoryB_get_type())G_DECLARE_FINAL_TYPE (ConFactoryB, con_factoryB, CON, FACTORYB, AbsFactory)AbsFactory *con_factoryB_new();#endif // CON_FACTORYB_H
con_factoryB.c
#include "con_factoryB.h"#include "abs_factory.h"#include "con_productB.h"struct _ConFactoryB {AbsFactory base_class;};G_DEFINE_TYPE (ConFactoryB, con_factoryB, ABS_TYPE_FACTORY)static AbsProduct *override_make_product (AbsFactory *self){if (!CON_IS_FACTORYB (self)) {g_print ("ConFactoryB.override_make_product(): 类型不兼容\n");abort();} elsereturn con_productB_new();}static voidcon_factoryB_init (ConFactoryB *instance){ }static voidcon_factoryB_class_init (ConFactoryBClass *class_){AbsFactoryClass *parent_class = ABS_FACTORY_CLASS (class_);parent_class->make_product = override_make_product;}AbsFactory *con_factoryB_new(){return g_object_new (CON_TYPE_FACTORYB, NULL);}