Explore Design Patterns

Source: Internet
Author: User

The Software Design Patterns come from Christopher Alexander's architectural patterns and object movements. According to Alexander, the pattern is a repetition of the general solution itself for a specific system. Object movement focuses on the relationship between the real world and the software. For these two reasons, the software design model should be repeatable for objects in the real world. This article presents examples of non-software models in the real world, these patterns come from design patterns-elements of reusable object-oriented software [13. This article also gives an example of how pattern languages practice non-software expressiveness and design patterns.

In the software industry, groups of model supporters are expanding. The origins of pattern development can be found in the book of architect Christopher Alexander, who believes that pattern is a universal solution for specific systems in the world. The model he described can be observed in everyday buildings. Each pattern in pattern language contains an image of the original pattern.

Although material is the opinion of the mainstream world, and the model is believed in by the Software world, it also has its roots in reflecting the development of things. Unfortunately, the examples of the software design model are not as rich as the Alexander model, because the software design shows exquisite ideas rather than the original ideas. The uniqueness of most software today limits our access to first-class design opportunities.

According to Alexander, patterns in the real world always repeat themselves, because in a specific environment, they always adapt well to the existing environmental factors. In software, either real-world problems are completely modeled, or real-world objects are converted into hardware and software to generate real-world results. Since the software design pattern is rooted in Alexander's pattern and object, it is normal to find the software design pattern in the real world. This does not mean that the software design model is an inevitable model of real-world things, but that the relationship between the fit objects can be observed in the same way as the "real-world" and the software objects. To verify this assumption, we will find a real-world example for each design pattern.


Creation Mode


The author (the author of design patterns) summarizes the five creation patterns. Examples of creation patterns can be found in manufacturing, fast food, biology, and administrative institutions.

Abstract Factory example

The abstract factory aims to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes. This mode can be found in the metal stamping equipment used by Japanese automobile manufacturers. This type of stamping equipment can be used to manufacture automotive body parts. The same machine is used to press the right door, left door, right front fender, left front fender and hood of different models. By using a runner to change the volume, the specific category produced by this machine can be changed within three minutes.



Figure 1: stamping example of an abstract factory

Builder example 

The builder mode separates the construction of complex objects from the presentation of objects, so that the same building process can create different representations. This mode is used to make children's meals in fast food restaurants. A typical children's meal includes a staple food, a complementary food, a drink, and a toy (such as a hamburger, fried chicken, cola, and toy car ). These can be different in different children's meals, but the process of combining them into children's meals is the same. The process is the same no matter whether the customer orders a hamburger, sanmingzhi or chicken. The staff at the counter directly put the staple food, complementary food and toys together. These are put in a bag. The drink is poured into the cup and placed outside the bag. These processes are the same in competing restaurants.





Figure 2: Object action table of the generator mode using children's meals as an Example

Factory method) 

The factory method defines an interface for creating objects, but the subclass determines which class to instantiate. Injection Molding demonstrates this mode. Plastic toys manufacturers process plastic powder and inject the plastic into the desired shape of the mold. The type of toys (cars, people, etc.) is determined by the mold.





Figure 3: object graph of the factory method using injection molding as an Example

Prototype example 

In prototype mode, a prototype instance is used to specify the type of the object to be created. The prototype of a new product is usually created before all products. Such prototype is passive and does not participate in copying itself. The split of a cell produces two identical cells. This is an example of assuming an active role to copy its own prototype. This demonstrates the prototype. One Cell Division produces two cells of the same genotype. In other words, the cell clones itself.





Figure 4: Prototype pattern object graph using cell division examples

Singleton example 

The single-piece mode ensures that a class has only one instance and provides a global access point to access it. The single-piece mode imitates the naming of a single set. The definition of a single set is that each set contains only one element. The position of the US president is a single piece. The U.S. Constitution specifies the election of the President, the order of office and succession. In this way, only one current president can be employed at any time. Regardless of the identity of the current President, his title "President of the United States of America" is a global access point for the person accessing the position.





Figure 5: single-piece pattern object graph using the president's example



Structural Mode

The authors summarize seven structural patterns that can be found in tools, residential wiring, mathematics, holiday traditions, retail catalogs, and banking.

Adapter example 

The adapter mode allows you to convert an interface of a class into another interface that the customer expects, so that classes that originally cannot work together due to interface incompatibility can work together. The wrench provides an example of an adapter. A hole is mounted on the spine and each side of the spine is of the same size. In the United States, the typical side lengths are 1/2 ''and 1/4 ''. Obviously, if no adapter is used, the 1/2 ''spine is not suitable for the 1/4'' hole. An adapter between 1/2 ''and 1/4'' has a 1/2 ''overcast slot to enclose a 1/2'' tooth, at the same time, there is a 1/4 Yang trough to card the 1/4 ''wrench.





Figure 6: adapter objects using a wrench adapter example

Bridge example 

The Bridge Mode separates the abstract part from its implementation so that they can change independently. A common switch-controlled electric lamp or fan is an example of bridging. The purpose of the switch is to turn the device on or off. The actual switch can be a simple double-knife zipper switch or a dimming switch.



Figure 7: Bridging object graph using an electronic switch example

Composite example 

The combination mode combines objects into a tree structure to represent the "part-whole" hierarchy. Allows users to use a single object and composite object in a consistent manner. Although examples are abstract, arithmetic expressions are examples of combinations. Arithmetic expressions include operands, operators, and other operands. The operand can be a number or another expression. In this way, both 2 + 3 and (2 + 3) + (4*6) are valid expressions.





Figure 8: Combined Mode object graph using arithmetic expression examples

Decorator example 

The decoration mode dynamically adds additional responsibilities to an object. A painting can be mounted to a wall, whether it is a picture or a frame, but it is usually a frame that is actually mounted to a wall. Before hanging on the wall, the painting can be covered with glass and mounted into the frame. At this time, the painting, glass and frame form an object.





Figure 9: Decorative Pattern object graph with a framed image as an Example

Facade example

The appearance mode defines a unified and high-level interface for the interfaces in the subsystem for ease of use. When a consumer purchases data by directory, the appearance mode is displayed. The consumer calls a number to contact the customer service representative. The customer service representative plays the role of this "appearance", which includes the interface with the ordering department, the cashier department, and the delivery department. 




Figure 10: appearance mode object diagram of the telephone order example

Example of flyweight

The metadata mode uses the sharing technology to effectively support a large number of fine-grained objects. The Public Switched Telephone Network (PSTN) is an example of the metadata. Some resources, such as the dial-up sound generator, the ring tone generator, and the dial-up receiver, must be shared by all users. When a user picks up a phone call, he does not need to know how many resources are used. All the things for a user are dialing, dialing, And dialing.


Figure 11: An Example of the dial-up audio generator-type object


Proxy Mode

Proxy mode provides an intermediary to control access to this object. A check or bank deposit is the proxy of funds in the account. Check is used to replace cash in market transactions and control the funds on the issuer's account.



Figure 12: proxy mode object graph using the bank deposit example  


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 algorithms that can be replaced with each other. 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 the operation. The house architect uses the template method when developing a new project. A typical plan includes some floor plans, each of which reflects different parts. In a plan, the foundation, structure, water loading, and cabling are the same for each room. Different house styles are generated only when there is a difference in the later stage of the building.


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 pattern becomes a commonly used language for programmers, its foundation 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.

This article Reprinted from http://www.360doc.com/content/09/0610/23/155970_3850013.shtml

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.