Explore the software design model (2)

Source: Internet
Author: User
We are in front Article Some software design patterns have been introduced, and some non-software examples have been given. Next, let's continue to explore the software design patterns and take a look at the behavior patterns and instances in these patterns.

Behavior mode

The author summarizes eleven behavior patterns. These patterns can be found in coin classification banking, restaurant ordering, music, transportation, car repairs, vending machines, and home buildings.

Example of chain of responsibility

The responsibility chain mode gives multiple objects the opportunity to process requests, so as to avoid coupling between request senders and receivers. Mechanical coin sorting banks use responsibility chains. Instead of assigning a slide to each coin, you can use only one slide. When a coin falls, the coin is mechanically directed to an appropriate receiving box in the bank.

Figure 13: responsibility chain mode object chart using coin sorting example

Command mode

The command mode encapsulates a request as an object so that you can use different requests to parameterize the customer. The bill for dining is an example of the command mode. The waiter accepts the customer's order and writes it into the bill for encapsulation. This ticket is queued for cooking. Note that the "bill" here is not dependent on the menu, it can be used by different customers, so it can be added to different order items.

Example of an interpreter

The interpreter mode defines the syntax expression and interpreter for a specific language. A musician is an example of an interpreter. The scale and its duration can be represented by a symbol on the Five-line spectrum. These symbols are music languages [14]. A musician can play the same music repeatedly.

Iterator example

The iterator provides a way to access each element in a set object sequentially without exposing the internal representation of the object. In early television sets, a dial was used to change the channel. When changing the channel, you need to manually turn the dial to move through each channel, regardless of whether the channel has a signal. For the current TV set, use the [next] and [previous] buttons. When you press the [next] button, it switches to the next preset channel. Imagine watching TV in a hotel in a strange city. When a channel is changed, it is not the channels but the program content. If you are not interested in a channel program, you can switch to another channel without knowing how many channels it is.

Mediator example

The intermediary mode uses an intermediary object to control a series of object interactions. You can implement loose coupling between objects through intermediaries, rather than directly interacting with each other. The control tower at the airport demonstrates this mode. The plane that lands or departs communicates directly with the tower, rather than directly with each other. It is up to the tower to decide who can take off or land. Note that the tower does not control the entire flight process. It is only responsible for aircraft areas near the airport.

Figure 17: intermediary mode using the training center as an Example

Memento example

The memo mode captures and stores the internal state of an object externally, so that the object can be restored to this state in the future. This mode is usually reflected when you repair the brakes of a car. First, remove the baffle plate on both sides to expose the left and right brake pads. Only one piece can be removed, and the other piece serves as a memorandum to show how the brakes are installed. After the repair is completed, you can unload the other piece. When the second part is removed, the first part is a memorandum.

Observer (observer) Mode

The observer defines the one-to-many relationship between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated. The auction demonstrates this mode. Each bidder has a digital sign for bidding. When the auctioneer started the auction, he observed whether there was a sign raising his bid. Each acceptance of a new bid changes the current price of the auction and broadcasts new bids to all bidders.

State Mode

The State mode allows an object to change its behavior when its internal state changes. This mode can be observed on the vending machine. The status of the vending machine includes a list of items, collection, looking for money, and selecting items. When a coin is invested and a commodity is selected, the vending machine can perform the following operations: do not send out the goods, send out the goods and find the money, do not send the goods due to insufficient coins, because the goods sold out do not send the goods.

Strategy Mode

Policy mode defines a series of alternativeAlgorithm. Different ways to go to the airport are an example of a policy model. There are several options: self-driving, taking a taxi, taking an airport shuttle, taking a bus or using a dedicated car service. Subway and helicopter are also possible for certain airports. Either way can send you to the airport, and they can replace each other. You must make a choice based on the price, convenience, and time.

Template Method example

The template method defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. During the development, the house architect was excited, and the house architect was charming. t yan instantaneous Huang M class Yan Jian?

Example of visitor

Visitor mode indicates an operation that acts on each element in the object structure. Defining this operation does not change the element class. This model can be observed during the operation of taxi companies. When a person calls a taxi company, he or she becomes a member of all customers of the company. Then the company designates a car to serve (accept visitors ). After entering a taxi, this person (visitor) no longer controls his or her journey, but is under the responsibility of a taxi (driver.

Meaning

There are many non-software examples in the software design pattern. Some people may want to know the actual meaning of these examples. Non-software examples help improve the expression of the mode language and assist in learning the mode.

Increase the expression capability of the mode Language

Alexander thinks that the true model should be integrated into a common language so that everyone can share it. In the software design group, the pattern is considered a conventional development method among colleagues. Mode provides a higher level of concept than modules, processes, and objects.

A crucial factor in a language is the mental image corresponding to the same language image. In a language, the shape of a symbol is meaningful only when a person can understand the meaning of a symbol and depict it in his mind. Alexander did not ignore this important feature of a pattern language. He stipulated that a language is completely morphological only after the architectural type it generates can be seen in detail. In software design, richle and züllighoven recognize the importance of specific examples in guiding our understanding of the application field.

If the software design model becomesProgramThe general language used by members, the basis of which is a unified meaning. If the design decision is made but not understood, the designer is forced to complete the work through assumptions. Ordinary examples are easier to understand because people must find the relevant content in their memories. In projects of Ag communication systems, which is widely used, non-software examples are often used to explain the relationship between patterns. This example helps to provide a unified understanding between designers. By establishing a unified understanding of the design process in advance, it makes it easier for designers to communicate throughout the project lifecycle.

Conclusion

in non-software cases, the embodiment of the software design pattern shows that the pattern is not limited to a specific field. Software designers can benefit from the examples of patterns in these everyday things, even if they are not expressed in programming languages. This article tries to give some examples that most people are familiar with (although some prefer North American culture ). By describing common experiences, these examples help you understand specific design patterns and help you learn design patterns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.