示例一
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 定义子系统A
typedef struct subsystemA {
void (*operationA)(struct subsystemA* subsystem);
} SubsystemA;
// 定义子系统B
typedef struct subsystemB {
void (*operationB)(struct subsystemB* subsystem);
} SubsystemB;
// 定义子系统C
typedef struct subsystemC {
void (*operationC)(struct subsystemC* subsystem);
} SubsystemC;
// 定义外观
typedef struct facade {
SubsystemA subsystemA;
SubsystemB subsystemB;
SubsystemC subsystemC;
void (*operation)(struct facade* facade);
} Facade;
// 实现子系统A的操作
void operationA(SubsystemA* subsystem) {
printf("SubsystemA operation.\n");
}
// 实现子系统B的操作
void operationB(SubsystemB* subsystem) {
printf("SubsystemB operation.\n");
}
// 实现子系统C的操作
void operationC(SubsystemC* subsystem) {
printf("SubsystemC operation.\n");
}
// 实现外观的操作
void operation(Facade* facade) {
facade->subsystemA.operationA(&facade->subsystemA);
facade->subsystemB.operationB(&facade->subsystemB);
facade->subsystemC.operationC(&facade->subsystemC);
}
int main(int argc, char *argv[])
{
Facade facade;
facade.subsystemA.operationA = operationA;
facade.subsystemB.operationB = operationB;
facade.subsystemC.operationC = operationC;
facade.operation = operation;
facade.operation(&facade);
return 0;
}
示例二
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _Computer {
void ( *work )( void );
} Computer;
void work( void )
{
printf( "Computer work here!\n" );
}
typedef struct _Phone {
void ( *contact )( void );
} Phone;
void contact( void )
{
printf( "Phone contact here!\n" );
}
typedef struct _Factory {
Computer *computer;
Phone *phone;
void ( *create )( struct _Factory *factory );
} Factory;
void create( Factory *factory )
{
//assert( NULL != factory );
factory->computer->work();
factory->phone->contact();
}
int main(int argc, char *argv[])
{
Computer computer = {
.work = work,
};
Phone phone = {
.contact = contact,
};
Factory factory = {
.create = create,
.computer = &computer,
.phone = &phone,
};
factory.create( &factory );
return 0;
}