1 / 10

TK2023 Object-Oriented Software Engineering

TK2023 Object-Oriented Software Engineering. CHAPTER 13a GRASP Patterns: Creator. GRASP PATTERNS: CREATOR. Problem Who should be responsible for creating a new instance of some class? Solution Assign class B the responsibility to create an instance of class A if one of these is true:

louisbeck
Download Presentation

TK2023 Object-Oriented Software Engineering

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. TK2023 Object-Oriented Software Engineering CHAPTER 13a GRASP Patterns: Creator

  2. GRASP PATTERNS: CREATOR Problem Who should be responsible for creating a new instance of some class? Solution Assign class B the responsibility to create an instance of class A if one of these is true: • B “contains” or compositely aggregates A • B records A • B closely uses A • B has the initializing data for A that will be passed to A when it is created. B is said to be a creator of A objects.

  3. EXAMPLE OF APPLICATION • In the POS application, when the Cashier enters a new item, a line item needs to be created and associated with the current sale. • Referring to the domain model, Sale time 1 Contains * 1 .. Product Sales Description * 1 LineItem Described - by description quantity price itemID

  4. We have a design problem… who should be responsible for creating a SalesLineItem instance? • The Creator pattern suggests that Sale is a good candidate to be assigned that responsibility as a Sale contains many SalesLineItem objects (according to our domain model).

  5. : Register : Sale makeLineItem() SalesLineItem() : SalesLineItem {new} • One way to realize this is to define a method in the Sale class which will carry out that responsibility. Let’s call this method makeLineItem() (for now, we’ll ignore its parameters).

  6. DISCUSSION • A very common task in object design is assigning responsibilities related to the creation of objects. The Creator pattern provides guidance for doing this. • The basic intent of the Creator pattern is to find a creator that needs to be connected to the created object in any event. Choosing that object as the creator supports low coupling.

  7. Register SalesLineItem Sale : Register : Sale makeLineItem() SalesLineItem() SalesLineItem {new} :

  8. Register SalesLineItem Sale : Register : Sale • Consider the following realization: SalesLineItem() s1 : SalesLineItem {new} addLineItem(s1) Coupling is increased!

  9. Creator suggests that the enclosing container or recorder class is a good candidate for the responsibility of creating the thing contained or recorded. • Remember that the Creator pattern only provides a guideline. Use your judgement in deciding whether or not to apply the pattern.

  10. : Register : Sale makePayment() Payment(total) : Payment {new} • Sometimes a creator can be identified by looking for the class that has the initializing data that will be passed in during creation. • For example, supposing a Payment object needs to be initialized to the total of the current Sale. Since Sale knows the total, it is a candidate creator of the Payment.

More Related