Question

In: Computer Science

or each of the design patterns listed below, briefly discuss the purpose of that pattern and...

or each of the design patterns listed below, briefly discuss the purpose of that pattern and explain how its use contributed to the code you developed for your Maze Game in Assignment 2. Your explanation should provide specific examples from your game that illustrate the function performed by the implementation of each pattern.

a)      Command Pattern

b)      State Pattern

c)      Singleton Pattern

Solutions

Expert Solution

Command Pattern:-

purpose of that pattern :

  • Encapsulate a request as an object, thereby letting you parametrize clients with different requests, queue or log requests, and support undoable operations.
  • Promote "invocation of a method on an object" to full object status
  • An object-oriented callback

Structure

The client that creates a command is not the same client that executes it. This separation provides flexibility in the timing and sequencing of commands. Materializing commands as objects means they can be passed, staged, shared, loaded in a table, and otherwise instrumented or manipulated like any other object.

Command objects can be thought of as "tokens" that are created by one client that knows what need to be done, and passed to another client that has the resources for doing it.

Example

The Command pattern allows requests to be encapsulated as objects, thereby allowing clients to be parametrized with different requests. The "check" at a diner is an example of a Command pattern. The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of "checks" used by each waiter is not dependent on the menu, and therefore they can support commands to cook many different items.

state Pattern:-

purpose of that pattern :

  • Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • An object-oriented state machine
  • wrapper + polymorphic wrappee + collaboration

Structure

The state machine's interface is encapsulated in the "wrapper" class. The wrappee hierarchy's interface mirrors the wrapper's interface with the exception of one additional parameter. The extra parameter allows wrappee derived classes to call back to the wrapper class as necessary. Complexity that would otherwise drag down the wrapper class is neatly compartmented and encapsulated in a polymorphic hierarchy to which the wrapper object delegates.

Example

The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion.

Singleton Pattern:-

purpose of that pattern:-

  • Ensure a class has only one instance, and provide a global point of access to it.
  • Encapsulated "just-in-time initialization" or "initialization on first use".

Structure

Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.

Example

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.


Related Solutions

Briefly discuss the purpose of each of the following. Give one brief example of each.: a....
Briefly discuss the purpose of each of the following. Give one brief example of each.: a. Tests of controls b. Analytical procedures c. Tests of details of balances
Describe the three different patterns of dispersion and provide an example of each. Which pattern of...
Describe the three different patterns of dispersion and provide an example of each. Which pattern of dispersion is most common in nature?
assign the purpose each excipient (listed below) serves as part of the tablet formulation. Using no...
assign the purpose each excipient (listed below) serves as part of the tablet formulation. Using no more than one sentence, what is your reasoning (the rationale) for each ingredient assigned role? If uncertain, list what you believe the most likely role (or roles) an excipient would have, and then list any other possible role (or “job”) that the excipient may have.  If you are completely unsure of the role of an excipient, state that you are uncertain, but speculate as to...
Spray and Stretch(practice the technique, name the referral pattern) Describe the referral patterns below. a. Upper...
Spray and Stretch(practice the technique, name the referral pattern) Describe the referral patterns below. a. Upper Trapezius b. Levator Scapulae c. Supraspinatus d. Gastrocnemius
Briefly discuss the purpose and role that each type of financial institutions (depositary, contractual, and investment)...
Briefly discuss the purpose and role that each type of financial institutions (depositary, contractual, and investment) play in the U.S. economy. How do each of these institutions intersect with the various types of markets, i.e., capital, money, spot (cash), derivatives, Forex and Interbank, primary, and secondary (inclusive of OTC)?
Briefly discuss the purpose and role that each type of financial institutions (depositary, contractual, and investment)...
Briefly discuss the purpose and role that each type of financial institutions (depositary, contractual, and investment) play in the U.S. economy. How do each of these institutions intersect with the various types of markets, i.e., capital, money, spot (cash), derivatives, Forex and Interbank, primary, and secondary (inclusive of OTC)?
Briefly describe the function of each of the organelles listed below cell wall: plasma membrane: nucleus:...
Briefly describe the function of each of the organelles listed below cell wall: plasma membrane: nucleus: nucleolus: ribosomes: endoplasmic reticulum (smooth and rough): Golgi apparatus: Peroxisomes: Mitochondria: Chloroplasts: large central vacuole: lysosomes: extra cellular matrix:
For each type of sense-perception listed below, briefly state (in just a few sentences) how the...
For each type of sense-perception listed below, briefly state (in just a few sentences) how the results of that sense might be shown to be relative to the perceiver. (Brief short answer questions (just a few sentences for each is fine) about the relativity of the senses.) In doing this, you can discuss any case of perceptual disagreement you like. For example, you could cite a difference between human and animal perception (e.g. night vision); or between individual humans (e.g....
Discuss the determinants of demand (listed below) for a specific good: peanut butter. For each of...
Discuss the determinants of demand (listed below) for a specific good: peanut butter. For each of the shifters, give an explicit example of a change in an external factor that shifts demand for peanut butter, then indicate whether your particular example would increase (shift rightward) or decrease (shift leftward) the demand for peanut butter. Number of consumers or size of the population Consumer taste and preferences Consumer income Related product Changes in Expectations
For each pair of elements and compounds listed below, discuss their similarities and/or differences in terms...
For each pair of elements and compounds listed below, discuss their similarities and/or differences in terms of physical and chemical properties. (a) Boron (B) and silicon (Si) (b) BeCl2 and MgCl2; (c) Al2O3 and In2O3;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT