1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Design
• Goal: decide the structure of the software and the hardware configurations that support it.
• How individual classes and software components work together in the software system
– Small programs: ~ 10 classes and interfaces
– Medium programs: 1000s of classes and interfaces
• Design for single user applications different from web applications and mobile applications
• Software Artifacts: design documents, class diagrams, other UML diagrams
OO Design
• Discover classes (and their state)
– What are the useful entities or concepts in your project requirements?
• Determine the responsibilities of each class
– What should your class do?
How does you class manipulate its data?
• Describe the relationships between the classes
– Do classes have instances of other classes?
Does a method of a class need to use an instance of another class?
Finding Classes and Methods
• Candidate Classes (and state)
– Nouns
– You will determine if the noun is an appropriate object or appropriate state
• Candidate Methods
– Verbs
– You will determine what class the method is most appropriate for.
• Use CRC cards to help identify candidate classes
Exercise 2
Unified Modeling Language
• UML: Unified Modeling Language
– Models object-oriented software –
Convergence from three earlier modeling languages •
• OMT (James Rumbaugh)
• OOSE (Ivar Jacobson)
• Booch (Grady Booch)
• Overseen by the Object Mentor Group (OMG): (www.omg.org)
Creating: • Commercial or open-source tools – Microsoft Visio – Commercial Eclipse Plug-ins – Dia (freeware) – UMLetino (web) or UMLet (Eclipse plugin) – PlantUML (“program” diagrams) – Draw.io (web) – LucidChart (free for students)
Full Class Diagram (UML is a type of)
Class in UML
• Class Names
– In italics for abstract classes
– In <> brackets for interfaces (or with a purple I icon)
• State & Behaviors
– Access modifier
• Private: “-” or red square
• Public: “+” or green circle
• Protected: “#” or yellow triangle
• Default: no symbol
– Field/method name
– Behaviors/constructor only: parameter list
– Return type visibility name[(parameters:type,…)]:type
Constants have final values shown. B/c they are static, an underline is used.
Static fields/methods are also shown with an underline
Generalization(is-a) Relationship
• Generalization relationships show inheritance
• The child classes inherit the state and behavior of the parent class
– A Course is-a Event!
• A dashed line shows a class implementing and interface
Composition
• Composition is when one class has an instance of another class
– Fields
– Parameters
• Many ways to model in UML depending on type of composition relationship
• In assignments, requiring a composition relationship means there MUST be a has-a relationship in the diagram
– It doesn’t have to use the composition UML connector of a solid diamond
– It may be modeled using association (arrow) or aggregation (open diamond) connectors
Association Container
Attribute vs Association
• If attributes and associations are essentially the same thing, when should I use each one?
• Attributes
– Small things (Strings, primitives, Dates, etc.)
– Part of existing library
– Immutable value objects
• Associations
– Significant classes
– Part of what will be implemented
– Mutable references
Aggregation Connector
stronger association (unidirectional)
– Ex. A dept contains a set of employees
– Ex. A faculty contains a set of teachers
– A white diamond at the end of the association next to the aggregate class
– The part can exist separate from the whole
Composition Connector
stronger aggregation (unidirectional)
– Ex. Invoice– InvoiceLine
– A black diamond on the end of association next to the composite class
– The part cannot exist separate from the whole (the whole controls the lifecycle of the part).
Whole Class Diagram
Exercise 2
What is the class header of Y?
public class Y implements Z
What is the class header of W?
public class W extends X
What is a minimal implementation of the constructor for class X to avoid NullPointerExceptions? (Note that the parameter for X’s constructor should be named x.)
public X(int x) {
this.x = new x;
this.y = new Y();
}
What would be an appropriate type for list given its multiplicity?
ArrayList or List or LinkedList
What is a minimal implementation of the constructor for class W to avoid a NullPointerException?
public W() {
super(DEFAULT_VALUE);
}
Provide the code for Interface Z.
public interface Z {
void z();
}
Evaluating a Class/Class Diagram
Design Pattern
• Descriptions of communicating objects and classes
– Participating classes
– Roles and collaborations
– Distribution of responsibilities
• Customized to solve a general design problem in a specific context
• Don’t need to start from scratch to solve a programming problem
– Reuse previous useful ideas
• Design patterns are the closest thing we have to a Design Handbook for software engineering
• Design patterns are not a silver bullet!!!
Sometimes the best designs do NOT require design patterns!!!
• Patterns are found, not created!
Pattern Families
• Creational: process of object creation
– Singleton
• Structural: composition of classes or objects
• Behavioral: ways in which classes or objects interact and distribute responsibility
– State/Strategy
– Command
Model View Controller(MVC)
• Isolates business or domain logic from the input and presentation
• Model: data underlying the application
– Changes in state require notifying the view
– Model abstracts the data storage (DB, POJOs, etc.)
• View: UI
– A model may have many views
• Controller: receives input and initiates response by calling model
Design Best Practices
• Classes should have cohesion
– The extent to which the code for a class represents a single abstraction
– Degree to which the members of a class are related to the general purpose of the class
– Allows for reusability of the class in other programs
• Examples:
– Student: only contains information relevant to a student
– Courses: only contains information relevant to courses
• A program should have low coupling
– A connection between two classes is a dependency or coupling
• Instance of an object in the class
• Call another class to complete a task
– Internal coupling: modifying another class’ data – avoid if possible!
– Parameter coupling: using services provided by another class – unavoidable
• Highly coupled programs are difficult to write and maintain
Data and behavior should be encapsulated within a class or a package
– Use packages to group together common functionality
• Example: In an Android application, all Activities are part of the activity package
– Information hiding: make data members private
– Details about implementation are hidden within class and only exposed with public members