Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oct 200291.3913 R McFadyen1 Recall UML Class Diagram BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*

Similar presentations


Presentation on theme: "Oct 200291.3913 R McFadyen1 Recall UML Class Diagram BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*"— Presentation transcript:

1 Oct 200291.3913 R McFadyen1 Recall UML Class Diagram BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*

2 Oct 200291.3913 R McFadyen2 BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..* Find all persons waiting at any bus stop on a bus route Collaborating classes:

3 Oct 200291.3913 R McFadyen3 class BusRoute { BusStopList busstops; void printWaitingPassengers () { busstops->printWaitingPassengers (); } } class BusStopList { BusStop stops[]; void printWaitingPassengers () { for (int i = 0; i < stops.length; i++) stops[i].printWaitingPassengers (); } Applying Law of Demeter - Partial Java Solution

4 Oct 200291.3913 R McFadyen4 class BusStop { PersonList waiting; void printWaitingPassengers () { waiting.print (); } } class PersonList { Person people[]; void print () { for (int i = 0; i < people.length; i++) people[i].print (); } } class Person { String name; void print () { System.stdout.println (name); } } Applying Law of Demeter - Partial Java Solution

5 Oct 200291.3913 R McFadyen5 Law of Demeter What changes are needed to the current solution in order to adapt to the changes in the model (see next), and still follow the LoD?

6 Oct 200291.3913 R McFadyen6 BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..* Suppose the class model is modified to incorporate Villages: VillageList Village villages 0..*

7 Oct 200291.3913 R McFadyen7 Façade & Observer (Publish-subscribe)

8 Oct 200291.3913 R McFadyen8 Facade P. 368+ Main Entry: fa·cade Variant(s): also fa·çade / f&-'säd/ Function: noun Etymology: French façade, from Italian facciata, from faccia face, from (assumed) Vulgar Latin facia Date: circa 1681 1 : the front of a building; also : any face of a building given special architectural treatment 2 : a false, superficial, or artificial appearance or effect From www.m-w.com

9 Oct 200291.3913 R McFadyen9 Facade P. 368+ Problem: There are a set of classes, a subsystem, that you need to interact with for some purpose, but you don’t want to create dependencies on this subsystem. Solution: Create a class that wraps this subsystem. The wrapper will define an interface that hides the details (classes, methods) of the subsystem. The façade is a “front-end” object that defines a single point of entry to the subsystem’s services. Showing the classes as a package (a subsytem) A façade, a wrapper

10 Oct 200291.3913 R McFadyen10 Facade P. 368+ Text example. NextGen POS is a system to be sold to many customers. Customers will want to customize NextGen POS Example: Payment rules may vary for gift certificates. How are gift certificates to be handled: One per customer per purchase? No change is returned - another gift certificate issued? To allow flexibility, it is desired to reduce the impact of changes to the rest of the system To reduce the impact, the Façade pattern is applied The subsytem will be hidden behind a single object

11 Oct 200291.3913 R McFadyen11 Sale objects, in the Domain package, access the POS Rule Engine via the façade in the POSRuleEngine package if ( POSRuleEngineFacade.getInstance().isInvalid(…)... On page 370: Public class Sale … ASIDE: on Page 371 - mention of the Singleton pattern Figure 23.19

12 Oct 200291.3913 R McFadyen12 if ( POSRuleEngineFacade.getInstance().isInvalid(…)... On page 370: Public class Sale … ASIDE: on Page 371 - mention of the Singleton pattern Singleton is a pattern whereby one instance of an object of some class is created/allowed. The class will have a static method that returns the singleton. Façades are normally accessed via the Singleton pattern.

13 Oct 200291.3913 R McFadyen13 Facade :Sale:POSRuleEngineFacade isInvalid (…) [valid] add (…) : makeLineItem() :SalesLineItem......

14 Oct 200291.3913 R McFadyen14 Example: (from Design Patterns Explained by Shalloway & Trott) Suppose a client object must deal with Databases, Models, and Elements. The client must first open the Database, then get a Model, then queries the Model for Elements, and finally gets Elements. I.e. Client Database Model Element

15 Oct 200291.3913 R McFadyen15 Example: (from Design Patterns Explained by Shalloway & Trott) It may be easier to create a Database Façade that can be used by clients I.e. ClientA Database Model Element Db Facade ClientB ClientC To use the database, one only needs to become aware of DbFacade, and learn how to use it.

16 Oct 200291.3913 R McFadyen16 When to use Facade (from Design Patterns Explained by Shalloway & Trott) To create a simpler interface: number of methods, number of objects one has to deal with The system being hidden may have an older, more complex interface The cost of developing the façade, and of developers learning the new interface may be less than learning the old one You may only be needing a subset of the functionality You may want to augment the functionality of the system To facilitate tracking system usage – by forcing all requests to go through a Façade class, one can easily track the usage. To facilitate subsystem replacement – only one class is affected, the Façade class. This is the motivation in Larman’s example.

17 Oct 200291.3913 R McFadyen17 Observer P. 368+ Also known as Publish-Subscribe Applied in order to implement the Model-View Separation principle (see pages 471+) model and view are separated non-GUI objects are not directly coupled to GUI components domain objects are not directly coupled to window objects same as Model-View-Controller (MVC) principle that came from Smalltalk

18 Oct 200291.3913 R McFadyen18 Observer Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another object (publisher), and we want to keep the coupling low. Solution: Define a subscriber or listener interface that is implemented by the subscribers. Situations: Text example: A user interface object, a window, needs to be informed when a domain object changes In some distributed meta-data environments, replicas need to be notified when the source changes Alarm systems need notification of alarms being triggered...

19 Oct 200291.3913 R McFadyen19 Figure 23.20 the display must reflect the correct total There is a requirement for a window to be updated whenever the total value of the sale changes

20 Oct 200291.3913 R McFadyen20 Observer Text example. There is a requirement for a window to be updated whenever the total of the sale changes A subscriber interface, PropertyListener, is defined. SaleFrame1 is defined to inherit the PropertyListener interface. This will allow SaleFrame1 to be alerted of changes in the value of the sale total A reference to the Sale is passed to SaleFrame1 when SaleFrame1 is initialized. This allows SaleFrame1 to subscribe to the Sale instance The Sale only knows of objects that subscribe to it; it does not know what class of object they are - so, coupling is kept low.

21 Oct 200291.3913 R McFadyen21 Figure 23.21 The Observer Pattern in a DCD

22 Oct 200291.3913 R McFadyen22 Figure 23.22 a window subscribing When a SaleFrame1 (the subscriber) is initialized, it subscribes to the Sale (the publisher)

23 Oct 200291.3913 R McFadyen23 Figure 23.23 a sale publishing a change A Sale receives a message changing its state. The Sale invokes its method, publishPropertyEvent, which will in turn notify any subscribers of the change Note the activations for the sale

24 Oct 200291.3913 R McFadyen24 Figure 23.24 a window receiving notification The window receives notification of the state change and modifies its display appropriately Notice that this is a continuation from the previous sequence diagram


Download ppt "Oct 200291.3913 R McFadyen1 Recall UML Class Diagram BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*"

Similar presentations


Ads by Google