JDBC program • import java.sql.*; • class JdbcTest { • public static void main (String args []) { • // Load JDBCODBCDriver • Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); • // Connect to the local database • Connection conn = DriverManager.getConnection (\"jdbc:odbc:EMPDSN\",\"sa\", ―niit011p\"); Slide 151 of 92 Copyright © 2020
Contd… // Query the student names Statement stmt = conn.createStatement (); ResultSet rset = stmt.executeQuery (\"SELECT * FROM emp\"); // Print the name out //name is the 2nd attribute of Student while (rset.next ()) System.out.println (rset.getString (2)); //close the result set, statement, and the connection rset.close(); stmt.close(); conn.close(); }} Slide 152 of 92 Copyright © 2020
Statement type • Statement • PreparedStatement • CallableStatement Slide 153 of 92 Copyright © 2020
ResultSet • ResultSet • ResultSetMetaData Slide 154 of 92 Copyright © 2020
PreparedStatement with Select PreparedStatement ps = conn.prepareStatement( “Select * from emp where city=? And ctc>?\"); ps.setInt(2, 500000); ps.setString(1, “Bangalore”); ResultSet rs = ps.executeQuery(); Slide 155 of 92 Copyright © 2020
DML • Insert • INSERT INTO table (col1, col2, …..) VALUES (val1, val2, …..) • Update • UPDATE table SET col1=val1, col2=val2 WHERE col=val • Delete • DELETE FROM table WHERE col=val Slide 156 of 92 Copyright © 2020
Update using PreparedStatement PreparedStatement ps = conn.prepareStatement( \"UPDATE empDetails SET city= ? WHERE name = ?\"); ps.setString(1, “Bangalore”); ps.setString(2, “Harish”); ps.executeUpdate(); Slide 157 of 92 Copyright © 2020
Using Transaction • When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. conn.setAutoCommit(false); .... transaction ... con.commit(); con.setAutoCommit(true); Slide 158 of 92 Copyright © 2020
Using Transaction con.setAutoCommit(false); PreparedStatement updateCity = con.prepareStatement( \"UPDATE empDetails SET City = ? WHERE name LIKE ?\"); updateCity.setString(1, “Bangalore”); updateCity.setString(2, “Harish”); int res = updateCity.executeUpdate(); Copyright © 2020 If(res == 0) con.rollback(); else con.commit(); con.setAutoCommit(true); Slide 159 of 92
java.sql package Interface Name Description CallableStatement This contains methods that are used for execution of SQL stored procedures. Connection This is used to maintain and monitor database sessions. Data access can also be controlled using the transaction locking mechanism. DatabaseMetaData This interface provides database information such as the version number, names of the tables and functions supported. Driver This interface is used to create Connection objects. PreparedStatement This is used to execute pre-compiled SQL statements. ResultSet This interface provides methods for retrieving data returned by an SQL statement. ResultSetMetaData This interface is used to collect the meta data information associated with the last ResultSet object. Statement It is used to execute SQL statements and retrieve data into the RSlidee s16u0 olft9S2 et. Copyright © 2020
An Introduction to Design Patterns Douglas C. Schmidt Vanderbilt University Slide 161 of 92 161 Copyright © 2020
Overview Part I: Motivation & Concept • the issue • what design patterns are • what they‘re good for • how we develop & categorize them Slide 162 of 92 162 Copyright © 2020
Overview (cont‘d) Part II: Application • use patterns to design a document editor • demonstrate usage & benefits Part III: Wrap-Up • observations, caveats, & conclusion Slide 163 of 92 163 Copyright © 2020
Part I: Motivation & Concept OOD methods emphasize design notations Fine for specification, documentation But OOD is more than just drawing diagrams Good draftsmen good designers Good OO designers rely on lots of experience At least as important as syntax Most powerful reuse is design reuse 164 Slide 164 of 92 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Recurring Design Structures OO systems exhibit recurring structures that promote • abstraction • flexibility • modularity • elegance TPhroebreleinmli:es valuable design knowledge capturing, communicating, & applying this knowledge Slide 165 of 92 165 Copyright © 2020
Part I: Motivation & Concept (cont‘d) A Design Pattern… • abstracts a recurring design structure • comprises class and/or object dependencies structures interactions conventions • names & specifies the design structure explicitly • distills design experience 166 Slide 166 of 92 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Four Basic Parts 1. Name 2. Problem (including ―forces‖) 3. Solution 4. Consequences & trade-offs of application Language- & implementation-independent A “micro-architecture” Adjunct to existing methodologies (RUP, Fusion, SCRUM, etc.) 167 Slide 167 of 92 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Example: OBSERVER Slide 168 of 92 168 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Goals Codify good design • distill & generalize experience • aid to novices & experts alike Give design structures explicit names • common vocabulary • reduced complexity • greater expressiveness Capture & preserve design information • articulate design decisions succinctly 169 • improve documentation Copyright © 2020 Slide 169 of 92
Part I: Motivation & Concept (cont‘d) Design Space for GoF Patterns Scope: domain over which a pattern applies Purpose: reflects what a pattern does Slide 170 of 92 170 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Design Pattern Template (1st half) NAME scope purpose Intent short description of the pattern & its purpose Also Known As Any aliases this pattern is known by Motivation motivating scenario demonstrating pattern‘s use Applicability circumstances in which pattern applies Structure graphical representation of the pattern using modified UML notation Participants 171 participating classes and/orSloidbe 1je71cotfs92& their responsibilities Copyright © 2020
Part I: Motivation & Concept (cont‘d) Design Pattern Template (2nd half) ... Collaborations how participants cooperate to carry out their responsibilities Consequences the results of application, benefits, liabilities Implementation pitfalls, hints, techniques, plus language-dependent issues Sample Code sample implementations in C++, Java, C#, Smalltalk, C, etc. Known Uses examples drawn from existing systems Related Patterns discussion of other patterns that relate to this one Slide 172 of 92 172 Copyright © 2020
Part I: Motivation & Concept (cont‘d) Modified UML/OMT Notation Slide 173 of 92 173 Copyright © 2020
Motivation & Concept (cont‘d) object behavioral OBSERVER Intent define a one-to-many dependency between objects so that when one object changes state, all dependents are notified & updated Applicability • an abstraction has two aspects, one dependent on the other • a change to one object requires changing untold others • an object should notify unknown other objects Structure Slide 174 of 92 174 Copyright © 2020
Motivation & Concept (cont‘d) object behavioral OBSERVER class ProxyPushConsumer : public // … virtual void push (const CORBA::Any &event) { for (std::vector<PushConsumer>::iterator i (consumers.begin ()); i != consumers.end (); i++) (*i).push (event); } class MyPushConsumer : public // …. virtual void push (const CORBA::Any &event) { /* consume the event. */ } CORBA Notification Service example using C++ Standard Template Library (STL) iterators (which is an example of the Iterator pattern from GoF) Slide 175 of 92 175 Copyright © 2020
Motivation & Concept (cont‘d) object behavioral OBSERVER (cont‘d) Consequences 176 + modularity: subject & observers may vary independently + extensibility: can define & add any number of observers Copyright © 2020 + customizability: different observers offer different views of subject – unexpected updates: observers don‘t know about each other – update overhead: might need hints or filtering Implementation • subject-observer mapping • dangling references • update protocols: the push & pull models • registering modifications of interest explicitly Known Uses • Smalltalk Model-View-Controller (MVC) • InterViews (Subjects & Views, Observer/Observable) • Andrew (Data Objects & Views) • Pub/sub middleware (e.g., CORBA Notification Service, Java Messaging Service) • Mailing lists Slide 176 of 92
Part I: Motivation & Concept (cont‘d) • DesiBgnenreeusfeits of Patterns • Uniform design vocabulary • Enhance understanding, restructuring, & team communication • Basis for automation • Transcends language-centric biases/myopia • Abstracts away from many unimportant details Slide 177 of 92 177 Copyright © 2020
Part I: Motivation & Concept (cont‘d) • ReqLuiiraebsiiglintiifiecasntotfedPioautst&erenrrsor-prone human effort to handcraft pattern implementations design reuse • Can be deceptively simple uniform design vocabulary • May limit design options • Leaves some important details unresolved Slide 178 of 92 178 Copyright © 2020
Part II: Application: Document Editor (Lexi) 7 Design Problems 1. Document structure 2. Formatting 3. Embellishment 4. Multiple look & feels 5. Multiple window systems 6. User operations 7. Spelling checking & hyphenation 179 Note that none of these paSttlideer1n7s9 oaf 9r2e restricted to documeConptyrieghdt ©it2o0r2s0 …
Document Structure Goals: • present document‘s visual aspects • drawing, hit detection, alignment • support physical structure (e.g., lines, columns) Constraints/forces: 180 • treat text & graphics uniformly Copyright © 2020 • no distinction between one & many Slide 180 of 92
Document Structure (cont‘d) Solution: Recursive Composition Slide 181 of 92 181 Copyright © 2020
Document Structure (cont‘d) Object Structure Slide 182 of 92 182 Copyright © 2020
Document Structure (cont‘d) Glyph Base class for composable graphical objects Basic interface: Task Operations appearance void draw(Window) hit detection boolean intersects(Coord, Coord) structure void insert(Glyph) void remove(Glyph) Glyph child(int n) Glyph parent() Subclasses: Character, Image, Space, Row, Column 183 Slide 183 of 92 Copyright © 2020
Document Structure (cont‘d) Glyph Hierarchy Note the inherent recursion in this hierarchy i.e., a Row is a Glyph & a Row also has Glyphs! Slide 184 of 92 184 Copyright © 2020
Document Structure (cont‘d) object structural COMPOSITE Intent treat individual objects & multiple, recursively-composed objects uniformly Applicability objects must be composed recursively, and no distinction between individual & composed elements, and objects in structure can be treated uniformly Structure e.g., Glyph e.g., Character, e.g., Row, Rectangle, etc. Column Slide 185 of 92 185 Copyright © 2020
Document Structure (cont‘d) object structural COMPOSITE Leaf Node Composite Node CORBA Naming Service example using CosNaming::BindingIterator (which is an example of the “Batch Iterator” pattern compound from POSA5) Slide 186 of 92 186 Copyright © 2020
Document Structure (cont‘d) object structural COMPOSITE class Glyph { Component public: virtual void draw (const Drawing_Region &) = 0; // ... protected: int x_, y_; // Coordinate position. Composite }; class Character : public Glyph { class Row : public Glyph { public: public: Character Row (std::vector<Glyph*> children); (const std::string &name); // ... virtual void // ... virtual void draw (const Drawing_Region &c){ for (std::vector<Glyph*>::iterator draw (const Drawing_Region &c) i (children_); { c.draw_text (x_, y_, name_); } i != children_.end (); private: i++) std::string name_; (*i)->draw (c); }; } Leaf // ... private: std::vector<Glyph*> children_; // ... }; 187 Slide 187 of 92 Copyright © 2020
Document Structure (cont‘d) object structural COMPOSITE void list_context (CosNaming::NamingContext_ptr nc) { CosNaming::BindingIterator_var it; // Iterator reference CosNaming::BindingList_var bl; // Binding list const CORBA::ULong CHUNK = 100; // Chunk size nc->list (CHUNK, bl, it); // Get first chunk show_chunk (bl, nc); // Print first chunk if (!CORBA::is_nil(it)) { // More bindings? // Get next chunk while (it->next_n(CHUNK, bl)) // Print chunk show_chunk (bl, nc); // Clean up it->destroy(); } } void show_chunk (const CosNaming::BindingList_ptr &bl, // Helper function CosNaming::NamingContext_ptr nc) { for (CORBA::ULong i = 0; i < bl.length (); ++i) { cout << bl[i].binding_name[0].id << \".\" << bl[i].binding_name[0].kind; if (bl[i].binding_type == CosNaming::ncontext) { 188 Handle cout << \": context\" << endl; Composite CORBA::Object_var obj = nc->resolve (bl[i].binding_name); Copyright © 2020 list_context (CosNaming::NamingContext::_narrow (obj)); Node } Handle Leaf else cout << \": reference\" << endl; Node } } Slide 188 of 92
Document Structure (cont‘d) object structural COMPOSITE (cont‘d) Consequences + uniformity: treat components the same regardless of complexity + extensibility: new Component subclasses work wherever old ones do – overhead: might need prohibitive numbers of objects Implementation • do Components know their parents? • uniform interface for both leaves & composites? • don‘t allocate storage for children in Component base class • responsibility for deleting children Known Uses • ET++ Vobjects • InterViews Glyphs, Styles • Unidraw Components, MacroCommands • Directory structures on UNIX & Windows • Naming Contexts in CORBA Slide 189 of 92 189 • MIME types in SOAP Copyright © 2020
Formatting Goals: • automatic linebreaking, justification Constraints/forces: • support multiple linebreaking algorithms • don‘t tightly couple these algorithms with the document structure Slide 190 of 92 190 Copyright © 2020
Formatting (cont‘d) CompSoosluittoiorn: Encapsulate Linebreaking • baSsteractlaesgsyabstracts linebreaking algorithm • subclasses for specialized algorithms, e.g., SimpleCompositor, TeXCompositor Composition • composite glyph (typically representing a column) • supplied a compositor & leaf glyphs • creates row-column structure as directed by compositor Slide 191 of 92 191 Copyright © 2020
Formatting (cont‘d) New Object Structure Generated in accordance with compositor strategies & do not affect contents of leaf glyphs Slide 192 of 92 192 Copyright © 2020
Formatting (cont‘d) object behavioral STRATEGY Intent define a family of algorithms, encapsulate each one, & make them interchangeable to let clients & algorithms vary independently Applicability Strucwtuhreen an object should be configurable with one of many algorithms, and all algorithms can be encapsulated, and one interface covers all encapsulatio1n9s3 Slide 193 of 92 Copyright © 2020
Formatting (cont‘d) object behavioral STRATEGY class Composition : public Glyph { public: void perform_composition (const Compositor &compositor, const std::vector<Glyphs*> &leaf_glyphs) { compositor.set_context (*this); Simple for (std::vector<Glyph*>::iterator algorithm i (leaf_glyphs); creates row- i != leaf_glyphs.end (); class SimpleCompositor column i++) { : public Compositor { structure as this->insert (*i); public: directed by compositor.compose (); virtual void compose () compositor} { /* ... */} } }; Complex }; algorithm class Compositor { class TexCompositor public: : public Compositor { void set_context public: (Composition &context); virtual void compose () { /* ... */} virtual void compose () = 0; }; 194 // ... Slide 194 of 92 Copyright © 2020
Formatting (cont‘d) object behavioral STRATEGY Hook for the event Hook for demuxing strategy the request Hook for demuxing marshaling strategy strategy Hook for the Hook for the concurrency connection strategy management strategy Hook for the underlying transport strategy 195 Strategy can also be appliedSliidne 19d5iosf 9t2ributed systems (e.gCo.p,yrmightid© 2d02l0eware)
Formatting (cont‘d) object behavioral STRATEGY (cont‘d) Consequences + greater flexibility, reuse + can change algorithms dynamically – strategy creation & communication overhead – inflexible Strategy interface – semantic incompatibility of multiple strategies used together Implementation • exchanging information between a Strategy & its context • static strategy selection via parameterized types Known Uses • InterViews text formatting • RTL register allocation & scheduling strategies • ET++SwapsManager calculation engines • The ACE ORB (TAO) Real-time CORBA middleware See Also 196 Slide 196 of 92 Copyright © 2020
Formatting (cont‘d) class behavioral Template Method (cont‘d) Intent • Provide a skeleton of an algorithm in a method, deferring some steps to subclasses class Composition : public Glyph { public: // Template Method. void perform_composition (const std::vector<Glyphs*> &leaf_glyphs) { set_context (*this); for (std::vector<Glyph*>::iterator i (leaf_glyphs); i != leaf_glyphs.end (); i++) { insert (*i); compose (); } } virtual void compose () = 0; // Hook Method. }; class Simple_Composition : public Composition { virtual void compose () { /* ... */ } }; class Tex_Composition : public Composition { 197 virtual void compose () { S/li*de 1.9.7 .of 92*/ } Copyright © 2020
Formatting (cont‘d) class behavioral Template Method (cont‘d) Intent • Provide a skeleton of an algorithm in a method, deferring some steps to subclasses class Base_Class { 198 public: Copyright © 2020 // Template Method. void template_method (void) { hook_method_1 (); hook_method_2 (); // ... } virtual void hook_method_1 () = 0; virtual void hook_method_2 () = 0; }; class Derived_Class_1 : public Base_Class { virtual void hook_method_2 () { /* ... */ } }; class Derived_Class_2 : public Base_Class { virtual void hook_method_1 () { /* ... */ } virtual void hook_method_2 () { /* ... */ } }; Slide 198 of 92
Embellishment Goals: • add a frame around text composition • add scrolling capability Constraints/forces: • embellishments should be reusable without subclassing, i.e., so they can be added dynamically at runtime • should go unnoticed by clients 199 Slide 199 of 92 Copyright © 2020
Embellishment (cont‘d) MonSooglulytpiohn: ―Transparent‖ Enclosure • base class for glyphs having one child • operations on MonoGlyph (ultimately) pass through to child MonoGlyph subclasses: • Frame: adds a border of specified width • Scroller: scrolls/clips child, adds scrollbars Slide 200 of 92 200 Copyright © 2020
Search
Read the Text Version
- 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
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355