Strategy

Using BOUML – Day 1

Design Patterns: Strategy Pattern

Using Head First Design Patterns as our textbook.

Today I will go through Chapter 1 Welcome to Design Patterns using the BOUML tool and Eclipse IDE …

Steps

  • Go To  BOUML and download and install BOUML
  • Just accept the defaults and press OK.
  • Under Languages select Java
  • Project/New Project – create a new project called DesignPatterns
  • Create a package called SimUDuck.
  • Right Click SimUDuck and select Create a Class View – call it DuckClassView
  • Right Click DuckClassView / Create Class Diagram — call it DuckClassDiagram
  • Double click DuckClassDiagram
  • Click the Class icon and create three classes Duck, MallardDuck, and RedHeadDuck
  • Using the Generalization(Inheritance) icon connect MallardDuck to Duck and RedHeadDuck to Duck
  • Right click Add Operation – add quack, swim, and display operations to Duck, making the display operation abstract.
  • Add the display operation to MallardDuck and RedHeadDuck classes
  • Add fly operation to Duck
  • Create a new class called RubberDuck
  • etc. until we create the class diagram in page 6
  • Right Click SimUDuck and create Deployment View – called SimUDuckDeploymentView
  • Double Click on the ClassView folder and in the drop down for deployment view select SimUDuckDeploymentView
  • Double Click the package folder and in the Java tab update the path where you want the generated Java to be stored.
  • Right click on the Duck class in the browser – the tree view – Select Source Artifact.
  • In the Deployment View there will now be an artifact <<source>> Duck
  • Right click/edit on the artifact and select the Associated Classes tab and add the rest of the classes to this artifact. One generated source file will now have all the code for these classes.
  • Generate the Java code – Control J
  • In Eclipse create a new project. Select Java Project. Call the Project SimUDuck. Select Create Project from Existing Source. Point the source code for the project to the same folder that you used in BOUML to generate the Java code. You can specify the order the classes appear in the source file on the right side pane.
  • Keep enhancing/refactoring model as per book. Alternate between BOUML and Eclipse where you will be prompted that the code has changed on the file system and you can reload it and make sure there are no compile errors and then execute the Java Application to see the results.
  • I elected to code MiniDuckSimulator only in Eclipse as a separate source file.

What I Learnt

The obvious: Abstract out common behavior/code – limit duplication.

The subtle: Prefer interfaces to classes, and abstract classes to concrete classes.Classes do not have to be nouns or things, sometimes they can qualities or just plain behavior. The less we know about another object the better – code on a need-to-know basis – do you need to know that an object is of a specific class or just that it is capable of something? Would it be sufficient to know that it was within the same superclass and so on up until you reach the Object root class? Additionally, does the class need to know how an operation is implemented – does it need to contain the implementation logic?

Behaviors of objects may be suitable candidates for their own class even though the instances of such classes are not objects in our real or imagined world.

We delegate behavior to other classes to keep our class simple where that behavior may be of use elsewhere potentially or where the behavior is subject to change or evolution.

The strategy pattern is vaguely similar to mixins in Ruby – the distinction typifies why I prefer Ruby to Java: in Ruby you just include BehaviorX and you are done. That is not to say that this pattern does not also have applicability to Ruby.

Conventions

Interface Naming: if an interface does not have abstracted realizations name the class …able otherwise name it …Behavior

Operation Naming: Delegation of behavior – where common logic is abstracted out to a new class (hierarchy) then call the operation performXxx

Convention: Where behavior is delegated then the original class will need a reference to the class instance that is performing the implementation – name this attribute xxxBehavior where the delegated class type is XxxBehavior.

Coding Convention: When referencing another object, use the most abstract class type possible, prefer an interface to a class. With x=Y.new() declare x to be an interface or most abstract class possible in the class hierarchy of Y.

BOUML Q&A

How/Where is the code generated? You need a deployment view. Right Click/edit this view and add a directory and package for code generation. You also need to have one or more artifacts – for example an output source file(s). After creating the deployment view select a class in your class view, Right Click/edit, and select the deployment view. Then within the deployment view you will see <<source>> YourClass. If multiple classes are to be contained within a single source file you can edit the new artifact in the deployment view and associate additional classes. Then when you generate you will have all the code generated in that file. The paths to the local filesystem where the output is generated is available through Right Click/edit the package in the browser and click on the Java tab.

No return types in generated code: In the Uml tab you need to set the value type: field – WL default to void.

The value type for a abstract operation is not inherited: this is only true if you change the value type in the superclass after you have added the inherited operation to the subclass. (WL)

Added UML operation parameters not in Java: In the Java tab click on the Default Definition.

Where can you place constructor logic: Add an operation with the name of the class and no value type specified.

Cursor always changes back to pointer arrow: Say you want to add a bunch of classes. It would be nicer to keep the class icon selected and not have it deselected after each new class is added. It does not look like you can do this. WL

Re-Add inherited operation after changing parameters:If you change/add a parameter to an operation that you have added to a subclass or realizing class then you must either update each corresponding operation (manually) ro perform the add inherited operation again. Unfortunately, if you perform the latter you will get a duplicate operation and will need to moreve the first operation from the model. WL

Double click parameters.Name in Operation dialog:If you do not double click your edist then you will continue typing the the operation name box instead Bug

Operation Dialog parameters.Type:Pressing tab or Enter does not do what you want- must use mouse.

Browser – deleting an operation or relationship:Delete or Backspace does not delete the entity from the model you must Right-Click/Delete

X: y

BOUML Tips

#1 double click on a class for the properties

#2 Miscellaneous/Edit Shortcuts – you can add a short cut for example to add a new class I inserted a shortcut to have shift Enter as new class operation – this did not do what I hoped it works when you are in the browser in the left hand side, and then you need to reselect the view to prevent creating a subclass.

Create a website or blog at WordPress.com

Up ↑