Software Architecture and Design Lecture Notes PDF
Document Details
Uploaded by EncouragingLimeTree6735
Mansoura University
Shahenda El Kholy
Tags
Summary
These lecture notes cover software architecture and design, focusing on various design patterns. The notes include the Observer, State, Strategy, and Adapter patterns, providing definitions and examples. Topics are presented in a structured format, suitable for undergraduate-level learning.
Full Transcript
Software Architecture and Design Instructor Name: Shahenda El Kholy Information System Dept , FCI Mansoura University 2 Structural & Behavioral Patterns 3 Behavioral Patterns Behavioral Patterns ( Observer Pattern ) 4 Observer Pattern : According to...
Software Architecture and Design Instructor Name: Shahenda El Kholy Information System Dept , FCI Mansoura University 2 Structural & Behavioral Patterns 3 Behavioral Patterns Behavioral Patterns ( Observer Pattern ) 4 Observer Pattern : According to GoF, observer design pattern intent is; ❑ Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. ❑ Subject contains a list of observers to notify of any change in its state. ❑ Subject also contain a method to notify all the observers of any change and either it can send the update while notifying the observer or it can provide another method to get the update. Behavioral Patterns ( Observer Pattern ) 5 Observer should have a method to set the object to watch and another method that will be used by Subject to notify them of any updates. ❑ Model-View-Controller (MVC) frameworks also use Observer pattern where Model is the Subject and Views are observers that can register to get notified of any change to the model. ❑ real-world example of observer pattern can be any social media platform such as Facebook or twitter. When a person updates his status – all his followers gets the notification. A follower can follow or unfollow another person at any point of time. Behavioral Patterns ( Observer Pattern ) 6 The classes and objects participating in this pattern are: Subject knows its observers. Any number of Observer objects may observe a subject provides an interface for attaching and detaching Observer objects. Concrete Subject stores state of interest to Concrete Observer sends a notification to its observers when its state changes Observer defines an updating interface for objects that should be notified of changes in a subject. Concrete Observer maintains a reference to a Concrete Subject object stores state that should stay consistent with the subject's implements the Observer updating interface to keep its state consistent with the subject's Behavioral Patterns (State Pattern ) 7 State Pattern : allow an object to change its behavior when its internal state changes use State pattern when: an object’s behavior depends on its state operations have large conditional statements that depend on the object’s state (the state is usually represented by one or more enumerated constants) Behavioral Patterns (State Pattern ) 8 State Pattern Participants Context defines interface of interest to clients maintains reference to a ConcreteState subclass that defines the current state (one State at the time) State defines an interface for encapsulating the behavior associated with a particular state of the Context Concrete State subclasses each subclass implements a behavior associate with a state of the Context (by overriding methods in State) Behavioral Patterns (State Pattern ) 9 Example: TCP Connection A TCP Connection object can be in one of several different states Established Listening Closed Responds differently to requests depending on its current state Behavioral Patterns (Strategy Pattern ) 10 Strategy Pattern : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Create an abstract strategy class (or interface) and extend (or implement) it in numerous ways. Each subclass defines the same method names in different ways Behavioral Patterns (Strategy Pattern ) 11 Usages for Strategy Saving files in different formats, Word, ODT, RTF, HTML plain text. Compress files using different compression algorithms. Capture video using different video compression algorithms. Plot the same data using different formats (points, line chart, bar chart, etc.) Display calendars, with different holidays for different countries (Strategy classes are USA Holiday, Canada Holiday… 12 Structural Patterns Structural Patterns (Adapter Pattern ) 13 An Adapter Pattern says that just "converts the interface of a class into another interface that a client wants". An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected directly. Structural Patterns (Adapter Pattern ) 14 You can use the Adapter design pattern when you have to deal with different interfaces with similar behavior (which usually means classes with similar behavior but with different methods). Useful to Reuse existing classes from one project in another existing projects. An Adapter wraps an existing class with a new interface so it becomes compatible with the client’s interface. An example of it would be a class to connect to a Samsung TV and another one to connect to a Sony TV Structural Patterns (Adapter Pattern ) 15 Adapter Pattern Participants Target defines the domain-specific interface that Client uses. Adapter adapts the interface Adaptee to the Target interface. ExistingClass (Adaptee) defines an existing interface that needs adapting. Client collaborates with objects conforming to the Target interface. Example Imagine your drawing editor uses an internal structure for drawing graphics shapes that is different from how the UI for a “drawer” application asks for coordinates Structural Patterns ( Façade Pattern ) 16 Façade Pattern Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Structural Patterns ( Façade Pattern ) 17 The classes and objects participating in this pattern are: Facade knows which subsystem classes are responsible for a request. delegates client requests to appropriate subsystem objects. Subsystem classes implement subsystem functionality. handle work assigned by the Facade object. have no knowledge of the facade and keep no reference to it. Structural Patterns (Decorator Pattern ) 18 Decorator Pattern : Attach additional responsibilities to an object dynamically. ❑ Decorators implement the same interface as the component and add new functionality by forwarding requests to the component and potentially. ❑ adding its own behavior Defines a decorator class that holds a reference to a wrapped object (the component). Decorators provide a flexible alternative to subclassing for adding new functionality. Clients can stack decorators on a component to add functionalities dynamically. 19 THANK YOU