Download Implementing Design Patterns in Software Development and more Assignments International Law in PDF only on Docsity!
ASSIGNMENT 2 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 16: Advanced Programming Submission date 12 /08/2023 Date Received 1st submission 12 /08/ Re-submission Date Date Received 2nd submission Student Name MAIHUUNAM Student ID Bs Class PBIT17101 Assessor name MR. ON NHAT MINH Student declaration I certify that the assignment submission is entirely my work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice. Student’s signature Nam Grading grid P1 P2 P3 P4 M1 M2 D
❒ Summative Feedback: ❒ Resubmission Feedback: Grade: Assessor Signature: Date: Signature & Date:
II. Discuss a range of design patterns with relevant examples of creational, structural and behavioural
- I.Build applications derived from UML class diagrams. (P3)....................................................................
- The outcomes are identical to those below..........................................................................................
- pattern types. (P4)................................................................................................................................
- 1 Define design patterns......................................................................................................................
- a.Creational Patterns............................................................................................................................
- b.Structural Patterns:...........................................................................................................................
- c.Behavioural Patterns:.........................................................................................................................
- 1.1Creational Patterns:.........................................................................................................................
- 1.2Structural Patterns:..........................................................................................................................
- 1.3Behavioural Patterns:......................................................................................................................
- CONCLUSION........................................................................................................................................
- References............................................................................................................................................
- Figure 1 Diagram.........................................................................................................................................
- Figure 2 Public class car...............................................................................................................................
- Figure 3 Public car showroom.....................................................................................................................
- Figure 4 Public class Part.............................................................................................................................
- Figure 5 Public class Partstrore....................................................................................................................
- Figure 6 Menu Option..................................................................................................................................
- Figure 7 Menu.............................................................................................................................................
- Figure 8 displays..........................................................................................................................................
- Figure 9 class program...............................................................................................................................
- Figure 10 the rest.......................................................................................................................................
- Figure 11 The request of " add a car to showroom"..................................................................................
- Figure 12 the rest "add a part to store".....................................................................................................
- Figure 13 Result of"display car..................................................................................................................
- Figure 14 relist "display parts"...................................................................................................................
- Figure 15 Design Pattern............................................................................................................................
- Figure 16 Types of design pattern..............................................................................................................
- Figure 17 Builder pattern..........................................................................................................................
- Figure 18 Car Business...............................................................................................................................
- Figure 19 The output of the Builder pattern example's code....................................................................
- Figure 20 Adapter pattern.........................................................................................................................
- Figure 21 The output of the Adapter pattern example's code...................................................................
- Figure 22 Command..................................................................................................................................
- Figure 23 Diagram command.....................................................................................................................
- Figure 24 The output of the Adapter pattern example's code...................................................................
Table 1 Code for the Builder pattern example........................................................................................... 16 Table 2 Code for the Adapter pattern example........................................................................................ 20 Table 3 Code for the Command pattern example...................................................................................... 24
Introduction
In today's report, my proposal to create library administration software for the University of Greenwich, similar to scenario 1, will be implemented and put into use. I will go into great depth regarding each class in this report, as well as how the various components of the software relate to one another. Additionally, I'll put the code into practice in depth as well as precisely describe the program's direction
I. Build applications derived from UML class diagrams. (P3)
Scenario: I want to build a dealership where people can buy different automobile brands and have the toy accessories that go with each one attached. I use the spelling to record them because the item is relatively numerous and an import-export item. This manual technique gave me a little problem because I can't recall all the product names, qualities, etc. So I wrote a straightforward software to facilitate controlling the showroom. To be more precise, I'll develop an accessory inventory management system so I can keep track of the goods I currently sell and the preferences of my customers. Figure 1 diagram
- AddCar(Car car): This method adds a Car object to the inventory list by taking a Car object as an argument. The automobile is added to the list using the Add() function of the List class.
- DisplayInventory(): This function shows the showroom's inventory of available vehicles. Each automobile in the inventory list is iterated by using a for each loop. By gaining access to the corresponding attributes of the car object, it shows information on the brand, model, and price of each automobile. Figure 4 public class Part Three characteristics of the Part class are:
- Name: This property, which is of type string, denotes the part's name. It can save the part's name or unique identification.
- Category: This property, which is of type string, denotes the part's category. It can store the part's category or kind.
- Price: This decimal-type attribute, which reflects the part's cost, is present. It has storage for the price of the component's numeric value. There are two primary parts to the PartsStoreManager class:
- Private List inventory: This is a field of the type List that is private. It is used to keep the parts store's inventory of parts in storage.
- Constructor: The class has a constructor that creates a new instance of List to initialize the inventory list.
Figure 5 Public class Partstrore Additionally, the PartsStoreManager class contains two open methods:
- AddPart(Part part): This method adds a Part object to the inventory list by accepting a Part object as a parameter. The component is added to the list using the Add() function of the List class.
- DisplayInventory(): This function shows the parts store's inventory of parts. The inventory list of parts is iterated by using a for each loop. It uses the corresponding attributes of the component object to retrieve the name, category, and pricing information for each part before displaying it. Figure 6 Menu Option There are two primary parts to the Menu class:
- Private List options: This is a field of the type List that is private. The menu choices are kept there for further use.
- Constructor: The class provides a constructor that creates a new instance of List to initialize the options list.
- It displays the phrase "Enter your choice:" before asking the user to input their selection. Figure 9 class program The CarShowroom, PartsStoreManager, and Menu classes are created in the Main method, which serves as the program's entry point. Using the AddOption() function, it additionally adds menu items to the main menu object. The show() function is then used in an endless while loop to show the main menu object. This loop makes sure that the user sees the menu continually until the application is ended. The corresponding action is carried out by the user's selection. The user is requested to input the automobile brand, model, and price, for instance, if they choose "Add Car to Showroom". Using the data supplied, a new Car object is created and added to the showroom using the AddCar() function. Other menu options, such as adding a part to the shop or showing the inventory of cars and parts, are carried out similarly.
The outcomes are identical to those below:
Figure 10 the rest Figure 11 the request of " add a car to showroom" Figure 12 the rest "add a part to store"
a. Creational Patterns
b. Structural Patterns:
c. Behavioural Patterns:
Figure 16 Types of design pattern 1.1 Creational Patterns: These design patterns concentrate on methods for creating things, offering flexibility and separating the code from the particular classes or objects that are being formed. The Singleton pattern, Factory pattern, and Builder pattern are among examples. 1.1.a Builder pattern Builder is a creational design pattern that enables you to gradually build complicated items. utilizing the pattern, you may create several object kinds and representations while still utilizing the same building code. Figure 17 Builder pattern
Example: Scenario: When customers visit a store to purchase a car, they want the seller to be able to offer them the vehicle they require simply. Employees will utilize the Builder Pattern to construct the product on demand and then create specifications for this automobile that the client can see to address this challenge. These are some of the classes that could be offered in the auto shop:
- Car: This class is an example of an automobile. Vehicle code, name, year of production, selling price, status (sold/unsold), and other characteristics might be included in this class.
- CarBuilder: This abstract class or interface stands in for a builder used to create Car instances. SetVehicleID, SetVehicleName, SetManufacturedYear, SetSellPrice, SetStatus, etc. are just a few of the methods in this class that may be used.
- ConcreteCarBuilder: This class, which derives from CarBuilder, is in charge of creating concrete Car objects. For the Car's properties, this class has set methods, and it also delivers a created Car object as a result.
- CarShop: This class stands for the auto shop. This class includes methods to add, remove, and search for Car objects in the list as well as an internal list of Car objects. Here is a thorough illustration of a car business created with the Builder: Figure 18 Car business
public override void SetColor() { car.Color = "Silver"; } public override void SetPrice() { car.Price = 30000; } } // CarDirector class public class CarDirector { private CarBuilder carBuilder; public CarDirector(CarBuilder builder) { carBuilder = builder; } public void BuildCar() { carBuilder.CreateNewCar(); carBuilder.SetName(); carBuilder.SetModel(); carBuilder.SetColor(); carBuilder.SetPrice(); } } // Example usage public class Program { public static void Main(string[] args) { CarBuilder builder = new ConcreteCarBuilder(); CarDirector director = new CarDirector(builder); director.BuildCar(); Car car = builder.GetCar(); Console.WriteLine("Car Details: Make={0}, Model={1}, Color={2}, Price={3}", car.Name, car.Model, car.Color, car.Price); } } }
Table 1 Code for the Builder pattern example Figure 19 The output of the Builder pattern example's code 1.2 Structural Patterns: These design patterns deal with combining classes and objects to create bigger structures while maintaining their flexibility and effectiveness. They offer approaches for streamlining interactions between objects and classes. The Decorator pattern, Composite pattern, and Adapter pattern are among examples. 1.2.a Adapter pattern The interface of an existing class can be utilized as another interface thanks to the software design pattern known as the adapter. It serves as a link between two disparate interfaces, allowing them to communicate with one another. When it's necessary to merge existing classes with incompatible interfaces or reuse code that already exists, the adapter approach is frequently employed. It aids in preserving the separation of concerns, reusability, and flexibility of code. Figure 20 Adapter pattern
Code implementation in C# namespace Adapter { public class CreditCard { public string GetCreditCardNumber() { return "1234567890123456"; } public string GetExpirationDate() { return "12/2023"; } public string GetCVV() { return "123"; } } // Paypal class public class Paypal { public void MakePayment(string email, decimal amount) { Console.WriteLine("Payment made via Paypal with email: {0}, amount: {1}", email, amount); } } // IPaymentAdapter interface public interface IPaymentAdapter { void MakePayment(string email, decimal amount); } // PaymentAdapter class public class PaymentAdapter: IPaymentAdapter { private CreditCard creditCard; private Paypal paypal; public PaymentAdapter(CreditCard card) { creditCard = card; paypal = new Paypal();
public void MakePayment(string email, decimal amount) { string creditCardNumber = creditCard.GetCreditCardNumber(); string expirationDate = creditCard.GetExpirationDate(); string cvv = creditCard.GetCVV(); // Perform necessary operations to convert credit card information to Paypal format // ... paypal.MakePayment(email, amount); } } // Example usage public class Program { public static void Main(string[] args) { CreditCard creditCard = new CreditCard(); IPaymentAdapter paymentAdapter = new PaymentAdapter(creditCard); string email = "[email protected]"; decimal amount = 100.0m; paymentAdapter.MakePayment(email, amount); } } } Table 2 Code for the Adapter pattern example Figure 21 The output of the Adapter pattern example's code