Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the following cases choose the most appropriate design pattern. Choo

ID: 3600097 • Letter: F

Question

For each of the following cases choose the most appropriate design pattern. Choose from the following Factory Method, Singleton, Proxy, Composite, Template Method, Adapter, Decorator, Strategy, Observer, Iterator, State. Not all patterns may be appropriate. Some patterns maybe used more than once A Loan class calculates interest either based on Simple or Compound Interest. Choose] You have a Point class whose interface uses polar coordinates and you want to provide a wrapper so it can be used to implement a version of Point that uses rectangular coordinates. Choose] We have a computer simulation of a bicycle involving wheels, spokes, and so forth, and we want to be able to treat all of these parts and subassemblies of the bicycle uniformly as bicycle components in some parts of the code Choose] A method in the ATM software at the airport does currency conversions by forwarding the data to an identical method running on a computer at the bank's data center downtown where the calculations are done and results returned to the airport ATM computer. I Choose The client expects a TeaBag that supports steeplnCup method. However the library only provides LooseLeafTea that support steep method. A TeaBall class is introduced to help the client use LooseLeafTea Choose] A documentWriter always writes a document in the following order Header, PageContent and Footer. The actual content of these sections may vary by document type Choose] Your program can send content to a printer You want to ensure that different parts of the program don't command the printer to print output belonging to multiple documents simultaneously, mixed together [ Choose

Explanation / Answer

1. Design pattern for Loan class: Factory Method

Explanation: In factory method, we define only interfaces, subclasses must provide their own implementations.

As the implementation to calculate interest in case of simple or compound is quite different we need to have separate subclasses to implements the common interface for calculating interest.

2. Design pattern for Point class: Decorator

Explanation: Point can be used to form many structures like circle or rectangle or square or rhombus or any other shape

We are able to create new objects or shapes (square or rectangle) without altering the existing one (point).

Decorator design is used when we need to have varying objects (squares or rectangles) with the same underlying base object (point).

3. Design Pattern for computer simulation of a bicycle: Composite

Explanation: Here wheels, spokes etc are a group of objects that in turn form the parts of a bicycle and we must treat them equally (as mentioned in the question) but in fact, they are different (wheel is different from spoke). Composite design pattern allows us to represent or deal with these bunch of components (Spoke or wheel) that are part of another component (usually a larger one, example cycle) by providing a common interface for all the objects (Spoke or wheel) in the component (cycle).

4. Design pattern for ATM machine: State pattern

Explanation: The ATM machine doesn't have to do any computation by itself. It relies on the server and based on the response sent back by the server it proceeds further.

For example

- If sufficient funds are available then dispense cash (debiting is done by server).

- If mini statement is requested either display or print it

- If balance inquiry is requested display or print it.

In other words, ATM machine does different things based on the response (let's say state of the server)

5. Design pattern for TeaBag: Composite

Explanation: Similar to question 3 we have various components here TeaBag, LooseLeafTea and TeaBall, the client at the end is going to treat this as one single component. Hence here we have to use composite pattern.

6. Design pattern for Document Writer: Template method

Explanation: Template method is used when

- You need to perform some common logic (In this case writing the document)

- There is a part of logic that varies according to some given parameter (In this case content type)

Using template method we can have

- Base class that defines a common print method (It basically prints whatever is sent as argument)

- Defines an abstract method that allows us to format the data according to content type (Format of JSON is quite different from Javascript)

Subclasses can be created for each content type, all of them will implement the abstract method ( the second method that formats data according to the content type).

7. Design pattern for a printer: Singleton

Explanation: Information sent to the printer must be sequential (in other words we cannot have multiple instances writing to the same printer, this leads to mixing multiple documents). Hence we must create only one instance and this must be used to print all the required documents.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote