Design Patterns Lecture Notes PDF
Document Details
Uploaded by EncouragingLimeTree6735
Mansoura University
Shahenda El Kholy
Tags
Summary
These lecture notes cover design patterns in software engineering. They discuss various types of design patterns, including creational, structural, and behavioral patterns. The notes also provide examples and diagrams.
Full Transcript
Design patterns Instructor Name: Shahenda El Kholy Information System Dept , FCI Mansoura University What is a Design Pattern? Software design pattern – Reusable solutions to common problems in software design – Problem / solution pairs within a given context – A templ...
Design patterns Instructor Name: Shahenda El Kholy Information System Dept , FCI Mansoura University What is a Design Pattern? Software design pattern – Reusable solutions to common problems in software design – Problem / solution pairs within a given context – A template or recipe for solving certain software design problems GoF patterns – Classical object-oriented design patterns book by Gama, Helm, Johnson, Vlissides 1995 – The "Gang of Four Book" 2 Elements of Design Patterns Design patterns are described by a few essential elements: – Pattern Name Increases vocabulary of designers – Problem Intent, context, when to apply – Solution UML-like structure, abstract code – Consequences Results and tradeoffs 3 3 main types of design patterns Creational Structural Behavioral Patterns Patterns Patterns Chain of Responsibility Adapter Singleton Command Composite Interpreter Factory Iterator Decorator Mediator Abstract Factory Bridge Memento Builder Façade Observer State Prototype Flyweight Strategy Proxy Template Visitor Type 1: Creational Patterns Way to create objects while hiding logic Does not use the “new” operator Offers more flexibility in deciding which objects need to be created for a given case Solution to instantiate an object in the best possible way for the situation Type 2: Structural patterns Provide different ways to create a class structure Using inheritance to compose interfaces & define ways to create objects to get new functionality E.g. using inheritance & composition to create a large object from small objects Type 3 : Behavioral patterns Concerned with communication & better interaction between objects Provide loose coupling and flexibility to extend easily Creational Patterns Deal with object creation mechanisms Trying to create objects in a manner suitable to the situation – Hiding how instances of these concrete classes are created and combined 1- Singleton Pattern The Singleton class is a class that is supposed to have only one (single) instance – Usually created on demand (lazy loading) Best used in shared resource access like files, database , hardware,… Threading Needs Additional synchronization code to prevent problems of singleton. Example scenarios where Singleton can be used Service Proxies: In an application invoking a service aka API is an extensive operation. And creating a Service client itself time consuming process. By having a Service proxy as a Singleton this overhead can be reduced. Facades: Database connections are another one example where Singleton can be used to produce better performance and synchronization. Caching: Data fetching is a time taking process whereas caching required data in the application memory avoids DB calls. Singleton can be used here to handle the caching with thread 2- Factory Pattern In object-oriented programming, a Factory is an object for creating other objects (alternative constructor) Factory Pattern UML When to user Factory Pattern? Factory pattern should be used when: – a framework delegate the creation of objects derived from a common superclass to the factory. – we need flexibility in adding new types of objects that must be created by the class – Banking Systems 3- Builder Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations. The participants classes in builder pattern: Director constructs an object using the Builder interface Product: represents the complex object under construction. Concrete Builder builds the product's internal representation and defines the process by which it's assembled – includes classes that define the constituent parts, including interfaces for assembling the parts into the final result Builder : specifies an abstract interface for creating parts of a Product object ConcreteBuilder :constructs and assembles parts of the product by implementing the Builder interface – defines and keeps track of the representation it creates – provides an interface for retrieving the product Builder Example 1- Fast Food This pattern is used by fast food restaurants to construct children's meals. Children's meals typically consist of: – a main item, – a side item, – a drink, – and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children's meal, but the construction process is the same. Builder Example 1- Fast Food Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants typical meal could be a burger and a cold drink ,Burger could be either a : – Veg Burger – or Chicken Burger and Burger will be packed by a wrapper. Cold drink could be either : – a coke – or pepsi Cold Drink will be packed in a bottle. Builder Example 1- Fast Food