1. 不好的代码
1.1 lib.cpp
class Library {
public : void Step1 ( ) { } void Step3 ( ) { } void Step5 ( ) { }
} ;
1.2 app.cpp
class Application {
public : bool Step2 ( ) { } void Step4 ( ) { }
} ; int main ( )
{ Library lib ( ) ; Application app ( ) ; lib. Step1 ( ) ; if ( app. Step2 ( ) ) { lib. Step3 ( ) ; } for ( int i= 0 ; i< 4 ; i++ ) { app. Step4 ( ) ; } lib. Step5 ( ) ;
}
1.3 概括
2. 好的代码
2.1 lib.cpp
class Library {
public : void Run ( ) { Step1 ( ) ; if ( Step2 ( ) ) { Step3 ( ) ; } for ( int i= 0 ; i< 4 ; i++ ) { Step4 ( ) ; } Step5 ( ) ; } virtual ~ Library ( ) { } protected : void Step1 ( ) { } void Step3 ( ) { } void Step5 ( ) { } virtual bool Step2 ( ) = 0 ; virtual void Step4 ( ) = 0 ; } ;
2.2 app.cpp
class Application : public Library {
protected : virtual bool Step2 ( ) { } virtual void Step4 ( ) { }
} ; int main ( )
{ Library* pLib = new Application ( ) ; pLib-> Run ( ) ; delete pLib;
}
2.3 概括