ASM2 AdvancedProgramming PDF
ASM2 AdvancedProgramming PDF
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P3 P4 M3 M4 D3 D4
r Summative Feedback: r Resubmission Feedback:
2. M4 Reconcile the most appropriate design pattern from a range with a series of given scenarios...... 24
a. Benefits of Pattern Builder ........................................................................................................................................24
b. Differences of builder pattern and other design patterns. ......................................................................................24
c. Why we choose builder patterns to develop scenario? ...........................................................................................25
Bibliography ....................................................................................................................................... 26
Table Of Figures
Figure 1: Car ticket Class diagram. ................................................................................................................................ 6
Figure 2: Itialize interface variables. ........................................................................................................................ 7
Figure 3: Initialize the types of seats included in the ticket.......................................................................................... 7
Figure 4: Initialize foods and drinks. ............................................................................................................................. 8
Figure 5: Set sizes and prices for bread and drink. ....................................................................................................... 8
Figure 6: Install items.................................................................................................................................................... 9
Figure 7: Install Show Items. ......................................................................................................................................... 9
Figure 8: Initialize orders for customers. .................................................................................................................... 10
Figure 9: Program implementation. ........................................................................................................................... 10
Figure 10: Program Result. ......................................................................................................................................... 11
Figure 11: Factory Method class diagram................................................................................................................... 15
Figure 12:Prototype Pattern class diagram. ............................................................................................................... 16
Figure 13: Builder pattern class diagram. ................................................................................................................... 17
Figure 14: Decorator Pattern class diagram. .............................................................................................................. 18
Figure 15: Flyweight Pattern class diagram. ............................................................................................................... 19
Figure 16: Proxy Pattern class diagram. ..................................................................................................................... 20
Figure 17: Mediator Pattern class diagram. ............................................................................................................... 21
Figure 18:Iterator Pattern class diagram. ................................................................................................................... 22
Figure 19: State Pattern class diagram. ...................................................................................................................... 23
I. Introduction
- My team is now separated and perform similar tasks in parallel. I will choose one of the real scenarios that
your team introduced about DP in previous phase, then implement that scenario based on the corresponding
class diagram your team created. I may need to amend the diagram if it is needed for your implementation. In
additional, I should discuss a range of DPs related / similar to your DP, evaluate them against your scenario
and justify your choice.
- In the end, I need to write a report with the following content:
• A final version of the class diagram based on chosen scenario which has potential of using DP.
• Result of a small program implemented based on the class diagram, explain how you translate from design
diagram to code.
• Discussion of a range of DPs related / similar to your DP, evaluate them against your scenario and justify
your choice (why your DP is the most appropriate in that case).
II. Implement code applying design patterns.
1. P3 Build an application derived from UML class diagrams.
- As introduced in the Design patterns in the previous task. We have introduced to you 3 types of DP of 23 DP
models.
- Among those 23 DP models, we have introduced to you 3 models representing 3 main groups of DP:
creational, structural and behavioral.
- 3 DP models that my group introduced in the previous task are: Builder (creational group), Flywiegh
(structural group) and Stragery (behavioral group).
- At the request of the mission this time I decided to choose a realistic situation that was to use the Parttern
Builder for ordering food at a fast food restaurant.
- Based on the requirements of the project, I prepared a realistic scenario to introduce the Builder Pattern in the
previous task.
- Scenatio is as follows:
- I received an email from the manager of the south bus terminal company, who heard about your success. He
wants you to create a new OMS for your ticketing system. For the first milestones, he identified a number of
requirements that need to be met by your OMS:
• Car ticketing system, Car ticket booking (vip or regular seats) as well as some sandwiches (Big bread,
Small bread) and some drinks (Coca and Pepsi) besides customers Can choose the size they want them to.
The system will be able to represent these products.
• All priced products and breads have specific nutrition.
• Each bread contains less bread and sauces. It can be selected size of bread supplement (Big, Small), can be
ordered several times.
• Breads with certain topping combinations have names, for example: large bread and small bread. Names
should be on the bill. You do not need to automatically detect names for combinations if they are not
explicitly needed in order.
• Finally, after finishing the order, the system will display the product information of the customer and the
total amount on the invoice.
- The manager sent you the fastfood menu card to better understand their available items:
Ticket VIP $250
NORMAL $150
BREAD BREADBIG $50
BREADSMALL $35
DRINK COCA $50
PEPSI $35
SIZE BIG $50
SMALL $35
- Based on the scenario the company manager requested. We have planned to perform that scenario and choose
the most suitable DP to execute that scenario.
- After reaching agreement, we decided to choose the Builder Pattern to design that table script.
- Below is the class diagram that we have defined to implement the above project.
- After we have designed the class diagram according to the builder pattern, we have implemented the script on
c #. Here's how we deploy and execute on c #.
- Itialize interface variables:
- Program implementation
3 Implement Bread
- The prototype pattern is one of the creative patterns. It is responsible for initializing an object by clone an
existing object instead of initializing with the new keyword. The new object is a copy that can be 100%
identical to the original object, we can change its data without affecting the original object.
- Prototype Pattern is used when creating an object is costly and time consuming while you have a similar
object existing. (phan, 2019)
- Class Diagram.
• For example: A company has the same computer configuration for all employees, including: Operating
system (os), Office software (office), Antivirus software (antivirus), Browser (Browser), and Some other
software (others) depending on the needs of each employee will be installed additionally. Installing all of
the above software is time-consuming, so IT thought of one way would be to create a standard copy for a
computer and be able to clone () configure it for another employee without needing to. Must reinstall
from the beginning.
- The build pattern is one of the Creational patterns. Builder pattern is an object design pattern created to build
a complex pair of objects using simple objects and using a step-by-step approach, the construction of objects
that are independent of other objects.
- The Pattern Builder is built to overcome some of the disadvantages of Factory Pattern and Abstract Factory
Pattern when Objects have many properties.
- We can handle these problems with a large number of parameters by providing an initialization function with
required parameters and getter / setter methods to set optional parameters. The problem with this approach is
that the state of the Object will not be consistent until all the properties are explicitly set. If you need to build
an Immutable object, this way is also not possible. (Giang, 2019)
- Class diagram
• For axample: Use parttern builder to design car ticketing system
- Mediator Pattern is one of Behavior Pattern. Mediator means intermediary. This pattern says, “Defining an
object encapsulates how a set of objects interact. Mediator promotes lax articulation by preventing objects
from explicitly referring to each other and it allows you to change their interaction independently. ”
- Mediator Patern (intermediate model) is used to reduce the complexity of "communication" between classes
and objects. This model provides an intermediary layer that handles communication between classes,
supporting easy code maintenance by loosening coupling.
- The loose coupling here is understood to be similar objects that do not "communicate" directly with each
other but communicate through middlemen, and it allows to change the way they interact independently.
- The Mediator Patern fosters many-to-many relationships between objects in order to achieve the desired
results. (Giang, 2019)
- Class Diagram.
• For Example: In a chat application, a user will be able to send and recieve messages. When a user wants
to send a message to the group, that user has to find out whether all people are online or in a possible
message state to send. If normally the user has to send everything by himself, must check each member
and make the message.
• When using Mediator: users do not need to check, do not care who can receive messages, users just send
information to Mediator of the group. Mediator will coordinate this message to the recipient.
- Iterator Pattern is one of Behavior Pattern. It is used to "Provide a sequential access to elements of an
aggregate object, without having to create separate access methods for this composite object".
- In other words, an iterator is designed to handle many different types of collections by accessing the elements
of the set with the same method, the same method, without having to be familiar with the Inside details of
these collections.
- Class diagram.
- Components participating in the Iterator pattern:
• Aggregate: is an interface that defines the methods to create an object iterator.
• ConcreteAggregate: implements Aggregate methods, it implements the Iterator creation interface to return
an appropriate ConcreteIterator instance.
• Iterator: is an interface or abstract class, defining methods to access and browse elements.
• ConcreteIterator: install Iterator methods, keep indexing when browsing through elements.
• Client: The object uses the Iterator Pattern, which requires an iterator from a collection object to browse
for the elements it holds. The iterator methods are used to retrieve the items from the collection in an
appropriate sequence.
• For example: Create Iterator to browse items in a menu.
- State Pattern is one of Behavior Pattern. It allows an object to change its behavior when its internal state
changes. The object will appear to change its class.
- Class diagram.
• For example: Suppose we need to build a Document management application. A Document can include
states: create (New), submit (submit), approve (Approved) and refuse (Rejected).
• We will first create an inteface base to receive processing requests. This class is called State.
• Next, for each value in the enum, we will create a new class and implement State methods.
• Finally, we create a Context class. This class contains current State information and receives processing
requests directly from the Client.