Design Patterns Examples Prof dr Angelina Njegu Content
















![class Program { static void Main(string[] args) { Drink. Maker maker; IDrink. Builder builder; class Program { static void Main(string[] args) { Drink. Maker maker; IDrink. Builder builder;](https://slidetodoc.com/presentation_image_h2/85911875b1909f26ff2bc9cd27a0e002/image-17.jpg)










![Director and Client class Program { static void Main(string[] args) { // create object Director and Client class Program { static void Main(string[] args) { // create object](https://slidetodoc.com/presentation_image_h2/85911875b1909f26ff2bc9cd27a0e002/image-28.jpg)
- Slides: 28
Design Patterns Examples Prof. dr Angelina Njeguš
Content Example 1: Red dress Example 2: Weather. Data subscribers Example 3: Vending machine Example 4: PDF or XML Document Builder Example 5: Laptop manufacturing company
Example 1: Red dress • Lizza decides to get a new dress for her 18 th birthday party. She goes online to her favourite website fashionworld. com and selects a perfect Red dress. But oops its out of stock. • She tries to find another dress but there is nothing that can replace it. So, she goes back to the dress, registers her email id and click on "Notify Me" button.
Which pattern would you apply? • Similarly, there could be other users waiting for the Red Dress. As shown below user 1, user 2 and user 3 have registered for notification just like Lizza. So, they will be notified once dress is available. • User 4 is not interested in Red Dress so, she never registered by clicking "Notify Me" button. No email will be sent to her when the dress is 'In Stock' • If user 1 finds another dress and is no longer interested in the Red Dress. Then user 1 can get the registration removed. In this case she will no longer be informed when dress is available.
Lets dive into Observer Pattern • All the users who registered for notification in the above case are Observers (user 1, user 2, user 3). • The subject of their observation is Red Dress. So, Red Dress is Observable. • There is one Red dress but many users interested in it.
What functionalities will be required in an Observable?
How will Observer look like?
Observer Pattern Class Diagram
Observer Design Pattern Overview • Observer Pattern is a behavioral type design pattern. • According to Go. F, the observer design pattern is: The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. • An Object which is being watched by other objects is called Subject and Object which is watching state of the subject is called Observer. • In this pattern, there are many observers (objects) which are observing a subject (object). • Observers are basically interested and want to be notified when there is a change made inside the subject. Hence, they register themselves with the subject. When they lose interest in the subject they simply unregistered from the subject. Sometimes this model is also referred to as the Publisher-Subscriber model.
Example 2: Weather. Data subscribers • The diagram shows how Users are subscribed to Weather data feed. When there is a change in weather it will call the update method with the message and subscribers will receive the message
Example 3: Vending machine • Vending machine is drink preparing machine, therefore at the end product is a drink, which may be either tea or coffee. • First of all our end product drink can be represented by the below class Drink { string _label; public Drink(string label) { this. label = label; } public override string To. String() { return this. label; } }
Define Builder class • Now we will define an interface, which represent the process for building a drink. • A tea can be made by mixing hot water and tea powder, coffee can be made by mixing hot water and coffee powder. interface IDrink. Builder { void Add. Water(); void Add. Powder(); Drink { get; } }
Concrete Builder: Tea • Now we will create a concrete tea builder as below. class Tea. Builder : IDrink. Builder { Drink drink; public Tea. Builder() { drink = new Drink("Tea"); } public void Add. Water() { } public void Add. Powder() { //Tea Powder } public Drink { get { return drink; } } }
Concrete Builder: Coffee • Then a concrete coffee builder. class Coffee. Builder : IDrink. Builder { Drink drink; public Coffee. Builder() { drink = new Drink("Coffee"); } public void Add. Water() { } public void Add. Powder() { // Coffee powder } public Drink { get { return drink; } } }
Director • Now we will design our Director, which can be reused to build a tea or coffee. class Drink. Maker { public void Make. Drink(IDrink. Builder builder) { builder. Add. Water(); builder. Add. Powder(); } }
class Program { static void Main(string[] args) { Drink. Maker maker; IDrink. Builder builder; • You can use the class in your main. Console. Write. Line("Enter 'T' for Tea and 'C' for coffee. "); maker = new Drink. Maker(); while (true) { string input = Console. Read. Line(); if (input == "T") { builder = new Tea. Builder(); maker. Make. Drink(builder); Console. Write. Line(builder. Drink. To. String() + " is ready. "); Console. Read. Line(); } else if (input == "C") { builder = new Coffee. Builder(); maker. Make. Drink(builder); Console. Write. Line(builder. Drink. To. String() + " is ready. "); Console. Read. Line(); } else { Environment. Exit(0); } } }
Why Do We Need the Builder Pattern? • The Builder pattern is a creational pattern that can be used to create and configure objects. • The Builder pattern says we have to reuse the mechanism to construct a complex object.
Example 4: PDF or XML Document Builder
Define Document Builder, and Product //Abstract class - Doc. Builder. java public abstract class Doc. Builder{ public abstract void create. Document(); public abstract void create. Text(); public abstract void create. Images(); public abstract Document get. Document(); } //Interface - Document. java public interface Document{ } //Class PDFDocument. java public class PDFDocument implements Document{ //attributes for holding the PDFDocument } //Class XMLDocument. java public class XMLDocument implements Document{ //attributes for holding the XMLDocument }
Define Concrete Builder //PDFDoc. Builder. java //XMLDoc. Builder. java public class PDFDoc. Builder extends Doc. Builder{ public class XMLDoc. Builder extends Doc. Builder{ private PDFDocument pdf. Doc; private XMLDocument xml. Doc; public void create. Document(){ this. pdf. Doc=new PDFDocument(); this. xml. Doc=new XMLDocument(); } } public void create. Text(){ System. out. println("Creating text for PDF Document. "); System. out. println("Creating text for XML Document. "); } } public void create. Images(){ System. out. println("Creating images for PDF Document. "); System. out. println("Creating images for XML Document. "); } } public Document get. Document(){ System. out. println("Fetching PDF Document. "); return this. pdf. Doc; return this. xml. Doc; } }
Define Director and Client //Doc. Creation. Engine. java public class Doc. Creation. Engine{ public void generate. Document(Doc. Builder builder){ builder. create. Document(); builder. create. Text(); builder. create. Images(); } } //Client. java public class Client{ public static void main(String args[]){ Doc. Creation. Engine engine=new Doc. Creation. Engine(); //Creating PDF Document PDFDoc. Builder pdf. Doc. Builder=new PDFDoc. Builder(); engine. generate. Document(pdf. Doc. Builder); PDFDocument pdf. Document=(PDFDocument)pdf. Doc. Builder. get. Document(); //Creating XML Document XMLDoc. Builder xml. Doc. Builder=new XMLDoc. Builder(); engine. generate. Document(xml. Doc. Builder); XMLDocument xml. Document=(XMLDocument)xml. Doc. Builder. get. Document(); } }
Example 5: Laptop manufacturing company Problem Statement § There is a laptop manufacturing company which manufactures two types of laptop as follows: • Gaming Laptop • Normal Laptop § Now the accessories integrated in both the laptop are different, so different builders are required to manufacture these laptops.
Class diagram
namespace Builder. Pattern. Demo { // 'Product' class Laptop { public string Model. Number { get; set; } public string Display { get; set; } Builder Design Pattern in C# – class Laptop public string RAM { get; set; } public string Graphics. Card { get; set; } public string Touch. Screen { get; set; } public void Print. Details () { Console. Write. Line("------ Laptop Details --------"); Console. Write. Line("Model Number: {0}", Model. Number); Console. Write. Line("Display: {0}", Display); Console. Write. Line("RAM: {0}", RAM); Console. Write. Line("Graphics Card: {0}", Graphics. Card); Console. Write. Line("Touch Screen: {0}", Touch. Screen); } }
Builder // 'Builder' interface ILaptop. Builder { void add. Model. Number(); void add. Display(); void add. RAM(); void add. Graphics. Card(); void add. Touch. Screen(); Laptop get. Laptop(); }
// 'Concrete. Builder' class - implements 'Builder' interface class Gaming. Laptop. Builder : ILaptop. Builder { Laptop laptop = new Laptop(); public void add. Model. Number() { laptop. Model. Number = "Gaming 1001"; } public void add. Display() { laptop. Display = "Full HD display"; } public void add. RAM() { laptop. RAM = "16 GB"; } public void add. Graphics. Card() { laptop. Graphics. Card = "Nvidia Ge. Force"; } public void add. Touch. Screen() { laptop. Touch. Screen = "Yes"; } public Laptop get. Laptop() { return laptop; } } // 'Concrete. Builder' class - implements 'Builder' interface class Normal. Laptop. Builder : ILaptop. Builder { Laptop laptop = new Laptop(); public void add. Model. Number() { laptop. Model. Number = "Normal 1001"; } public void add. Display() { laptop. Display = "SD Display"; } public void add. RAM() { laptop. RAM = "2 GB"; } public void add. Graphics. Card() { laptop. Graphics. Card = "No graphics card"; } public void add. Touch. Screen() { laptop. Touch. Screen = "NO"; } public Laptop get. Laptop() { return laptop; } }
Director and Client class Program { static void Main(string[] args) { // create object of manufacturer Laptop. Manufacturer laptop. Manufacturer = new Laptop. Manufacturer(); // build Gaming Laptop // 'Director' class ILaptop. Builder gaming. Laptop. Builder = new Gaming. Laptop. Builder(); class Laptop. Manufacturer laptop. Manufacturer. Build. Laptop(gaming. Laptop. Builder); { Laptop Gaming. Laptop = gaming. Laptop. Builder. get. Laptop(); public void Build. Laptop (ILaptop. Builder laptop. Builder) // print details { Console. Write. Line("Gaming Laptop Object: "); laptop. Builder. add. Model. Number(); Gaming. Laptop. Print. Details(); } } laptop. Builder. add. Display(); laptop. Builder. add. RAM(); laptop. Builder. add. Graphics. Card(); laptop. Builder. add. Touch. Screen(); // build normal laptop ILaptop. Builder normal. Laptop. Builder = new Normal. Laptop. Builder(); laptop. Manufacturer. Build. Laptop(normal. Laptop. Builder); Laptop Normal. Laptop = normal. Laptop. Builder. get. Laptop(); // print details Console. Write. Line("n. Normal Laptop Object: "); Normal. Laptop. Print. Details(); } } } Console. Read. Line();