Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Similar presentations


Presentation on theme: "Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr."— Presentation transcript:

1 Lab2 Diagrams Cop 4232 Fall 2005

2 Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr

3 Fall 2005Lab2 Cop42323 SimMgmt & SimModels SimMgmtSimModels Agent #InMgr& finMgr //class #OutMgr& foutMgr //class -string name //instance +Agent() +Agent( string ) +Agent(Tokenizer& ) +Agent(StringTokenizer& ) +Insert() +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +operator>>(ifstream&, Agent&) +operator string +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) #Put() #Get(Tokenizer&) #Get(StringTokenizer&) Customer -Money cash -string paymethod -CreditCard bankcard +Customer() +Customer( string, Money, string, CreditCard ) +Customer(Tokenizer& ) +Customer(StringTokenizer& ) +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt //computed -double purchasegal //computed -double pumprate -Grade gradeselected +PumpStation() +PumpStation( string, Money[], Money, double, Grade ) + PumpStation(Tokenizer& ) + PumpStation(StringTokenizer& ) +Extract() +Extract(Tokenizer&) +Extract(StringTokenizer&) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put()

4 Fall 2005Lab2 Cop42324 OO Data for Class Hierarchies X{ }X XData1: value1 XData2: value2 XData3: XDatak: valuek Y{ }Y YData1: value1 YData2: value2 Instance of X Embedded instance of Y INPUT FORMAT: Tokens delimited by whitespace characters – otherwise, free form – no margin requirements. OUTPUT FORMAT: Tokens delimited by whitespace characters on each side. Opening token and closing token have to align on the same character position, but on different lines. First data member follows the opening token on the same line. All other data members are output one per line with all labels aligned on the same margin position as the first data member.

5 Fall 2005Lab2 Cop42325 SimMgmt & SimModels Version #1 Agent -string name //instance +Agent() +Agent( ifstream& ) +Agent(Tokenizer& ) +Agent(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator string #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) Customer -Money cash -string paymethod -CreditCard bankcard PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Customer() +Customer( ifstream& ) +Customer(Tokenizer& ) +Customer(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) +PumpStation() +PumpStation( ifstream& ) +PumpStation(Tokenizer& ) +PumpStation(string ) +Insert( ostream& ) +Extract( ifstream& ) +Extract( Tokenizer& ) +Extract( string ) #Put( ostream& ) #Get( ifstream& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) InMgr and OutMgr are NOT USED! Design Principle: Provide a constructor, Extract, Insert, Get and Put methods for each external medium or boundary object to be supported by the class design. Input objects or sources: ifstream, Tokenizer, string Output objects or sinks: ostream, Also – we want to provide insertion and extraction operators on ostream and ifstream, respectively.

6 Fall 2005Lab2 Cop42326 Client A for Version #1 int main() { ifstream fin; ostream fout; string finName, foutName; cout << “Enter input file name: “; cin >> finName; cout << “Enter output file name: “; cin >> foutName; fin.open( finName.c_str()); fout.open( foutName.c_str()); Agent alpha, beta(fin), gamma; fin >> gamma; alpha.Extract(fin); fout << alpha << endl; beta.Insert(fout); fout << gamma << endl; } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output Style 1 Output Style 2 Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Nested objects cannot be properly indented due to lack of formatting control on ostream objects. Free form Partially aligned

7 Fall 2005Lab2 Cop42327 Client B for Version #1 int main() { Tokenizer T; //Tokenizer encapsulates input stream and // user dialog Agent alpha, beta(T), gamma(T); alpha.Extract(T); string Image = “ Agent{ name: DELTA }Agent “; Agent delta( Image ); //Hidden use of StringTokenizer fout << alpha << endl; beta.Insert(fout); fout << gamma << endl; fout << delta << endl; } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: DELTA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: DELTA }Agent Output Style 1 Output Style 2 Problem Formatting the output image is limited. To achieve the indented style, each instance must begin on a new line of the output stream. Nested objects still cannot be properly indented. or

8 Fall 2005Lab2 Cop42328 SimMgmt & SimModels Version #2 Agent -string name //instance +Agent() +Agent( InMgr& ) +Agent( Tokenizer& ) +Agent( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) +operator>>(ifstream&, Agent&) +operator string #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) Customer -Money cash -string paymethod -CreditCard bankcard PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected +Customer() +Customer( InMgr& ) +Customer( Tokenizer& ) +Customer( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) +PumpStation() +PumpStation( InMgr& ) +PumpStation( Tokenizer& ) +PumpStation( string ) +Insert( OutMgr& ) +Extract( InMgr& ) +Extract( Tokenizer& ) +Extract( string ) #Put( OutMgr& ) #Get( InMgr& ) #Get( Tokenizer& ) #Get( StringTokenizer& ) InMgr and OutMgr are USED! Analysis Output format requirements can be met only if Insert( OutMgr&) is used. Insertion operator << provides insufficient format control – no way to link ostream to OutMgr within the insertion operator. In fact, the insertion operator may use any output stream – not necessarily the one associated with OutMgr. Even if the ostream used by the insertion operator is the associated with OutMgr, independent use of endl will corrupt the state of OutMgr – must use newLine().

9 Fall 2005Lab2 Cop42329 Client A for Version #2 int main() { InMgr finMgr; ifstream& fin = finMgr.getStream(); OutMgr foutMgr; ostream& fout = foutMgr.getStream(); Agent alpha, beta(finMgr), gamma; fin >> gamma; alpha.Extract(finMgr); fout << alpha << endl; //<< cannot use foutMgr beta.Insert(foutMgr); //previous line compromises foutMgr fout << gamma << endl; //invalidates state of foutMgr } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output Style 1 Output Style 2Problem Use of insertion operators in client may corrupt or invalidate the state of OutMgr. The implementation of << cannot know the associated OutMgr (and there may not be one)! If the client inserts its own formatting operations (e.g. endl) then the state of the associated OutMgr becomes invalid and subsequent output may not be formatted correctly. To correct this, operator<< should not be defined as a friend. Defining operator>> is permitted as a friend. In fact, Extract() and Get() can be defined to take ifstream& as a parameter – it is not necessary to use InMgr& Output File

10 Fall 2005Lab2 Cop423210 SimMgmt & SimModels Agent #InMgr& finMgr //class #OutMgr& foutMgr //class -string name //instance +Agent() +Agent(Tokenizer& ) +Agent(string ) +Insert() //uses OutMgr +Extract() //uses InMgr +Extract(Tokenizer&) +Extract(string) +operator>>(ifstream&, Agent&) +operator string +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) #Put() //uses OutMgr #Get() //uses InMgr #Get(Tokenizer&) #Get(StringTokenizer&) Customer -Money cash -string paymethod -CreditCard bankcard +Customer() +Customer(Tokenizer& ) +Customer(string ) +Extract() +Extract(Tokenizer&) +Extract(string) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() PumpStation -Money pricepergal[3] -Money purchaselimit -Money purchaseamt -double purchasegal -double pumprate -Grade gradeselected + PumpStation() + PumpStation(Tokenizer& ) + PumpStation(string ) +Extract() +Extract(Tokenizer&) +Extract(string) +Insert() #Get() #Get(Tokenizer&) #Get(StringTokenizer&) #Put() Version #3 InMgr and OutMgr are Encapsulated by Agent!

11 Fall 2005Lab2 Cop423211 Client A for Version #3 int main() { InMgr& finMgr = Agent::InMgrOf(); //access class attributes OutMgr& foutMgr = Agent::OutMgrOf(); //access class attributes ifstream& fin = finMgr.getStream(); ostream& fout = foutMgr.getStream(); Agent alpha, beta, gamma; beta.Extract(); fin >> gamma; alpha.Extract(); fout << alpha; //verifies that fout is associated with foutMgr beta.Insert(); //uses foutMgr fout << gamma; //verifies that fout is associated with foutMgr foutMgr.newLine(); } Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Agent{ name: ALPHA }Agent Input File Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent Output File

12 Fall 2005Lab2 Cop423212 Hidden File Streams IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr Agent #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) CreditCard #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) Base class #InMgr& finMgr //class #OutMgr& foutMgr //class +InMgrOf()=>InMgr& +OutMgrOf()=>OutMgr& +setInMgr(InMgr&) +setOutMgr(OutMgr&) … Initialized at load time to pgmInMgr and pgmOutMgr Conclusion: If your design must satisfy both: (a) Use of insertion operator<< (b) Indented margins for nested objects Then each base class must provide a hidden OutMgr

13 Fall 2005Lab2 Cop423213 Lab2 Fall05 SimMgmt SimModels CustomerPumpStation JiffyStop Agent 1..* MoneyCreditCard IOMgmt InMgrOutMgr TokenErrorIOError AppError pgmInMgrpgmOutMgr StringTokenizerTokenizer

14 Fall 2005Lab2 Cop423214 Lab1 Figures A-1,2,3,4 Line Org Cur pos File Org idx Margin Stack MARGINSIZE Line Org Cur pos File Org Stack (before) Stack (after) idx Method: pushMargin() Line Org Cur pos File Org Stack (before) Stack (after) idx Method: popMargin() Cur fin File Org Method: deltaMargin(input) New fin (delta > 0) New fin (delta < 0) Line Org Curpos (before) File Org Method: newLine() Curpos (after) File Org Line Org

15 Fall 2005Lab2 Cop423215 Figures 3-6,7,8 Line Org Cur pos File Org Method: deltaMargin(output) Cur pos (delta > 0) diff File Org Cur pos (delta < 0) Line Org diff Cur fin File Org Method: toMargin(input) New fin idx


Download ppt "Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr."

Similar presentations


Ads by Google