UML - Class Diagrams ==================== 1) How to model OO projects? 1.1) What is the notation used? * UML came out in the mid ninities, and it was the unificatin of three different design approaches: Rumbaugh's, Booch's and Jacobson's. These three designers were working at Rational Software Corporation. 2) What do we know about UML? - Based on diagrams - Targets OO software development - Very graphical - Some diagrams represent static info, others represent dynamic info 3) There are criticisms to UML, in particular: - It is big. - There are redundancies. - Some diagrams are never used in practice. - There are many tools, and they vary a bit in notation. * Class diagrams describe the way in which objects are related in a system. - Interfaces are represented as boxes, with methods, and the stereotype <>: ----------------- | <> | | Shape | |---------------| | + draw() | |---------------| - Method can have different visibilities: + public, # protected, - private, ~ package - Classes are represented as boxes, with attributes and methods: - Slide 2 - slides07_CD.pdf --------------- | Class | |-------------| | Attribute | |-------------| | Operation() | |-------------| * Classes may implement interfaces, e.g, Rectangle implements Shape Use the dashed line with the triangle for this. 4) There are two basic ways in which an object can use implementation from another. Which are these ways? - Inhreritance - Association - Aggreagation - Composition - Slide 3 - slides07_CD.pdf * Classes may also extend other classes. 5) Can you give examples from the applet lesson? Square extends Rectangle - Slide 5 - slides07_CD.pdf Association: Pessoa assina Revista Revista eh lida por Pessoa Associations may have qualifiers: - Name - multiplicity A - role A - multiplicity B - role B * There are 7 often used multiplicities: 0..1, The applet contains zero lists of shapes (before init) or one (after). 1, Each circle must contain one center point 0..*, The list of shapes contain zero or more shapes. 1..*, The list of command line arguments must contain at least one argument. n, The square contains two points. 0..n, The bounded array contains up to n elements. 1..n The list of file descriptors must contain at least one, up to the maximum number of files that can be simultaneously open. Associations may also have directionality. 6) What is the meaning of this directionality? 6.1) Can anyone give an example? Drawing contains one or more shapes. * There is a special type of association called Aggregation. It is denoted by adding a while diamond in one side of the link. It means that an object 'has' another. 7) One example, any one? - Slide 4 - slides07_CD.pdf * We will not be using aggregation in this course, because I think it is very redundant with association. * There is a strong type of aggregation called composition. In a composition the owned object is part of the life cycle of the owner. 8) How to implement this relation in C++? #include class ObjAggr { public: ObjAggr() {std::cout << "Creating objAggr\n";} ~ObjAggr() {std::cout << "Deleting objAggr\n";} int p; }; class Obj1 { public: Obj1() {std::cout << "Creating obj1\n";} ~Obj1() {std::cout << "Deleting obj1\n";} ObjAggr o; }; class Obj2 { public: Obj2() {o = new ObjAggr(); std::cout << "Creating obj2\n";} ~Obj2() {std::cout << "Deleting obj2\n";delete o;} ObjAggr* o; }; // 8.1) How to change this implementation, so that the objects become related // by an association instead of a composition? class Obj3 { public: Obj3() {o = new ObjAggr(); std::cout << "Creating obj3\n";} ~Obj3() {std::cout << "Deleting obj3\n";} ObjAggr* o; }; int main() { // Composition 1: Obj1 *o1 = new Obj1(); delete o1; // Composition 2: Obj2 *o2 = new Obj2(); delete o2; // Association: Obj3 *o3 = new Obj3(); delete o3; } 9) How to distinguish associations from compositions in Java? 10) What is the relation between Graphics and Driver in our applet example? public void paint(Graphics g) {...} * We can add notes to UML diagrams. They look like little pieces of paper. 11) How to implement the state machine with two states: locked and unlocked, and two events: coin and pass? 11.1) Which test this machine should pass? public final void testZero() throws Exception { TurnstileUserInterface ts = new TurnstileUserImpl(); assertEquals(ts.isLocked(), true); ts.coin(); assertEquals(ts.isLocked(), false); ts.coin(); assertEquals(ts.isLocked(), false); ts.pass(); assertEquals(ts.isLocked(), true); ts.pass(); assertEquals(ts.isLocked(), true); } 11.1) What is public to the user of our system? Which operations this user should be allowed to see? Remember: "The shoemaker should not see beyond the shoes." public interface TurnstileUserInterface { void coin(); void pass(); boolean isLocked(); } 12) What is a turnstile? Which actions it can execute? public interface TurnstileMachineInterface { void lock(); void unlock(); void alarm(); void thankyou(); } 12.1) Should we make these actions public or private? Should we have an interface turnstile at all? 13) Can you provide a simple implementation to this interface? public class TurnstileMachineImpl implements TurnstileMachineInterface { public final void lock() { System.out.println("Locking turnstile."); } public final void unlock() { System.out.println("Unlocking turnstile"); } public final void alarm() { System.out.println("Sounding alarm"); } public final void thankyou() { System.out.println("Thank you, very much!"); } } 14) How would the implementation of this interface look like? public class TurnstileUserImpl implements TurnstileUserInterface { private interface State { State coin(TurnstileMachineInterface m); State pass(TurnstileMachineInterface m); } private class LockedSt implements State { public State coin(final TurnstileMachineInterface m) { m.unlock(); return new UnlockedSt(); } public State pass(final TurnstileMachineInterface m) { m.alarm(); return new LockedSt(); } } private class UnlockedSt implements State { public State coin(final TurnstileMachineInterface m) { m.thankyou(); return new UnlockedSt(); } public State pass(final TurnstileMachineInterface m) { m.lock(); return new LockedSt(); } } private TurnstileMachineInterface turnstile; private State s; public TurnstileUserImpl() { turnstile = new TurnstileMachineImpl(); s = new LockedSt(); } public final void coin() { s = s.coin(turnstile); } public final void pass() { s = s.pass(turnstile); } public final boolean isLocked() { return s instanceof LockedSt; } } 15) Assume that the creation and destruction of objects is very expensive. How to avoid creating and deleting objects all the time? private static class LockedSt implements State { private static final LockedSt instance = new LockedSt(); public static State createInstance() { return instance; } public State coin(final TurnstileMachineInterface m) { m.unlock(); return UnlockedSt.createInstance(); } public State pass(final TurnstileMachineInterface m) { m.alarm(); return LockedSt.createInstance(); } } private static class UnlockedSt implements State { private static final UnlockedSt instance = new UnlockedSt(); public static State createInstance() { return instance; } public State coin(final TurnstileMachineInterface m) { m.thankyou(); return UnlockedSt.createInstance(); } public State pass(final TurnstileMachineInterface m) { m.lock(); return LockedSt.createInstance(); } } 16) Assume that if a client tries to pass the turnstile withou inserting the coin, then you go to a state Fined, that requires two coins, instead of one, to liberate passage. public static class FinedSt extends LockedSt { private int counter = 0; private static FinedSt instance = new FinedSt(); public static State createInstance() { return instance; } private FinedSt() {} public State coin(final TurnstileMachineInterface m) { if (counter > 0) { counter = 0; m.thankyou(); return UnlockedSt.createInstance(); } else { counter++; m.thankyou(); return FinedSt.createInstance(); } } public State pass(final TurnstileMachineInterface m) { m.alarm(); return FinedSt.createInstance(); } } 17) What would be a good test for this program? public final void testZero() throws Exception { TurnstileUserInterface ts = new TurnstileUserImpl(); assertEquals(ts.isLocked(), true); ts.coin(); assertEquals(ts.isLocked(), false); ts.coin(); assertEquals(ts.isLocked(), false); ts.pass(); assertEquals(ts.isLocked(), true); ts.pass(); assertEquals(ts.isLocked(), true); ts.coin(); assertEquals(ts.isLocked(), true); ts.coin(); assertEquals(ts.isLocked(), false); } 18) How to compile this stuff? javac state/*.java java state.MyTestTs