Sie sind auf Seite 1von 24

7/21/12

KIT2 OOAD-UML-II

Chapter 10
Using Frameworks and Patterns
The success of a software solution depends largely on how well its various components have been designed. An effort should be made to ensure that the components of the existing software solution can be reused. This can be done by using frameworks and patterns. This chapter explains how to apply frameworks and patterns to design a software system. In addition, it explains how to generate code by using a design model.

Objectives
In this chapter, you will learn to: Apply frameworks and patterns to design a software system Generate code from a design model

Understanding Frameworks and Patterns


BlueSeas Inc. is a company that provides software consultancy services. The company foresees a high demand for retail automation software. As a result, BlueSeas Inc. decides to develop a Retailer Automation Solution (RAS) that provides the functions to update the inventory, automate the sales records, and reduce the transaction-processing time. The RAS needs to be developed in such a way that it can be modified according to the specific requirements of each retailer. For this reason, BlueSeas Inc. ensures that the RAS is extensible and adaptable, which enables BlueSeas Inc. to reuse the components of the RAS to create customized solutions for different retailers. To ensure that the components of the RAS are reusable, you need to create a framework and use patterns to develop the RAS. A framework is a set of classes that provides the basic functions for a particular domain, such as retail shops. You can create new applications by using the classes provided in the framework to implement the basic functions. Patterns make the classes used in developing the software system independent to ensure that the classes remain reusable and manageable.

Defining Patterns and Frameworks


A development team tends to reuse its existing solutions to reduce the development time and increase quality. Reusing an existing solution includes reusing the existing code, a component, a test case, or a concept that has been implemented in similar projects. Patterns and frameworks are standards that are used to make the software components reusable.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

1/24

7/21/12

KIT2 OOAD-UML-II

Frameworks
A framework defines the features that are common to similar applications belonging to a particular domain. These features are, in turn, extended and customized according to a particular application. Frameworks increase reusability and reduce the application development time. Microsoft Foundation Classes (MFC) provided with Microsoft VC++ is an example of a framework that allows you to develop GUI having common characteristics, such as command buttons. MFC also allows you to create customized components, such as bitmap buttons, which extend the various features of MFC framework. A framework has the following characteristics: Represents a collection of classes or a library that enables you to perform a particular function without developing the code from scratch. Contains abstract and concrete classes that realize interfaces to conform to predefined specifications. Contains classes that can be extended by subclassing. Defines abstract methods that receive messages in the derived class from the predefined classes. In case of BlueSeas Inc., you can implement the RAS as a framework consisting of a library of classes, interfaces, and abstract classes. The framework should provide the functions for the general behavior of the retail stores, such as calculating the billing amount of goods purchased. The framework should also allow you to define customized behavior, such as giving discounts on product quantities or on the purchase of a combination of products or to the members of the retail store.

Pattern
A pattern is a set of principles and guidelines that provide the standard solution to a given problem. Patterns enable you to create solutions that can be reused to fulfill new requirements by modifying the existing system. A pattern is useful in making communication better among software components. It is also useful in making the design reusable, solving a problem in the most efficient way, and assigning responsibilities to classes.

Note

A responsibility is defined as a contract or an obligation of a class. The assignment of responsibilities is necessary in designing a solution because it describes what a class will do and define its operations and private data accordingly.

Patterns are generally categorized into the following two categories: General Responsibility Assignment Software Patterns (GRASP) Gang of Four (GoF) patterns

GRASP
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 2/24

7/21/12

KIT2 OOAD-UML-II

GRASP is a set of patterns that provides the principles of assigning responsibilities to objects. Responsibility assignment decisions are reflected in sequence and communication diagrams. GRASP consist of the following patterns: Expert Creator Controller

Expert Pattern
The expert pattern provides guidelines to assign responsibility to a class that contains the relevant information. For example, in the RAS application of BlueSeas Inc., you may need to know the total sales. You can use the expert pattern to identify the class that is responsible for knowing the total sales. The information about total sales can be obtained from the S a l eclass. However, in order to calculate the total sales, the S a l eclass would need the subtotals of the amount of each sold item from the S a l e L i n e I t e mclass. The S a l e L i n e I t e mclass would in turn need the price of a product from the P r o d u c tclass. The following is a partial class diagram that shows the relationship between the S a l e , S a l e L i n e I t e m , and P r o d u c tclasses.

According to the expert pattern, responsibility of calculating the total sales should be assigned to the S a l e class, the responsibility of calculating the subtotals for each sold item should be assigned to the S a l e L i n e I t e m C l a s s , and the responsibility of providing the price information of a product should be assigned to the p r o d u c tclass. The same is illustrated in the following partial communication diagram.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 3/24

7/21/12

KIT2 OOAD-UML-II

Creator Pattern
The creator pattern provides the guidelines for assigning responsibility to a new object of a particular class. For example, in the RAS application, a sale consists of various sale line items. The S a l eclass contains various objects of the S a l e L i n e I t e mclass. For this reason, the S a l eclass has the responsibility to create the objects of the S a l e L i n e I t e mclass. In other words, the S a l e class is the creator class of the S a l e L i n e I t e mclass. According to the creator pattern, a class is responsible for creating objects if any of the following conditions are true: A class contains another class. A class records the instances of another class. A class uses the objects of another class. A class provides information to initialize the objects of another class. The creator pattern makes the software more reusable and manageable because it centralizes the responsibility of creating the objects and reduces interdependency among different classes. The following figure depicts the sequence diagram for creating a S a l e L i n e I t e m .

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

4/24

7/21/12

KIT2 OOAD-UML-II

In the preceding figure, S a l eis the creator class for the S a l e L i n e I t e mclass because the S a l eclass contains the objects of the S a l e L i n e I t e mclass.

Controller Pattern
The controller pattern provides the guidelines for handling system events. A system event is generated by an external entity and represents a signal that indicates that the system needs to perform some action. The system will have some class that receives the event. However, it is not necessary that the class that receives the event should handle the event. According to the controller pattern, you assign the responsibility of handling the system event to the class that fulfills one or more of the following criteria: Represents the overall system Represents a use case handler

Note

A use case handler is a class that handles all the system events of a use case. A use case handler localizes the functionality required to handle a use case and makes the design more flexible.

The class that is assigned the responsibility of handling the system event is called a controller. There is a tendency to use user interface objects, such as windows and frames as controller objects. This is not advisable because it weakens the cohesion of the class and results in increased coupling. The use of a separate control object results in a better layering of the architecture. In the RAS application, at end of the day when cashier clicks the Close Shop button, a system event is generated that indicates that no more sales processing should be done. In this scenario, instead of the assigning the responsibility associated with the C l o s eS h o p button to a user-interface object, the responsibility can be assigned to another class, say C l o s e S h o p H a n d l e r . When the user clicks the C l o s eS h o pbutton, the C l o s e S h o p H a n d l e rclass activates and starts controlling the
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 5/24

7/21/12

KIT2 OOAD-UML-II

processing activities for closing the shop. It first checks that there should not be any in-process sales. It then de-activates any new sales creation. Next, it activates the D a y E n d A c c o u n t i n gclass to perform day end accounting activities. Once this is done, it activates the S y s t e m S h u t D o w nclass to initiate system shutdown.

GoF Patterns
The GoF patterns were introduced by the four authors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. GoF patterns are based on open-close principle, which suggests that the design should be open to extension and close to modification. In other words, the open-close principle states that GoF patterns should make the design flexible enough to accommodate new changes without modifying the existing code. The GoF Patterns are used to create a flexible design. Therefore, they are also known as design patterns. There is a difference between GRASP and GoF patterns. The GRASP patterns describe the fundamental principles of assigning responsibilities to objects. Whereas, GoF patterns are mechanisms that apply to classes that enable you to solve a given set of problems. The various features of the GoF patterns are: Enable you to reuse existing solutions for common design problems Establishes common terminology for problems and their solutions to improve understanding According to their use, GoF are broadly categorized into three types: Creational Structural Behavioral

Creational Patterns
Creational patterns provide techniques for creating objects and managing the lifecycle of the objects. The most commonly used creational patterns are: Factory Builder Singleton

Factory Pattern
The factory pattern provides a class known as f a c t o r yclass, which controls the lifetime of the objects of the subclasses of an abstract base class. The f a c t o r yclass contains a method known as f a c t o r y method, which depending on its arguments returns the required object of the subclasses. Consider that you have an abstract base class, S h a p e , with an abstract method, D r a w (), to draw a shape on the screen. Two classes, L i n eand A r r o w L i n e , are derived from the S h a p eclass and provide
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 6/24

7/21/12

KIT2 OOAD-UML-II

definitions of the D r a w () method. Depending upon the specification by an end user, either a line or an arrow line is drawn. To draw the lines, you need to create and use object of either L i n eclass or A r r o w L i n e class and use the D r a w () method of the S h a p eabstract class. In this case, you need another class that provides the f a c t o r ymethod to create the objects of the L i n eand A r r o w L i n eclasses. If the end user specifies that a line needs to be drawn, a parameter, line, is passed to the f a c t o r ymethod. The f a c t o r y method, in turn, creates and returns the object of the L i n eclass. You need not use the L i n eclass and A r r o w L i n eclasses directly in code because f a c t o r yclass is creating the required objects. The factory pattern is required in the following cases: When you are not able to anticipate which type of objects are required at runtime When the base class is abstract and the pattern must return an initialized object

Builder Pattern
The builder pattern separates the creation and construction of a complex object from its representation so that the same construction process can create different representations. An example of a complex object can be a panel that contains various other objects, such as buttons and textboxes. Builder pattern enables you to create several representations of the same panel object with a flexibility of modifying the alignment of the objects. Builder pattern is similar to abstract factory method because both create family of objects. In addition, builder pattern assembles or structures the simple objects to represent a complex object. For example, the RAS application may contain two panels: one panel contains the bill and the other contains product specification. The bill panel contains a list of product IDs, product name, quantity, and price. Product specification panel contains the product id, product name, vendor, price, and material used in the product. You can use an abstract class, P r o d u c t , which includes the data members, such as productId and product name. You can also create two different GUIs for displaying bill and product specifications. To create two different GUIs, you need to derive two classes, B i l land S p e c i f i c a t i o n s from the P r o d u c tclass. You need to create another class, B u i l d e r , which builds the GUI using these two class objects. The f a c t o r y ( )method of the B u i l d e rclass creates and constructs the GUI using the required user interface components.

Singleton Pattern
The singleton pattern is class that allows you to create only one instance of itself. A singleton pattern is useful when you need to access a single object representing a real life object, such as a printer or a mouse. To define a singleton pattern, you use a static data member that keeps track of the life of the created object. The static data member is incremented in constructor and decremented in the destructor of the object. Consider an example of an application where you want to create a class, M o u s e , which enables you to handle all the mouse events in the application. You need to put constraint on the number of objects of the M o u s eclass as there is only one mouse attached to the system. To define a singleton pattern, you use a static data member that keeps track of the life of the created object of the M o u s eclass. The static data member is incremented in constructor and decremented in the destructor of the object in the M o u s eclass. If aM o u s eclass object is alive, then the pattern will not allow you to create another M o u s eclass object.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 7/24

7/21/12

KIT2 OOAD-UML-II

Structural Patterns
Structural patterns describe how classes and objects can be combined to form larger structures using object composition. In other words, structural patterns describe the composition of objects and their organization to obtain new and varied functionality. The most commonly used structural patterns are: Composite Proxy Decorator Facade

Composite Pattern
A composite pattern represents a complex object, which can further consists of simple objects and complex objects. The composite pattern provides you with interfaces to access the composite and simple objects within the composite object. You can represent the composite pattern as a tree in which the composite objects represent the nodes and simple objects represent leaves of the tree. For example, in the RAS application, you can implement a c o n t a i n e rclass in the O r d e r D e t a i lform. Each c o n t a i n e robject consists of either a simple object, such as a t e x t b o x , or another container object. Providing a common interface in the form of c o n t a i n e rclass is an implementation of composite pattern in the RAS application.

Proxy Pattern
You can use the proxy pattern when you need to represent an object that is either complex or time consuming to create, with a simpler object. If it is expensive to create an object in terms of time or computer resources, you can use a proxy object as a placeholder for that object. The proxy object allows you to postpone the creation of the actual object until you actually need the object. A proxy class usually has the same methods as the object it represents. Once the object is loaded, proxy object passes on the method calls to the actual object. For example, in the RAS application, login information of store manager is stored in the database, which is residing on a remote machine. The store manager uses a user interface to enter login information. The proxy object, acting as the complex object, is available at the client end from where the store manager enters the login information. When the store manager clicks the Login button to connect to the database, the proxy object is initialized and connects to the database to authenticate the login credentials.

Decorator Pattern
The decorator pattern allows you to modify the behavior of individual objects without creating a new derived class. You use decorator pattern when you need to define additional functionality for the existing objects. The decorator pattern works by wrapping a new decorator object around the original object. This is typically done by passing the original object as a parameter to the constructor of the decorator. The decorator implements the new functionality and maintains the interface of the original object.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 8/24

7/21/12

KIT2 OOAD-UML-II

This pattern offers a flexible alternative to creating derived classes. The main difference between derived classes and the decorator pattern is that derived classes add behavior at compile time, whereas the decorator pattern provides new behavior at runtime. When you create a derived class, the change you make to the child class will affect all instances of the child class. However, with the decorator pattern, you apply changes to each individual object you want to change. Consider that you need to create toolbar buttons for OrderDetails form in the RAS application. Some of the buttons in the toolbar may have an additional feature, such as a hovering effect. This can be easily implemented by creating a derived class for each of these buttons. However, if each of these buttons need to be modified in different ways, then multiple derived classes would need to be created. Suppose that three of the buttons in the toolbar need to be modified such that one button has a hovering effect, the second displays text along with the icons, and the third has a hovering effect as well as displays text along with the icons. In this case, creating multiple classes would unnecessary result in increased complexity. A better alternative in this case would be to create a D e c o r a t o rclass that decorates the buttons. Then, we can derive two specific decorators, h o v e r D e c o r a t o rand t e x t D e c o r a t o rfrom the main D e c o r a t o r class, each of which performs a specific type of decoration. When a button needs to have a hovering effect, you can wrap a h o v e r D e c o r a t o robject around the b u t t o nobject. When a button needs to display text along with icons, you can wrap the t e x t D e c o r a t o r object around the b u t t o nobject. When a button needs to have a hover effect and display text along with the icons, you can first wrap the h o v e r D e c o r a t o robject around the b u t t o nobject and then wrap the t e x t D e c o r a t o robject around the same b u t t o nobject.

Facade Pattern
The facade pattern simplifies the software development by providing simplified interfaces. For example, you may need to provide Mouse support in an application, which is running on the DOS platform. The DOS platform provides various Application Programming Interfaces (API) for using a mouse. To simplify this implementation, you need to provide a facade class that wraps the APIs. The facade class provides methods to enable your application to use the wrapped functionality. Facade pattern is also useful in implementing layered architecture. For example, you can implement TCP/IP sockets in the RAS application. The functionality provided by the TCP/IP protocol is implemented using various layers where each layer interacts with another layer to perform a specific function. Each layer acts as a subsystem because functionality for each layer is precisely predefined for TCP/IP protocol. You should use a facade pattern for each layer to hide the internal structure and functionality of the layer. Facade pattern, in turn, provides interfaces to make the other layers access the functionality implemented.

Behavioral Patterns
Behavioral patterns provide guidelines for enabling communication between two objects. The most commonly used behavioral patterns are: Chain of Responsibility
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 9/24

7/21/12

KIT2 OOAD-UML-II

Command Observer

Chain of Responsibility
The chain of responsibility pattern describes how various classes should handle a request. Each class contains the logic that describes the types of requests it can handle. It also describes how to pass off the requests that the class cannot handle to another class. Chain of responsibility represents the loose interconnection between classes. This means that the interaction between classes or the request of method invocation is passed among classes. The request continues to flow across the classes until one of the classes responds to it. Consider an example of the RAS application where you can create help for the application. The help for the application can be displayed using Web pages. Each Web page of the help should contain information specific to a particular topic only. Help about more generic topics should be provided at pages that consist of information about complete application. You should use the chain of responsibility pattern to handle end users request using a chain of modules. Whenever an end user seeks help, the help topic is transferred to a particular module, which is responsible for displaying Web page containing the help for that topic. For example, if an end user seeks help for a tool button in the application, the topic is transferred to the first module. The first module shows the help message if it contains the help for the tool button, otherwise the request is transferred to the next module. The process continues until a method in the chain handles the responsibility. If the request is not handled by any of the modules, the message is lost and no information is displayed.

Command Pattern
The chain of responsibility pattern forwards the received requests along a chain of classes. However, the command pattern forwards a request only to a specific module. The command pattern uses objects to represent actions. A command object encapsulates an action and its parameters. The command pattern provides a mechanism to separate implementation of a command from the interface environment. The request for a specific action or method invocation is transferred through an interface. Consider that you want to create a GUI for the RAS application. You need to provide the menu items, buttons, and checkboxes that allows the user to perform the functionality provided in the existing application. According to the command pattern, you need to create separate classes to represent each of these objects. The actions related to each of these objects would then be handled by the corresponding class.

Observer Pattern
The observer pattern enables you to create separate objects for displaying the information in various forms. One of the objects contains data and other objects display data in various forms, such as in tabular form or in a list box. If any change occurs in the object that contains the data, the change is reflected in all display objects simultaneously.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 10/24

7/21/12

KIT2 OOAD-UML-II

Consider the example of the RAS application. The RAS application displays the product prices in: A tabular form in the CUI based application at cashier computer. A list box on the Web page. The change in data should be reflected in both, the tabular and graphical forms. You need to implement the observer pattern, which separates the graphical display and the tabular display and still manages to synchronize the objects. According to the observer pattern, the object containing the data needs to be separate from the objects that display the data in different forms. These display object observe changes in the object containing the data. The observer registers its interest in the data by calling a public method in the object containing the data. Whenever data changes, the object containing the data calls a known interface in each observer to inform the observer of the change.

Just a minute:

Which of the following pattern provides guidelines to assign responsibility to a class that contains the relevant information? Expert Creator Controller Structural

1. 2. 3. 4.

Answer:
1. Expert

Modeling Design Patterns


Design patterns are modeled with respect to the real world scenarios and with respect to the developer who creates the design pattern. The various views in which you model the design patterns are: Outside view: Represents the structure of the design pattern as a parameterized collaboration. Note that the parameterized collaboration contains formal parameters, which bind with the classes that realize the parameterized class. Inside view: Represents the structure of the design pattern as seen by the creator of the pattern, such as the developer. The inside view is depicted as a collaboration without parameters. The collaboration has a structural and behavioral aspect. The structural aspect of collaboration is modeled by a set of class diagrams and the behavioral aspect is modeled using interaction diagrams.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 11/24

7/21/12

KIT2 OOAD-UML-II

To model a design pattern, consider the example of the command pattern. When you apply the pattern, you need to bind elements to the parameters. Elements signify the commands that need to be executed, such as opening a file by clicking a command button or selecting an operation from the menu. When depicting the command pattern, you may represent the elements as Application, CalculateBill, MenuItem, and Document. Application represents the user interface of the RAS, MenuItem represents the various options available on the menus of the RAS, and Document represents the output of the action performed in the user interface. These elements are bound to the parameters of the Command parameterized class. The following figure shows the parameterized collaboration of the command pattern, which provides abstractions with the structure and behavior to carry out a particular operation.

The following figure shows the class diagram depicting the structural aspects of the command design pattern.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

12/24

7/21/12

KIT2 OOAD-UML-II

The preceding figure depicts the class diagram that shows the association from the C l i e n tclass to the R e c e i v e rclass. The C l i e n tclass uses the a d d C o m m n d ( )method of the I n v o k e rclass, which returns the object of the C o m m a n dclass. The e x e c u t e ( )method further delegates the calls to the classes, the parameters of which are passed in the e x e c u t e ( )method. You can also depict the Command pattern by using sequence diagram to depict the behavioral aspect. The following figure shows the behavioral aspects of command design pattern.

Associating Patterns with Tiered Architecture


Design patterns can be implemented in tiered applications, which are developed to support companies in their business operations. Applications take data as input, process the data based on business rules, and provide data or information as output. As a result, applications can be divided in three tiers: Presentation tier: Takes data as input. Business tier: Implements the business logic and operations that need to be performed on the input data. Data tier: Manages the storage and retrieval of data. The following table lists the design patterns and the tiers on which they are generally implemented. Pattern Facade Builder
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

Tier Business Tier Business Tier


13/24

7/21/12

KIT2 OOAD-UML-II

Singleton Composite Decorator Chain of Responsibility Command Observer

Business Tier Presentation Tier Presentation Tier Business Tier Business Tier Presentation Tier

Design Patterns and their Corresponding Tiers


Mapping Design to Code
Using CASE tools, such as Rational Rose, Jude, and Visio, you can automatically generate code from class and communication diagrams. CASE tools use various thumb rules and heuristic principles to generate code from the designed model. You need to optimize the code generated using CASE tools according to the operating system. The tools such as, Microsoft Visio and Rational Rose support reverse engineering that enables you to generate design models from the code that may be modified during the course of implementation and testing phases of the project. You can also reverse engineer the code into the design model to measure the deviation from the designed model after the code is modified.

Note

The end goal of Object Oriented Analysis and Design (OOAD) is to generate code from the design model.

Generating Code from a Design Model


You focus on the class diagrams and communication diagrams while generating code because code is generated from class methods and variables.

Note

Generating code from a design model is a translation process which is incomplete because you cannot provide specification of data members and definition of methods in a model.

A class diagram contains the name, super class name, methods, and attributes of classes. Using the CASE tool, you can derive code from each class. For example, you may have a class, Product with attributes, P r o d u c t I D ,P r o d u c t N a m e , and P r i c eand methods, g e t P r i c e ( ) and s e t P r i c e ( ) , as shown in the
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 14/24

7/21/12

KIT2 OOAD-UML-II

following figure.

Using the CASE tool, a public class, P r o d u c t , is generated with a default constructor. The definition of constructor is not provided. The attributes, P r o d u c t I dand p r o d u c t N a m e , are defined in the P r o d u c t class. The declaration of the methods, g e t P r i c e ( ) and s e t P r i c e ( ) , is also provided in the class. The following code shows the C# class definition, derived from the class diagram, for the P r o d u c t class.

p u b l i cc l a s sP r o d u c t{ p r i v a t ei n tP r o d u c t I d ; p r i v a t es t r i n gP r o d u c t N a m e ; p r i v a t ei n tP r i c e ; p u b l i cv o i dg e t P r i c e ( ){ } p u b l i cv o i ds e t P r i c e ( ){ } }
A reference attribute of a class refers to the object of another class and defines the association relationship of the class with another class. Consider that a class, P r o d u c t L i s t has an association relationship with another class, P r o d u c t . The P r o d u c t L i s tclass displays the list of the products using the objects of the P r o d u c tclass. The following figure shows the association relationship between the P r o d u c t L i s tand P r o d u c tclasses.

In the preceding figure, the a d d P r o d u c t ( ) method of the P r o d u c t L i s tclass uses the object of the P r o d u c tclass to add the products in the list. Therefore, a reference attribute should be defined in the
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 15/24

7/21/12

KIT2 OOAD-UML-II

P r o d u c t L i s tclass.

If the reference attribute is included in the P r o d u c t L i s t class, the following code

would be generated.

p u b l i cc l a s sP r o d u c t L i s t{ p r i v a t ei n tQ u a n t i t y ; p u b l i cv o i da d d P r o d u c t ( P r o d u c tp ){ } }
You can also use the role name as the name of the reference attribute when you define a class. For example, the role name describing the association relationship between the P r o d u c t and P r o d u c t L i s t classes can be Contains, as shown in the following figure.

The following code shows the P r o d u c t L i s tclass definition with the role name as reference attribute.

p u b l i cc l a s sP r o d u c t L i s t{ p r i v a t ei n tQ u a n t i t y ; p r i v a t eP r o d u c tC o n t a i n s ; p u b l i cv o i da d d P r o d u c t ( P r o d u c tp ){ } }
You can also use communication diagrams to generate code. The messages sent to various objects in the communication diagrams are useful in identifying and generating additional methods that are not specified in the class diagram. If a message is sent to an object, then the object should have a method corresponding to the received message. If the method corresponding to the message is not present in the class, then you should declare the method in the class diagram.

Refactoring Design
Refactoring design is a technique for restructuring internal structure of the existing code without changing its external behavior. Refactoring alters the class model without altering the architecture of a system. In other words, the existing interfaces provided by the classes remain the same but their implementation changes.
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 16/24

7/21/12

KIT2 OOAD-UML-II

The factors that may lead to refactoring are: Identification of new methods in the implementation phase due to the platform on which the design is implemented The methods that are derived from the communication diagram are not present in the class diagram For example, in the P r i c e class, a new method, e n t e r I t e m (i n tu p c ,i n tq t y ) is identified because of communication diagram. This method contains another method, i s N e w S a l e ( ) and, therefore, i s N e w S a l e ( ) method is also defined during implementation. As a result, e n t e r I t e m (i n tu p c ,i n t q t y ) and i s N e w S a l e ( ) methods need to be reflected in the corresponding class diagram. If the definition of the i s N e w S a l e ( ) method includes another attribute or method, such as i s C o m p l e t e ( ) , then you need to reflect even these methods in the class diagram.

Applying Session Facade


A session facade is a design pattern that is used to create enterprise applications, such as applications that use Enterprise Java Beans (EJB). In an enterprise application, a session facade pattern is defined as a highlevel business component such as a session bean. The high-level business component defines the complete interaction among low-level business components, such as entity beans. The session facade pattern acts as an interface to the low-level business components. It decouples lowlevel business components from one another, making the design of enterprise applications more reusable and manageable. When a client needs to access multiple server side objects in an enterprise application scenario, you can wrap all the client calls into one session bean so that clients have one interface to access the session bean. Consider the enterprise application scenario of a bank where a bank customer with two saving accounts wants to transfer money from one account to another. In this scenario, the client application on the Web needs to: 1. 2. 3. 4. Authorize the bank customer. Check the status of the two accounts. Validate the balance of the first account. Transfer the cash to the second account.

It is clear from the above series of actions that multiple server-side objects need to be accessed to transfer money from one account to another. If each server-side object resides in a separate entity bean, then the client application needs to invoke each entity bean separately.

Note

Entity beans implement the business objects in a J2EE application. The business objects represent the basic entities in an application on which various operations can be performed.
17/24

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

7/21/12

KIT2 OOAD-UML-II

The following figure shows how a client application invokes multiple server-side objects such as S e s s i o n B e a n ,E n t i t yB e a n 1 , and E n t i t yB e a n 2in the absence of a session facade pattern.

The separate invocation of each server-side object by a client significantly increases the network traffic. This results in performance degradation due to an increase in the latency of transferring client invocations. When you introduce a session bean as a session facade in between the client invocation and the various server-side objects, you restrict clients from directly accessing the various server-side objects. This significantly reduces network traffic and decreases the latency of transferring client invocations. The introduction of a session facade results in the encapsulation of server-side objects and does not allow clients to directly access them. A session facade abstracts the interaction among the session bean as a session facade and the various server-side objects and acts as a service layer that exposes only the interfaces that are required for the interactions. The following figure shows how a client interacts with server-side components such as S e s s i o nB e a n , E n t i t yB e a n 1 , and E n t i t yB e a n 2in the presence of a session facade.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

18/24

7/21/12

KIT2 OOAD-UML-II

Just a minute:

Which of the following structural design patterns simplifies the software development by providing simplified interface? Proxy Decorator Composite Facade

1. 2. 3. 4.

Answer:

4. Faade

Activity: Generate C# Code for Bank ATM System

Problem Statement
Generate the C# code of the Bank ATM model using Visio. Prerequisite : To perform this activity, you will need the BANK_ATM.vsd file, which you created in the activity Architectural Modeling for the Bank ATM System of Chapter 9.

Solution
1. Select StartAll ProgramsMicrosoft OfficeMicrosoft Office Visio for Enterprise Architects . 2. Open the Bank_ATM file. The Bank_ATM model appears in Microsoft Visio window. 3. Select UMLCode Generate from the menu bar. The Generate dialog box appears, as shown in the following figure.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

19/24

7/21/12

KIT2 OOAD-UML-II

4. Select C# from the Target language drop-down list. 5. Click the Browse button. The Browse For Folder dialog box appears, as shown in the following figure.

6. Select the folder in which you want to store the Visual Studio project from the Select a Directory section, as shown in the following figure.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

20/24

7/21/12

KIT2 OOAD-UML-II

Note

You can optionally create a new folder to store the Visual Studio project. To create a new folder, select the location where you want to create a new folder from the Select a Directory section. Click the Make New Folder button in the Browse For Folder dialog box. A new folder is created in the Select a Directory list. Rename the folder as per your requirement.

7. Click the OK button. The path of the folder appears in the Location text box in the Generate dialog box. 8. Select the Add Classes to Visual Studio Project check box in the Project section. 9. Type InfoSuper in the Name text box in the Project section. 10. Select the Class Library template from the Template drop down list in the Project section. 11. Type InfoSuper in the Name text box in the Solution section and select the Create directory for solution check box. 12. Select the Static Model check box in the Select classes for code generation section, as shown in the following figure.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

21/24

7/21/12

KIT2 OOAD-UML-II

13. Click the OK button. A Visual Studio project is created in the Code\InfoSuper folder. The project contains the code for all the classes in the UML model.

Note

For generating the code, the Generate utility may need to create some subdirectories. Therefore, the Generate utility may show a message box displaying the message The directory <<directory path>> could not be found. Would you like to create this directory? Click the Yes button in the message box to create the new directory.

14. Close Microsoft Visio. You can view the generated code in the folder that you specified for storing the Visual Studio project.

Summary
In this chapter, you learned that: A framework is a unit of architecture that provides a reusable and extensible solution to a group of problems. A pattern is a named problem-solution pair that enables you to apply standard solution to reoccurring problems in the design and implementation phases of SDLC. GRASP represents a set of patterns that are used to assign responsibilities to different identified classes. A design pattern provides standard approach for performing tasks so that the solution
www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm 22/24

7/21/12

KIT2 OOAD-UML-II

becomes reusable and maintainable. The three types of GoF are: Structural Creational Behavioral Creational patterns deal with the creation and lifetime of objects of other classes. Structural patters deal with the structure of the system. Behavioral patterns deal with the behavior of the system. Code generation is the process of deriving code from the design model. Refactoring design is a technique for restructuring internal structure of the existing code without changing its external behavior.

Exercises

Exercise 1
Blue Valley Consulting Inc. has decided to implement the data entry system, which includes providing functionality to enter the information of the disposed property, for Real Estate Management System. The details of the module, which is required to implement the data entry system, are:

Module: PROPERTY DISPOSAL


This module will enable the Operations Manager to record the information of the property, which will be disposed off. The module will allow the Operations Manager to enter a termination date when the property will be disposed off along with any additional information about the disposal process, if any. The system will not accept any changes after the termination date for the property that will be disposed off. Identify a design pattern for the requirements provided in the Real Estate Management System.

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

23/24

7/21/12

KIT2 OOAD-UML-II

Reference Links

Understanding Frameworks and Patterns


Reference Reading: Books Applying UML and patterns by Craig Larman Publisher: Prentice Hall Reference Reading: URLs http://smartdraw.com/resources/centers/uml/uml6.htm#whatdepl

M apping Design to Code


Reference Reading: Books Applying UML and patterns by Craig Larman Publisher: Prentice Hall Reference Reading: URLs http://smartdraw.com/resources/centers/uml/uml6.htm#whatdepl

www.niitstudent.com/india/Content/063OUML2S1/OEBPS/08_ch10.htm

24/24

Das könnte Ihnen auch gefallen