外观/门面facade模式可以将应用与一个复杂的第三方库简单地连接起来,尤其是应用只依赖于复杂第三方库的一小部分功能的时候。它有四个部分
- Facade
- Additional Facade: 继承Facade并为Facade添加一些其他功能,而不改变Facade这一简单接口本身
- The Complex system
- client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| class VideoFile { }
class OggCompressionCodec { }
class MPEG4CompressionCodec { }
class CodecFactory { }
class BitrateReader { }
class AudioMixer { }
class VideoConverter { public: File convert(std::string& filename, std::string& format) { VideoFile file = VideoFile(filename); CodecFactory codecfactory = CodecFactory(); Codec* sourceCodec = codecfactory.extract(file); Codec* destinationCodec = nullptr; if (format == "mp4") destinationCodec = MPEG4CompressionCodec(); else destinationCodec = OggCompressionCodec(); Buffer buffer = BitrateReader::read(filename, sourceCodec); Buffer result = BitrateReader::convert(buffer, destinationCodec); AutoMixer mixer = AutoMixer(); result = mixer.fix(result); delete destinationCodec; delete sourceCodec; return File(result) } }
int main(int argc, char* argv[]) { VideoConverter converter = VideoConverter(); File mp4 = converter->convert("some.ogg", "mp4"); mp4.save();
return 0; }
|