Question? Leave a message!

Implementing a model

Implementing a model
Dr.MasonHanks Profile Pic
Published Date:23-07-2017
Website URL
Object Oriented Software Development 7. Implementing a model www.ThesisScientist.comUML models  UML may be used to visualise, specify, construct and document the artefacts of a software system  Part of a software development method  To produce a working application the artefacts in the model must be implemented using development tools, languages and frameworks www.ThesisScientist.comImplementing a model  We will use Visual Studio, C and the .NET framework to begin the implementation of a UML model  Based on examples from Software Modelling Analysis and Design 1  That module and this one represent two closely related aspects of the same process www.ThesisScientist.comUML diagrams and C code  Class Diagram – a C application is constructed as classes, which generally correspond to classes in the class diagram  Sequence/Collaboration Diagrams – these describe the messages which pass between objects, and relate to method calls in C code  Object Diagram – represents the objects which exist as the program runs  Use Case Flow of Events/Activity Diagrams – describe sequences of events and workflows which must be implemented in C code www.ThesisScientist.comExample – order system  In the system, an order consists of a number of order lines  An order line specifies a single product and the quantity of that product ordered  Each order is associated with a customer, who has a discount level which is applied to all his or her orders  Sample code in OrderSystem project www.ThesisScientist.comUse case – calculate order price  We will look at the use case, the classes involved in it and the sequence diagram for it  Use Case Description  Calculates the total price of a single order including the customer’s discount  Use Case Flow of events  Calculate the price of each order line as (product price) x (quantity) and add to total  Apply the customer’s discount to the total and calculate the final price www.ThesisScientist.comClass diagram customer can be associated with many orders Customer Order -lastName : string -dateReceived : Date -firstName : string -ordNumber -discount : float -CalcDiscPrice() 1 +GetDiscount() +CalcPrice() orderline only exists as part of an order - 1 composition -orderLines Product -product OrderLine -name : string -quantity : int -productID : string -price : decimal +GetLinePrice() 1 1 +GetPrice() order line contains one product only www.ThesisScientist.comSequence diagram collaborates with other objects to do so responsibility for calculating its anOrder anOrderLine aProduct aCustomer price belongs to order CalcPrice() loop for each order line GetLinePrice() GetPrice() price linePrice CalcDiscPrice() GetDiscount() discount www.ThesisScientist.comCreate classes using class diagram  Create a new C class for each class in the diagram  Declare instance variables based on the attributes in the diagram  Define (empty) methods based on the operations in the diagram  Methods which simply access instance variables (e.g. GetDiscount) can be implemented in C as properties www.ThesisScientist.comCreate classes using class diagram  Consider whether properties should be read- write or read-only  Define constructor to set values of instance variables www.ThesisScientist.comImplement relationships  Add code to implement relationships between classes shown in the diagram  May need to refine model to clarify the meaning of the relationship in some cases  Purpose of relationship is to allow objects to collaborate, so sequence diagram may help clarification by showing what collaborations are needed www.ThesisScientist.comOrderLine – Product association  OrderLine is associated with a single product  OrderLine sends a message to Product  Product does not need to send message to OrderLine, so doesn’t need an OrderLine reference  Association is navigable in one direction only, OrderLine to Product www.ThesisScientist.comOrderLine-Product implementation  Implement this with an instance variable in OrderLine of type Product  Product has no reference to OrderLine  C implementation option – create LinePrice property instead of a GetLinePrice method  Cleaner syntax but no difference conceptually – accessing a property is a message, just like a method call www.ThesisScientist.comOrderLine-Product Note – when constructor would only set properties, can omit it and create objects with object initialiser syntax in C sends message to product object to ask for its price www.ThesisScientist.comCoding patterns  This is an example of a one-to-one “has-a” association  OrderLine has-a Product  Common type of association, usually implemented using the same coding pattern  One class has an instance variable whose type is the name of the other class  Can also provide a property or accessor method for this instance variable if required www.ThesisScientist.comCoding patterns  A pattern is a general reusable solution to a common problem  Patterns can be applied to many aspects of software design and programming, from loops to complex groups of collaborating classes  Here we are using patterns for relationships between two classes (binary relationship coding patterns) www.ThesisScientist.comCoding patterns  The coding pattern for a relationship includes:  Implementation  The code artefact without which the relationship does not exist  Helpers  Properties and methods which may be required to make use of the relationship in particular situations www.ThesisScientist.comCoding pattern: one-to-one “has-a” association  Implementation  Instance variable in one class whose type is the name of the other class  Helpers  Property in the first class which can be used to get or set the associated object www.ThesisScientist.comOrder – OrderLine association  Order is associated with many OrderLines  Order “has-a” set of OrderLines  OrderLine only exists as part of an order – composition  This is a whole-part relationship  Order sends a message to OrderLine (GetLinePrice)  Association is navigable in one direction only, Order to OrderLine www.ThesisScientist.comOrder – OrderLine implementation  Implement this with an instance variable in Order which is a collection of type OrderLine  OrderLine has no reference to Order  Order will call GetLinePrice method of each of its OrderLines  C implementation option – what kind of collection should we use?  No need to search, will only add to end of collection, best option is ListOrderLine