designpatterns.zip

  • PUDN用户
    了解作者
  • Visual C++
    开发工具
  • 9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 3
    下载次数
  • 2010-06-28 15:27
    上传日期
Design Patterns sample files. It is very good and gives in depth understanding of the design patterns.
designpatterns.zip
  • samples
  • composite
  • MonoLithic.cpp
    2.5KB
  • Composite.h
    846B
  • Composite.cpp
    425B
  • adapter
  • Adapter.cpp
    372B
  • Adapter.h
    1.4KB
  • bridge
  • Bridge.cpp
    362B
  • Bridge.h
    1.6KB
  • facade
  • Facade.h
    1.1KB
  • Facade.cpp
    453B
  • Monolithic.cpp
    1.9KB
  • factory
  • MonolithicFactory.cpp
    1.9KB
  • Factory.cpp
    391B
  • FactoryMethod.h
    1.7KB
  • FactoryMethod.cpp
    524B
  • Factory.h
    911B
  • command
  • Command.h
    1.3KB
  • Command.cpp
    2KB
内容介绍
// Purpose. Composite #include <string.h> enum NodeType { FileT, DirT }; int g_indent = 0; class File { public: File( char* n ) { type_ = FileT; strcpy( name_, n ); } NodeType getType() { return type_; } void ls() { for (int i=0; i < g_indent; i++) cout << ' '; cout << name_ << endl; } private: NodeType type_; char name_[20]; }; class Dir { public: Dir( char* n ) { type_ = DirT; strcpy( name_, n ); total_ = 0; } NodeType getType() { return type_; } void add( File* f ) { files_[total_++] = f; } void ls() { for (int i=0; i < g_indent; i++) cout << ' '; cout << name_ << ":" << endl; g_indent += 3; for (int i=0; i < total_; i++) if (files_[i]->getType() == DirT) ((Dir*) files_[i])->ls(); else files_[i]->ls(); g_indent -= 3; } private: NodeType type_; char name_[20]; File* files_[10]; int total_; }; void main( void ) { Dir one("1"), two("2"), thr("3"); File a("a"), b("b"), c("c"), d("d"), e("e"); one.add( &a ); one.add( (File*) &two ); one.add( &b ); two.add( &c ); two.add( &d ); two.add( (File*) &thr ); thr.add( &e ); one.ls(); } // 1: // d // a // 3: // 2: // e // c // b
评论
    相关推荐