270 likes | 380 Views
Comp 110/401 Composite Annotations. Instructor: Prasun Dewan. Prerequisite. Composite Objects Shapes. Interpreted How?. public interface RectangleWithPoint { public int getX (); public int getY (); public void getWidth (); public void getHeight ();
E N D
Comp 110/401Composite Annotations Instructor: PrasunDewan
Prerequisite • Composite Objects Shapes
Interpreted How? publicinterfaceRectangleWithPoint { publicintgetX(); publicintgetY(); publicvoidgetWidth(); publicvoidgetHeight(); public Point getPoint(); } Bad programming as code for defining Rectangle cannot be used in situations where rectangle without point is needed Object interpreted as either an atomic rectangle or an atomic point depending on whether Point or Rectangle in the name gets precedence
Reusable Design publicinterface Rectangle { publicintgetX(); publicintgetY(); publicvoidgetWidth(); publicvoidgetHeight(); } publicinterfaceRectangleWithPoint { public Rectangle getRectangle(); public Point getPoint(); }
ObjectEditor Graphics Rules publicinterfaceShuttleLocation { publicFancyCartesianPlanegetCartesianPlane(); publicImageLabelgetShuttleLabel(); publicintgetShuttleX(); publicvoidsetShuttleX(intnewVal); publicintgetShuttleY(); publicvoidsetShuttleY(intnewVal); } publicinterfaceNotAPoint { publicFancyCartesianPlane getCartesianPlane(); publicImageLabelgetShuttleLabel(); publicintgetX(); publicvoidsetX(intnewVal); publicintgetY(); publicvoidsetY(intnewVal); public Point getLocation(); } Type interpreted as a Point as its name contains “Point” and has X and Y Properties
IS_ATOMIC_SHAPE Class Annotation importutil.annotations.IsAtomicShape; @IsAtomicShape(false) //same as AShuttleLocation except interface name publicclassAShuttleLocationImplementingABadlyNamedInterfaceimplementsNotAPoint { Annotation is like a comment except it is typed and available at runtime IsAtomicShape(false) before class name says do not interpret the class as an atomic shape (it can be a composition)
ShuttleLocation Alternatives publicinterfaceShuttleLocation { publicFancyCartesianPlanegetCartesianPlane(); publicImageLabelgetShuttleLabel(); publicintgetShuttleX(); publicvoidsetShuttleX(intnewVal); publicintgetShuttleY(); publicvoidsetShuttleY(intnewVal); } publicinterfaceShuttleLocationWithPoint { publicFancyCartesianPlanegetCartesianPlane(); publicImageLabelgetShuttleLabel(); publicPoint getShuttleLocation(); publicvoidsetShuttleLocation(Point newVal); }
Point Property publicclassAShuttleLocationWithPointimplementsShuttleLocationWithPoint { publicPoint getShuttleLocation() { returnshuttleLocation; } … } Does not have X and Y properties, so not a point But location displayed as a point shape
IS_ATOMIC_SHAPE Annotation of Class Getter importutil.annotations.IsAtomicShape; publicclassAShuttleLocationWithPointimplementsShuttleLocationWithPoint { @IsAtomicShape(false) publicPoint getShuttleLocation() { returnshuttleLocation; } … } IsAtomicShape(false) before getter of a property means property not displayed as an atomic shape ShuttleLocation is not a textual property displayed in main panel What if we do not want it in main panel either?
Visible Annotation of Class Getter importutil.annotations.Visible; publicclassAShuttleLocationWithPointimplementsShuttleLocationWithPoint { @Visible(false) publicPoint getShuttleLocation() { returnshuttleLocation; } … } Visible(false) before getter of a property means property not displayed at all
Automatic Label Pattern publicclassAShuttleimplementsSpecificImageLabel { … }
Not Following Automating Label Pattern publicclassAShuttleimplements Shuttle { … }
Explicit Pattern Specification importutil.annotations.StructurePattern; importutil.annotations.StructurePatternNames; @StructurePattern(StructurePatternNames.LABEL_PATTERN) publicclassAShuttleimplements Shuttle{ … } Structure(<PatternName>) before class asserts that the class is following the pattern. ObjectEditor ignores class name and gives warnings if methods do not follow the pattern
Explicit Pattern Specification importutil.annotations.StructurePattern; importutil.annotations.StructurePatternNames; @StructurePattern(StructurePatternNames.LINE_PATTERN) publicclassAShuttleimplements Shuttle{ … }
Explicit Pattern Specification publicclassAShuttleLocationimplementsShuttleLocation { … }
Explicit Pattern Specification importutil.annotations.StructurePattern; importutil.annotations.StructurePatternNames; @StructurePattern(StructurePatternNames.LINE_PATTERN) publicclassAShuttleLocationimplementsShuttleLocation { … }
Explicit and Implicit Label Pattern importutil.annotations.StructurePattern; importutil.annotations.StructurePatternNames; @StructurePattern(StructurePatternNames.LABEL_PATTERN) publicclassAnotherShuttleimplementsSpecificImageLabel { staticfinal String IMAGE = "shuttle2.jpg“; staticfinalintWIDTH = 80; staticfinalintHEIGHT = 25; Point location; publicAShuttle (intinitX, intinitY) { location = newACartesianPoint(initX, initY); } publicAShuttle () { location = newACartesianPoint(50, 50); } public Point getLocation() {returnlocation;} publicvoidsetLocation(Point newVal) {location = newVal;} publicintgetWidth() { returnWIDTH;} publicintgetHeight() {returnHEIGHT;} public String getImageFileName() {returnIMAGE;} }
Explicit and Attempted Implicit Label Pattern importutil.annotations.StructurePattern; importutil.annotations.StructurePatternNames; @StructurePattern(StructurePatternNames.LABEL_PATTERN) publicclassAnotherShuttleimplementsSpecificImageLabel { staticfinal String IMAGE = "shuttle2.jpg“; staticfinalintWIDTH = 80; staticfinalintHEIGHT = 25; Point location; publicAShuttle (intinitX, intinitY) { location = newACartesianPoint(initX, initY); } publicAShuttle () { location = newACartesianPoint(50, 50); } public Point getLocation() {returnlocation;} publicvoidsetLocation(Point newVal) {location = newVal;} publicintgetWidth() { returnWIDTH;} publicintgetHeight() {returnHEIGHT;} //public String getImageFileName() {return IMAGE;} }
BMI Calculator Pattern? publicclassABMICalculator { publicdoublecalculateBMI(double height, double weight) { returnweight/(height*height); } }
BMI Calculator Pattern? @StructurePattern(StructurePatternNames.NO_PATTERN) publicclassAnAnnotatedBMICalculator { publicdoublecalculateBMI(double height, double weight) { returnweight/(height*height); } }
BMI Calculator Pattern? @StructurePattern(StructurePatternNames.BEAN_PATTERN) publicclassAnAnnotatedBMICalculator { publicdoublecalculateBMI(double height, double weight) { returnweight/(height*height); } }
Bean Pattern? @StructurePattern(StructurePatternNames.BEAN_PATTERN) publicclassABMISpreadsheetNotFollowingBeanConventions { doubleheight = 1.77; doubleweight = 75; publicdoublegetWeight() { returnweight; } publicvoidset(doublenewWeight, doublenewHeight) { weight= newWeight; height= newHeight; } publicdoublegetHeight() { returnheight; } publicvoidsetHeight(intnewHeight) { height= newHeight; } publicdoubleBMI() { returnweight/(height*height); } }
(Editable) Property Name Annotations @StructurePattern(StructurePatternNames.BEAN_PATTERN) @PropertyNames({ "Height", "Weight", "BMI"}) @EditablePropertyNames({"Height", "Weight"}) publicclassABMISpreadsheetNotFollowingBeanConventions { doubleheight = 1.77; doubleweight = 75; publicdoublegetWeight() { returnweight; } publicvoidset(doublenewWeight, doublenewHeight) { weight= newWeight; height= newHeight; } publicdoublegetHeight() { returnheight; } publicvoidsetHeight(intnewHeight) { height= newHeight; } publicdoubleBMI() { returnweight/(height*height); } }
Order of Properties @StructurePattern(StructurePatternNames.BEAN_PATTERN) @PropertyNames({ "Weight", "Height", "BMI"}) @EditablePropertyNames({"Height", "Weight"}) publicclassABMISpreadsheetNotFollowingBeanConventions { doubleheight = 1.77; doubleweight = 75; publicdoublegetWeight() { returnweight; } publicvoidset(doublenewWeight, doublenewHeight) { weight= newWeight; height= newHeight; } publicdoublegetHeight() { returnheight; } publicvoidsetHeight(intnewHeight) { height= newHeight; } publicdoubleBMI() { returnweight/(height*height); } }
Bean Pattern? @StructurePattern(StructurePatternNames.BEAN_PATTERN) publicclassABMISpreadsheetNotFollowingBeanConventions { doubleheight = 1.77; doubleweight = 75; publicdoublegetWeight() { returnweight; } publicvoidset(doublenewWeight, doublenewHeight) { weight= newWeight; height= newHeight; } publicdoublegetHeight() { returnheight; } publicvoidsetHeight(intnewHeight) { height= newHeight; } publicdoubleBMI() { returnweight/(height*height); } } Warning if (editable) properties not declared? Overhead, chances of mistake low, C# has built in support for properties Why warning if no structure annotation?
Why Warnings • Accidental patterns • Efficiency
Large Search Space publicclassStructurePatternNames { publicstaticfinal String NO_PATTERN = "No Pattern"; publicstaticfinal String BEAN_PATTERN = "Bean Pattern"; publicstaticfinal String POINT_PATTERN = "Point Pattern"; publicstaticfinal String LINE_PATTERN = "Line Pattern"; publicstaticfinal String OVAL_PATTERN = "Oval Pattern"; publicstaticfinal String RECTANGLE_PATTERN = "Rectangle Pattern"; publicstaticfinal String ARC_PATTERN = "Arc Pattern"; publicstaticfinal String LABEL_PATTERN = "Label Pattern"; publicstaticfinal String TEXT_PATTERN = "Text Pattern"; publicstaticfinal String STRING_PATTERN = "String Pattern"; publicstaticfinal String CURVE_PATTERN = "Curve Pattern"; publicstaticfinal String VECTOR_PATTERN = "Vector Pattern"; publicstaticfinal String LIST_PATTERN = "List Pattern"; publicstaticfinal String STACK_PATTERN = "Stack Pattern"; publicstaticfinal String HASHTABLE_PATTERN = "Hashtable Pattern"; publicstaticfinal String MAP_PATTERN = "Hashmap Pattern"; publicstaticfinal String ENUM_PATTERN = "Enum Pattern"; … } • Accidental patterns • Efficiency