Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Software Design Laboratory Subject Code: 13MCA56 I.A Marks : 50 Hours/Week : 3 Exam Hours: 03 Total Hours : 42 Exam Marks: 50 The student has to draw the necessary UML diagrams using any suitable UML Drawing Tool and implement in Java OR C++ OR C# a program to demonstrate the Design Pattern specified by the Examiner. For Analysis and Design models, diagrams such as Use-case, Class Diagram, Sequence/Collaboration Diagram should be drawn with suitable scenario, activity diagram, component diagram & deployment diagram. The Design Pattern is allotted based on lots from the following list: 1) Expert 2) Controller 3) Publisher-Subscriber 4) Command 5) Forward-Receive 6) Client-Dispatcher 7) Proxy 8) Façade 9) Polymorphism 10) Whole-Part 11) Master-Slave Note: Any Supporting Tool may be used. 1) Expert Pattern Problem: What is the most basic principle by which responsibilities are assigned in object-oriented design? Solution: Assign a responsibility to the class that has the information necessary to fulfill the responsibility. Structure: Client Information Expert Participants Classes: InformationExpert: This is the class, which has the information to fulfill the responsibility. We assign the responsibility to this class to accomplish the behavior. Client: The class which will be using the Information Expert class. Example: The Point of Sale (POS) is a computerized application used to record sales and handle payments. In this system POS will be calculating the total sales at any given point of time. Use Case Diagram : System SaleLineItem <<include>> Sale <<include>> SaleCounter Product Sequence Diagram: : SaleLineItem : Sale : SaleCounter 1 : getTotal() 2 : getQuantity() 3 4 : getPrice() 5 6 Colloboration Diagram: : Product : SaleLineItem : Product 4 : getPrice() 2 : getQuantity() 3 5 : Sale 6 1 : getTotal() : SaleCounter Pattern Instance: Structure Class Client Information Expert Class Diagram: Instance Class SaleCounter Sale POS SaleLineItem +SaleLineItem -quantity: int = 100 +getQuantity(): int SaleCounter +SaleCounter +Sale Sale -total: float +main() +getTotal(): float +ProductDescription Product -price: float = 15.50 +getPrice(): float Java Code: SaleCounter.java package POS; public class SaleCounter { public Sale Sale; public static void main(String []args) { Sale s= new Sale(); System.out.println("Total sale is " + s.getTotal()); } } Sale.java package POS; public class Sale { private float total; public SaleCounter SaleCounter; public Product ProductDescription; public SaleLineItem SaleLineItem; public float getTotal() { Product pdt = new Product(); SaleLineItem sli = new SaleLineItem(); total = pdt.getPrice() * sli.getQuantity(); return total; } } SaleLineItem.java package POS; public class SaleLineItem { private int quantity = 100; public int getQuantity() { return quantity; } } Product.java package POS; public class Product { private float price = 15.50f; public float getPrice() { return price; } } 2) Controller Pattern Problem: The presentation-tier request handling mechanism must control and coordinate processing of each user across multiple requests. Such control mechanisms may be managed in either a centralized or decentralized manner. Solution: Use a controller as the initial point of contact for handling a request. The controller manages the handling of the request, including invoking security services such as authentication and authorization, delegating business processing, managing the choice of an appropriate view, handling errors, and managing the selection of content creation strategies. Structure: Client Controller Dispatcher View 1 : Send Request() 2 : Delegate Request() 3 : Forward Request() 4 : Send Request() 5 : Forward Request() 6 : Send Request() 7 : Process Request() Example: The Point of Sale (POS) is a computerized application used to record sales and handle payments. In this system POS will be calculating the total sales at any given point of time. Now the Controller Pattern suggests, that events coming from UI layer should not be directly accessing the expert classes. There should be a Controller class to control these events. In this example, we will use a SaleController class that will be receiving the events from UI and forward it to the Sale class. There will be one View class to decide how the output will be displayed. In this example we are using Console for taking input, and displaying the output. UseCaseDiagram: Helper System Client Execute Sale Total SequenceDiagram: Sale SaleController : Client 1 : Enter the Req Input() 2 : execute() 3 : getTotal() 4 5 : display() Activity Diagram: View Enter the input of Quantity & Price System Calculate Total System Displays Total Pattern Instance: Structure Class Client Controller Dispatcher View Helper Class Diagram: Instance Class Client SaleController View Sale Controller View Client +View +main() +display() 1 +Client 1 1 +SaleController +SaleController SaleController 1 +Sale sale +SaleController +View view +execute() 1 +Sale 1 Sale +getTotal(): float Java Code: Client.java package Controller; import java.util.Scanner; public class Client { public static void main(String[] args) { Scanner scr=new Scanner(System.in); SaleController tc = new SaleController(); System.out.println("Enter the Quantity: "); int quantity= scr.nextInt(); System.out.println("Enter the Price: "); float price=scr.nextFloat(); tc.execute(quantity, price); scr.close(); } } SaleController.java package Controller; public class SaleController { public Sale sale = new Sale(); public View view = new View(); public void execute(int quantity,float price){ float result = sale.getTotal(quantity, price); view.display(result); } } View.java package Controller; public class View { public void display(float total) { System.out.println("The Total Sale is: " + total); } } Sale.java package Controller; public class Sale { public float getTotal(int quantity, float price) { return quantity * price; } } 3) Publisher Pattern Intent: Define a one-to-many dependency between object so that when one object changes state, all its dependents are notified and updated automatically. Also Known As: Dependents, Model-View, Observer Applicability: Use the Publisher-Subscriber pattern in any of the following situations: > When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently. > When a change to one object requires changing others > When an object should be able to notify other objects without making assumptions about those objects Structure: Subject +Attach(o: Observer) +Detach(o: Observer) +Notify() ConcreteSubject +subject Observer +Update() ConcreteObserver Participants Classes: Subject: Keeps track of its observers. Provides an interface for attaching and detaching Observer objects Observer: Defines an interface for update notification Concrete Subject: The object being observed. Stores state of interest to Concrete Observer objects. Sends a notification to its observers when its state changes Concrete Observer: The observing object - stores state that should stay consistent with the subject. Implements the Observer update interface to keep its state consistent with the subject. Implements the Observer update interface to keep its state consistent with the participants. Example: Weather Information system The weather Station will be broadcasting the current weather conditions like temperature, humidity and biometric pressure. We have to create an application which receives the weather conditions and displays them in different forms. (i.e. displaying the current weather conditions, displaying weather statistics). All displays will be updating the weather conditions in real time, as and when the new conditions are recorded. Use Case Diagram: System Forecast Display Weather Data User Weather Station CurrentCondition Display Sequence Diagram: : Weather Data : CurrentCondition Display : Forecast Display : Weather Station : User 1 : setMeasurements NoChange() 2 : Display Current Condition() 3 : display() 4 : ChangeInWeather() 5 : DisplayForecastData() 6 : forecast data() Class Diagram: ForecastDisplay -currentPressure: float -lastPressure: float +ForecastDisplay(Observable) +update(Observable, Object) +display() WeatherData -temperature: float -pressure: float -humidity: float +getTemperature(): float +getPressure(): float +getHumidity(): float +measurementschanged(): float +setMeasurements(theTemperature: float, thePressure: float, theHumidity: float): float CurrentConditionDisplay -temperature: float -humidity: float +CurrentConditionDisply(Observable) +update(Observable, Object) +display() Java Code: WeatherData.java import java.util.Observable; public class WeatherData extends Observable { private float temperature; public float getTemperature() { return temperature; } private float pressure; public float getPressure() { return pressure; } private float humidity; public float getHumidity() { return humidity; } WeatherStation +main(Strings) public void measurementschanged(){ setChanged(); notifyObservers(); } public void setMeasurements(float theTemperature, float theHumidity, float thePressure){ temperature=theTemperature; pressure=thePressure; humidity=theHumidity; measurementschanged(); } } ForeCastDisplay.java: import java.util.Observable; import java.util.Observer; public class ForeCastDisplay implements Observer { private float currentPressure = 29.92f; private float lastPressure; public ForeCastDisplay(Observable observable) { observable.addObserver(this); } public void update(Observable observable, Object arg) { if (observable instanceof WeatherData) { WeatherData weatherData = (WeatherData)observable; lastPressure = currentPressure; currentPressure = weatherData.getPressure(); display(); } } public void display() { System.out.print("Forecast: "); if (currentPressure > lastPressure) { System.out.println("Improving weather on the way!"); } else if (currentPressure == lastPressure) { System.out.println("More of the same"); } else if (currentPressure < lastPressure) { System.out.println("Watch out for cooler, rainy weather"); } } } CurrentConditionDisplay.java: import java.util.Observable; import java.util.Observer; public class CurrentConditionDisplay implements Observer { private float temperature; private float humidity; Observable observable; public CurrentConditionDisplay(Observable observable) { this.observable = observable; observable.addObserver(this); } public void update(Observable obs, Object arg) { if (obs instanceof WeatherData) { WeatherData weatherData = (WeatherData)obs; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); } } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } } The Client: public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionDisplay currentConditions CurrentConditionDisplay(weatherData); ForeCastDisplay forecastDisplay = ForeCastDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); } } = new new