Object-Oriented Modeling and Design
MDE
MDE inc level of abs & automates dev process(faster, consistency, reuse)
Descriptive
understand complex systems, communicate, predict.
Prescriptive
specify system (blueprints) & implement systems
Abstraction
Emphasize important things, ignore irrelevant | Accuracy | Prediction | Low Cost | Understandability (Ex: Obj – world, Class – obj, Superclass – class, Op – set of methods)
Usefulness
Understanding, Communicating, Control complexity, Developing
Structural
(Describe concept and their relationship: class diag), behavior (What happens during execution: state machine), intentional (stakeholders and their objectives), configuration (features and their constraints)
Class
Set of obj, Concept; thing, obj, role (desc for a set of similar objects that have the same structure and behaviour like its instances, leaf must be concrete)
Modularity
Code can be constructed entirely from a set of classes
Abstract class
(no instance created)
Subclass
cannot change type (Ex OverdrawnAccount not acc anymore once $ deposited)
Fields
(instances & class var)
Class variables
shared by all instances (default values/cst, lookup table), static
Variable
not obj, reference obj or no obj, obj lives as long as it’s referenced by variable, type defines what classes of obj it may contain (don’t duplicate att. & assoc), type = interface -> abs class
Instances
Two groups(Att. & assoc)
Encapsulation
(details hidden in class) + Abstraction -> maintainable, evolvable system
Object
Concrete member of a class (object: Class), interact with each other at runtime, identity never change, non role
Attribute
Simple piece of data, exists only when obj exists, lowercase. Ex: Series (title, nbrOfEp), no identity, no plural, role
Association
models relationship btwn two classes (each class plays a role)
Multiplicity
how many instances of one class relate to ? instances of another (One, Many, Optional)*
Set
members unique (same instances not in a set twice), one instance of class A cannot be related to the same instance of class B > 1 for the same assoc
Person(non-role term) plays the role of daughter/son/etc
Reification
Make out of a concept an object (applies to assoc/role)
Physical
object
Composition
part whole, destroyed when parent destroyed (transitive and antisymmetric)
Aggregation
not tied to lifecycle (transitive and antisymmetric)
Association
Operation
query: Constructor (create, build init); Observer (retrieve info about obj state); Modifier (alt state of obj); Destructor; Iterator (access all parts of composite obj)
Abstract Op
subclass must have a concret op if superclass has an abs op
Att/op without type info accepted during analysis, but added during design
UML supports predefined data type (Integer, String, Time, etc) and user defined data type (user classes)
Data type
operation signature (parameters & return value)
Att.
Private (-), Protected (#) , Public (+), Package (~) (default)
[visibility] name [: type] [multi] [= default] [{prop}] + id : String {readOnly}
Operations
[visibility] [type] name [(params)] [{prop}]
where each parameter is defined using: [direction] name : type [multi] [= default]b+ boolean containsKey(key : Object)
Class, package, subclass, world; UML/Java: – YYYY; # YN(Y)YN – Def YYNN – YNNN
Direction
In (=/= modif param), Out(can modif, returns to caller), Inout
Concurrent (proceed multiple calls), guarded (proceed one of the multiple calls), sequential (one)
Generalization
(relationship btwn general and specific), (Three checks: ISA, subclass must retain its distinctiveness, inherited feature makes sense)
Inheritance:
specific element incorporate
Liskov Substitution Principle:
S is a subclass of T, if and only if any instance of T can be substituted by an instance of S, without any visible effect
Overridden
(subclass contains same method): restriction, extension, optimization
Polymorphism
(Abs operation performed in different ways and classes, same name, reduces ifelse switch)
Dynamic binding
(decision about which method running made at runtime): var declared w/ same type superclass, + polymorphic method
1. Check current class for method 2. Check immediate superclass 3. Repeat until method found (error if not found)
Occurrences
Share same info but differ (Ex. episodes in TV)*
Interface
Abstract classes but no statement, public (no body for operations)
MVC
(separate concerns(UI, logic, persistence), enable evolution(change one without changing the others)
1. Request made 2. View calls API of controller 3.Controller interacts with Model (data retrieve/stored from persistence layer) 4. Controller invokes view (view may interact with controller/model – view never makes change to model) 5.View renders next UI
Combined fragments
(Allow multiple sequences to be represented in compact form)
Operators: Alt (multiple operands, guard condition, one chosen), Opt (guarded with no alt, = alt with second operand being empty), Loop, Par (concurrency: 2+ operand executes in parallel), Critical, Break (true: Execute statement then exit, false: skip)
State machines
(Describe reactive event, =/= continuous)
addToWL(Person aPerson) [almostAtCapacity()] / addPerson(aPerson);
Method call [condition] / action
Wait for request(public method called), checks correct state, conditions if any are satisfied, perform action, update state if needed
Different level of abs
(System, component, class, protocol)
Machine’s output depends on input + internal state
(H)
Mealy: action, Moore: transition
Orthogonal regions: Combine concurrent perspectives – shared variables/triggering events
Protocol state machine: Specifies which operations can be called in which state and under which condition
Deferred events: May be deferred if doesn’t trigger a transition in that state, saved until the state does not defer anymore.
History state: Remember last active sub-state before the most recent exit from composite state
Shallow history(H): remember same nesting depth as history state
Deep history(H*): Remember state at any nesting depth
State machine in umple: Event in umple state machine is a new method (not generated because of att. or associations), actions may call generated methods, guard best encapsulated by private methods.
Modeling Patterns: Abstraction-Occurrence, Player-Role, Singleton
Design Patterns: Common design problem & describe best practice, discusses forces at play (good idea or not)
Structural: Adapter (or Wrapper. Use when class interface =/= match with the hierarchy used. Soln: inject class to adapt the interface), Façade(Simplifies use of a package), Proxy (Reduce load of heavyweight objects into database when not needed. Soln: Create a proxy which is a simpler version, has same interface, delegates op to heavyweight, may limit # operations.), Composite(or General hierarchy. allow clients to treat individual objects and collections of them identically. Soln: Use an abstraction to hide the fact that an object is an aggregate of several objects), Decorator(Add additional responsibility to an object dynamically, alternative to subclassing. Soln: Use an abstraction to inject added behavior dynamically)
(Abstracts how classes and objects are composed to build larger structures)
Behavioral: Observer(or Publish-Subscribe. An obj, called the subject, maintains a list ) Use when multiple presentations of an object’s state are needed. Soln: Separates presentation of object state from the object itself), Strategy(Use to make the method by which a task is accomplished easily replaceable. Soln: New approaches to accomplishing a task are produced by extending an abstraction), Template(Use to share common parts of an implementation while allowing subclasses to refine other parts. Soln: Common parts are defined in the superclass, which makes calls to abstract methods that deliver the other parts)
(Abstracts algorithms and assignment of responsibility among objects)
Creational: Singleton, Factory Method(or Virtual constructor. create object without exposing the creation logic to the client and refer to newly created object using a common interface. Use when a class cannot anticipate the objects it must create. Soln: Defers the responsibility of creating objects to subclasses that know what to create), Abstract Factory(or Kit. creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern. Use when your system must be configured with one of multiple families of objects. Soln: Creates abstractions that hide the configuration details of the object families) (Abstracts the object instantiation process)
Immutable (Soln: Constructor of immutable class is the only place where the values of instances variables are set or modified, instance methods which access properties must not have side effects, return a new instance if modified)
Read-Only(Introduce an interface for unprivileged classes and allow more privileged classes to modify the class directly)
YAGNI “You Aren’t Going to Need It”
Anti-patterns (opposite of design pattern, identify poor soln to design problems, symptoms of poor design choices): Code Duplication(Soln: Use an abstraction technique(e.g., method, inheritance) to refactor the code), Schizophrenic Class(A class that contains disjoint sets of public methods that are used by disjoint sets of client classes. Soln: Refactor each set of methods into individual classes), Blob (or God class. A large class that is lacking cohesion or makes most of the processing decisions. Soln: Refactor Blob classes into a series of more cohesive classes), Data Class (API exposes data members instead of providing any substantial functionality, Data manipulated elsewhere. Soln: Review client code for methods that should be located in the data class itself), Data Clump(Large group of parameters appears together in the signature of many operations. Soln: Introduce a new class that encapsulates the parameter group to simplify the operations with the data clump), Feature Envy(A method that manipulates the data of other classes more than its own data. Soln: Method was probably displaced and should be moved to the data it is manipulating), Tradition Breaker (A class breaks the API inherited from its superclass by either overriding an inherited method with an empty method or reducing the visibility of a method. Soln: Inheritance structure is flawed and needs to be fixed, because subclass does not properly substitute its superclass)
Static constraint: invariants(bool)
Constraints defined on a modeling level, but are evaluated on instances. basis: classes and their properties
Constraints are evaluated on instances information of the object graph is queried (represents system status, i.e., a snapshot)
Collection Types
Set {1, 2, 5, 88}, Set {‘apple’, ‘orange’, ‘strawberry’}
Bag {1, 3, 45, 2, 3} is the same as Bag {1, 2, 3, 3, 45}
OrderedSet(T) set with ordered elements
OrderedSet {1, 5, 2, 88}, OrderedSet {‘apple’, ‘strawberry’, ‘orange’}
OrderedBag (T) bag with ordered elements
OrderedBag {1, 3, 45, 2, 3} is not the same as OrderedBag {1, 2, 3, 3, 45}
Summary:
singlennavigation results in a Set, OrderedSet ifn{ordered}, combined navigations in a Bag, and navigation over associations with at least one annotated with {ordered}in an OrderedBag If the multiplicity of an association end is 0..1 or 1, then the expression results in an object or a set with
one element (or possibly null or an empty set in the case of 0..1)
Cannot navigate inheritance
You can use operations of classes in constraints, provided that they have no side effects
Quantification: ForAll, Nesting (For all e1, e2 in employees it is true that e1 e2 implies e1.number e2.number), Exists (checks, if at least one element evaluates to true)
For all collections (set, bag, ordered set, ordered bag)
•size of collection(number of elements in collection) •count of object in collection •collection includes object or collection •collection excludes object or collection •collection is/not empty •collection plus/minus object
Each element in an ordered collection (i.e., an ordered set or ordered bag) has an index
User-defined types: enumeration types
Defined by UML class with stereotype>
User defined set of values for defining constants(literals: class::value) e.g., Status::started – enum Status {started, landed} Can be compared with =,
or each Person: (intersection of wife.employers and employers is empty) and (intersection of husband.employers and employers is empty)
or each Company: intersection of employees.wife and employees is empty