1 / 36

Object-Oriented Software Engineering Practical Software Development using UML and Java

Object-Oriented Software Engineering Practical Software Development using UML and Java. Design Patterns Sources: Chapter 6: Using Design Patterns, and Chapter 30 (parts) Designing the Logical Architecture with Patterns

tschmidt
Download Presentation

Object-Oriented Software Engineering Practical Software Development using UML and Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Software EngineeringPractical Software Development using UML and Java Design Patterns Sources: Chapter 6: Using Design Patterns, and Chapter 30 (parts) Designing the Logical Architecture with Patterns Craig Larman’s Text: Applying UML and Patterns, Chaps: 16, 17, 22, & 23.

  2. Patterns - Architectural • Architectural Patterns: • Related to large-scale and coarse-grained design; • Typically applied during the earlyiterations (the elaboration phase) when the major structures and connections are established. • Examples: pipe and filter; MVC; Client-Server; Transaction, Broker, others… Chapter 6: Using design patterns

  3. Patterns – Design • Design Patterns: • Related to the small and medium-scale design of objects and frameworks. • Applicable to designing a solution for connecting the large scale elements defined via architecturalpatterns • Done during detailed design work • Design patterns are sometimes known as architectural patterns. Chapter 6: Using design patterns

  4. General Concepts of Design Patterns • Design Patterns are groups of objects (and their relationships) designed to supporta ‘good object design’ • What is ‘good object design?’ • design that yields highcohesion of our objects, • design that has lowcoupling between our objects, which, among other things, fosters more likely reuse • It is one thing to create an object…it is quite another to create the right object for the right situation. Chapter 6: Using design patterns

  5. Object Design – Not a Simple Undertaking! • All design involves making decisions, and design is a decision-making activity • Good object design involves the assignment of object responsibilities. • There are patterns or groupings of objects that work together to accommodate commonly needed activities / functions that subscribe to accepted principles of good design. • Deciding what methods belong where and how objects interact (their relationships) is critically important and NOT trivial. • All this is really at the heart of what it means to develop an object-oriented system, not merely the drawing of domain model diagrams, package diagrams, etc. Chapter 6: Using design patterns

  6. 6.1 Introduction to Patterns (from OOSE book) • ‘Design Patterns’ refer to a recurring arrangement of classes to better address a specific design issue. • A pattern • is the outline of a reusable ‘solution’ • to a general problem • encountered in a particular context • A good pattern should • Be as general as possible • Contain a solution that has been proven to effectively solve the problem in the indicated context. Studying patterns is an effective way to learn from the experience of others Chapter 6: Using design patterns

  7. Introduction – Suggestive Names and Concepts Patterns have names and the names are ‘suggestive’ of what function they address. • Their names can be used in our vocabulary to ‘communicate design intentions.’ • Here again, naming a pattern is using an abstraction of the details of that pattern. • Patterns represent well-known knowledge • We are documenting common practice • Should be in the public domain • Need to be written for the public good. Chapter 6: Using design patterns

  8. Sample Titles of Some Design Patterns. • The Façade Pattern, can be used to provide the interface from one • layer to the next… • Singleton Pattern – to ensure global access to a singleinstance of a • class. (a class (really object!) that many processes may desire • access to: a monitor?) • More Design Patterns: • Information Expert (Expert); Adaptor, Creator, Observer, • Low Coupling, High Cohesion, Controller; etc. etc. • Design Patterns are often organized into different categories too. • (Most of these notes from the first two lectures on Design Patterns are taken directly from Larman’s text.) Chapter 6: Using design patterns

  9. Want to look at recurring groupings of classes regularly used to address commonproblems and design issues. • Want to take advantage of experiences of others and create a better, • more resilient design. • Want to use • patterns that assist us in separatingconcerns • (Abstraction-Occurrence, Observer, Player-Role); • patterns used to better createclasshierarchiesofinstances; • patterns in which one method simply calls another method • in another class; • patterns where you use Delegation to gain access to facilities • in one or more other classes (Adaptor, Façade, Proxy); • patterns that help protect other objects from unanticipated • access (Immutable and Read-Only Interfaces). Chapter 6: Using design patterns

  10. Design Patterns deal with Class Diagrams • We are looking at certain arrangements of classes and relationships among classes that provide better design and/or more flexibility in addressing common design issues Chapter 6: Using design patterns

  11. Categories of Design Patterns • We will study several of the most often used patterns – GRASP Patterns, and • Gang of Four (GoF) Patterns • You will note you may be already doing some of these things. • You may now see more rationale and more formal reasoning • In all cases, the patterns will be most helpful as we strive to design objects and groupings of objects to accommodate common issues. Chapter 6: Using design patterns

  12. Grasp Patterns • General Responsibility Assignment Software Patterns. • In the title, GRASP, Patterns’ is redundant – but it stuck. • Recognize that according to Craig Larman: • “The skillful assignment of responsibilities is extremely important in object design, • Determining the assignment of responsibilities oftenoccurs during the creation of interactiondiagrams and certainly during programming.” • (Larman, p. 219. Please note that much of the following information is taken directly from Larman’s book.) Chapter 6: Using design patterns

  13. GRASP Patterns • A Design Model may have • hundreds or even thousands of software classes and hundreds or thousands of responsibilities to be assigned. • During objectdesign, when interactions between objects • are defined; we make choices about the assignment of • responsibilities to software classes.. • Let’s look at a few Grasp Patterns… Chapter 6: Using design patterns

  14. 1. Information Expert (or Expert) (GRASP Pattern) • We start by looking at the Expert Pattern (or InformationExpert Pattern) This one is pretty simple and yet very important. • Note how we evolve this class…. • Start by assigning responsibilities by clearly stating the responsibility: In this case, we are talking about a Sale. • Who should be responsible for knowing the grand total of a sale? • (We know we need a grand total from Use Cases / requirements • and interaction diagrams for this scenario) • So, the real question then becomes, ‘who’ has the information • needed to determine the total? Chapter 6: Using design patterns

  15. Expert (Grasp Pattern) • Next point:  look inDomain Model or Design Model. • Domain Model contains conceptual classes of the real-world domain;These are often business/ application entities in common use enterprise-wide. (attributes not responsibilities) • Design Model contains the software classes. • (These will have attributes and methods) • First choice: If we have relevant classes in DesignModel, choose this first. • Second choice: look into DomainModel and attempt to use (or expand) its representations to inspire the creation of corresponding design classes. Chapter 6: Using design patterns

  16. Expert (Grasp Pattern) – Using Domain Model • Assume we are just starting. We have no real Design Model. So look to Domain Model and we find “Sale.” Sale date time Is this clear to you?? (note: no responsibilities) 1 Contains * Product Specification SalesLineItem * 1 description price itemID Described by quantity Chapter 6: Using design patterns

  17. Add Sales Class to the Design Model • Add a ‘Sale’ class to the Design Model, and give it • the responsibility of knowing its total, expressed with a method named getTotal. • This approach supports a low representational gap, in which the software design of objects appeals to our concepts of how the real domain is organized…. • We now have: Chapter 6: Using design patterns

  18. Our First Design Class: Our first design class: :Sale date time t := getTotal() : Sale New method ------ getTotal() Notice also that the requirement for a responsibility came from constructing the sequencediagram (on the left). Chapter 6: Using design patterns

  19. Is ‘Sales’ Enough? • What information do we need to compute a grand total? • Need to know about all the SalesLineIteminstances of a sale and sum their subtotals. Clearly, a Sale consists of those sales items purchased, and they are alldifferent. • Thus a SalesInstance contains all these; thus by ‘Information Expert,’ Sale is the suitable class for the responsibility for computing a grand total • Standard approach: Recall some programming examples dealing with CDs and arrays of CDs – perhaps from data structures classes… • CDLibrary may contain number, cost, etc. • But it will be referring to CD instances. • Orders (in a store) refers to OrderList. • Students and then StudentList (or whathaveyou)…. • This is the Information Expert design pattern – one you may have been using already. • But it still needs help…and we are not done. Chapter 6: Using design patterns

  20. What information is needed to determine the line item subtotals? • We need: SalesLineItem.quantity and • ProductSpecification.price • The SalesLineItem “knows” its quantity (is typically an attribute) and its associated ProductSpecification (via association); • Therefore, by Expert, SalesLineItemshould determine the subtotal; it is the InformationExpert in this case. • So, ‘now’ what do we have? Chapter 6: Using design patterns

  21. Consider: Well, we now have: :Sale date time getTotal() t := getTotal() 1 *: st := getSubtotal() : SalesLineItem : Sale :SalesLineItem quantity getSubtotal() Note: we have added a responsibility, getSubtotal() to SalesLineItem We have also added another class to our Design Model This is a pretty standard way of designing objects: the nouns and the instance. Sale and SalesLineItem; Order and Order Entry; CD and CD instances…. Chapter 6: Using design patterns

  22. Note the Use of the Domain Model!! • To fulfill the responsibility of knowing and answeringthesubtotal, a SalesLineItem (design class) needed to know the productprice. • The ProductSpecification is also aninformation expert on answering its price (it is clearly an attribute of ProductSpecification); thus we need a messagesent to ProductSpecification asking for the price. • (something like: price and getPrice() ) •  Note: this is WHY we don’t normally include operations (methods) in the Domain Model – only entity attributes. • We do not know for sure ‘how’ these entities are going to be used, and it is how they will be used (that is assigned responsibilities) that are determined in developing the designclasses. • This is shown on the next slide….and we end up with: •  Chapter 6: Using design patterns

  23. Consider: • See the designclasses and an abbreviated • communicationsdiagram. Sale date time getTotal() t := getTotal() 1 *: st := getSubtotal() : SalesLineItem : Sale 1.1: p := getPrice() SalesLineItem Note not only the messages sent but also the multiplicity (one to zero or more; one-to-one, etc.) Please note that the responsibilities were decided upon whiledrawing an interaction diagram; that is design.. The principle by which each responsibility was assigned was Information Expert – placing responsibilitieswith the object that had the information needed to fulfill it. quantity getSubtotal() : Product Specification Product Specification description price itemID getPrice() Chapter 6: Using design patterns

  24. Continuing…Design Model considerations • Information Expert is a frequently used Design Pattern which has great value in assigningresponsibilities. • It is theeguidingprinciple; continuouslyused in object design. • Used extensively in data structures class. •  Note that the fulfillment of responsibilities often requires spanning several different classes. • This implies that there are several partialinformation experts who collaborate in the task. • Fundamentally, these “information experts” do things relative to the information they ‘know.’ Chapter 6: Using design patterns

  25. But be careful: Don’t run ‘Expert’ into the Ground! • Who should be responsible for savingSale in a database? • Clearly, much of the information is ‘in’ the Sale object and thus by ‘Expert’ an argument could be made to put that responsibility in the Sale class. • But, by extension, then, EACH class would have its own services to save itself in the database. • This, of course, is untenable and leads to problems in cohesion and coupling, reuse, and duplication Chapter 6: Using design patterns

  26. What does all this mean? If we were to do this: • Cohesion and Coupling – Two Essential Design Principles: • Sale would have to nowcontainlogic related to database handling, such as related to SQL and JDBC (for J2EE) or more • If we included logic to save the data, the class would now no longer be focused (decreasedcohesion!) on just pure application of logic of ‘being a sale;” • Classnow has other kinds of responsibilities, like ‘saving itself’ which lowers its cohesion! This is NOT desirable!! • (Rule of Thumb: We always want to separate I/O from computations / data manipulation - whether it is a ‘paragraph, function, object, etc…’) Chapter 6: Using design patterns

  27. What does all this mean? If we were to do this: • Cohesion and Coupling – Two Essential Design Principles - more: • Classes like this one (and other classes that need database services) need to be coupled to the technical databaseservices – likely found of another subsystem, such as JDBC services, ratherthan ‘just’ being coupled to other objects in the domainlayer of software objects. • This, of course makes the coupling tighter (in general not desirable) – but this provides the benefit of increasing its cohesion (highly desirable) and strengthens the case for its possible reuse.. • (And, it is likely that similar database logic would have to be duplicated in many persistent classes were this responsibility relegated to the classes themselves) Chapter 6: Using design patterns

  28. Being careful with Expert - Final • Keep application logic in oneplace (like domain objects) • Generally found in an ‘application layer’ if your architectural pattern is a layered architecture. • Keep databaselogic in anotherplace (e.g. persistence services subsystem), rather than intermingling different system concerns in same component. •  Supporting a separation of major concerns improves coupling and cohesion in a design. • Thus, even though “byExpert” - could be justification to putting • responsibility for database services into the Sale class, for other • reasons, (usually coupling and cohesion), this represents a very poor • design choice. Chapter 6: Using design patterns

  29. Benefits of Expert: • 1. Information encapsulation is maintained, since objects use their own information to fulfill tasks. • This usually supports low coupling, which leads to a more robust and maintainable system. (‘Low Coupling’ is also a GRASP pattern). • Behavior is distributed across the classes that have the required information thus encouraging more cohesive, ‘lightweight’ class definitions that are easier to understand and maintain. • High cohesion is usually supported. • Reuse potential up. Chapter 6: Using design patterns

  30. The Creator Pattern (a GRASP Pattern) • Problem: Who is responsible for creating new instances of some class? • Solution: Assign class B the responsibility to create an instance of class A if one or more of the following is true: • B aggregates A (simple aggregate; shared attributes) • B contains A (composition; non-shared attributes) • B records instances of A objects • B closely uses A objects • B has the initializing data that will be passed to A • when it is created (thus B is an Expert with respect to • creating A) • e.g. queue collection class; queue driver class; stack …. • If more than one option applies, prefer a class B which • aggregates or contains class A. Chapter 6: Using design patterns

  31. Creator - continued • This is a very common activity in designing and implementing OO systems. • We’ve done this in our data structures classes…. • We have a State class and we create instances of State objects, or • We have a CD class, and we create instances (an array?) of CD objects…. • This is, then, a general principle for the assignment of creation activities. • This approach can result in lowcoupling, increased clarity, encapsulation, and reusability. • Let’s look more closely at this pattern… Chapter 6: Using design patterns

  32. Creator - Example • In Craig Larman’s Point of Sales application, we have the previously described class relationships. • (done by Expert) Sale date time getTotal() 1 Contains * Product Specification SalesLineItem * 1 quantity description price itemID Described by getSubtotal() getPrice() Who should be responsible for creating a SalesLineItem instance? In Creator, we look for a class that aggregates, contains, records … SalesLineItem instances (Remember, in Expert we liked to assign responsibilities to the classes that contained the data, with a close eye on resulting coupling and cohesion..) Chapter 6: Using design patterns

  33. Creator – Sales aggregates SalesLineItems • Note that ‘Sales’ aggregates, uses, etc. SalesLineItems. • Sales contains / has_a (one or more) SalesLineItems. • Since a Sale contains many SalesLineItem objects, the Creatorpatternsuggests that Sale is a good candidate to have the responsibility of creatingSalesLineItemobjects. • See next slide: • Clearly a Salecontains (aggregates) SalesLineItem objects. • Thus the Creator design pattern suggests that Sale is a good candidate for the responsibility of creatingSalesLineItem instances. • This may sound ‘all too obvious.’ But how many times have you been aware that you needed to create an array of objects and been uncertain exactly where or in which object to create this array? • Here is guidance using a proven, reliable approach that supports cohesion (reuse). Chapter 6: Using design patterns

  34. Abbreviated Sequence Diagram: :Register :Sale makeLineItem(quantity) create (quantity) :SalesLineItem The sequence diagram suggests an assignment of responsibilities that requires that a makeLineItem method be defined in Sale. (This is an additional ‘responsibility’ in Sale) Once again, the context for these decisions was while drawing the interaction diagram. Chapter 6: Using design patterns

  35. Benefits of Creator Pattern: • Object creation is another very common activity, and we want a ‘creator’ that needs to be connected to the ‘created’ object; • Please note that sometimes a creator is found by looking for the class that has the initializing data needed to be passed during creation. • The use of initializing data that suggests a class as a Creator is actually an example of the Expert pattern. Initializing data might be ‘passed in’ during creation via some kind of initialization method, like a Constructor, or, …. • See Craig Larman’s textbook for more complete • descriptions Chapter 6: Using design patterns

  36. More Grasp Patterns Coming… • We will next look at • Controller pattern – another Grasp Pattern. • Façade pattern • Singleton pattern • Observer pattern • Delegation pattern • Adaptor pattern • Proxy pattern Chapter 6: Using design patterns

More Related