Interfaces give a lot of support for OO-design in a language. Obviously, they can be done in C, but it takes a fair amount of writing "boring" code to do it. This gives an example of how it can be done using the c_intf_gen.py Python script to remove a lot of that "boring" code.
This example implements the GoF's Abstract Factory design pattern.
The interface structure is given via the description file abs_factory_def.txt
. Basically the gui_factory
interface has a create_button
function and is implemented by both the win_factory
and osx_factory
classes. The button
interface has a paint
function and is implemented by both the win_button
and osx_button
classes.
In generate_gui
in test_abs_factory.c, we specify a way of building GUIs that create buttons and is OS-indepenent. This same method of constructing GUIs can then be shared by different OS-specific GUI factories that create their own widgets.
The files follow the convention:
my_class
.my_class
. Should only be included by classes that implement from my_class
.my_class
and includes private data not directly accessible by even friend classes.Assuming your c_intf_gen.py script is in the parent directory (if not, the GEN_SCRIPT
variable in the Makefile
can be modified), just do:
make all
./test_abs_factory
You can edit test_abs_factory.c
to try various things with this class hierarchy. Running make clean
will remove the executable, object, and generated files.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.