Design pattern Quiz (iv)

Source: Internet
Author: User

What is bridging mode?

Bridging mode helps decouple abstraction from implementation. This way, if the implementation changes but does not affect abstraction, and vice versa. As shown in "abstraction and implementation", switches are abstract layers, electronic devices are implemented, and switches can be implemented by any electronic device, so switches are abstract thinking and devices are implemented.

Figure 1: Abstraction and implementation

Below we write code for switches and device examples. The first thing to do is to divide the implementation and abstraction into two classes total. The figure "Implementation" shows the ' Iequipment ' interface, which contains ' Start () ' and ' Stop () ' two methods. We have achieved two devices, one is a refrigerator and the other is a light bulb.

Figure 2: Implementing

The second part is abstraction. The abstract part of this example is the switch, which has a ' setequipment ' method to set the object; ' on ' method calls the device's ' Start ' method, ' off ' calls the device's ' stop ' method.

Figure 3: Abstract

Finally, the client code. We created the implementation objects and abstract objects separately, and we can use them separately.

Figure 4: Bridge mode client code

What is a combination mode?

Many times objects are organized in a tree-shaped structure, and developers have to understand the difference between a leaf object and a branching object. This complicates the code and increases the error rate. For example, the following is a simple tree structure object, the consumer is the main object, each consumer has more than one address, each address has multiple phone objects.

Figure 5: typically handled

Let's say we want to insert the complete object tree. A simple code example follows, looping all consumers, address objects in consumers, and phone objects in all addresses. The Update method is called within the loop in the following code fragment.

The problem with the above code is that each object needs to be updated. For the consumer is ' UpdateCustomer ', the address class is ' updateaddress ', the telephone class is ' Updatephone '. In other words, the main object and the contained leaf object are treated differently, which causes the application to become chaotic and increase the chance of error.

If you can treat both the main object and the leaf object, the code becomes neat. In the following code we create an interface (Ibusinessobject) that restricts all classes, such as consumers, addresses, and telephony, from using a unified interface. Once the interface is unified, all objects have an "Update" method.

In order to implement the combined pattern we first created an interface, as shown in the following code snippet:

Force all root nodes and leaf nodes to use the interface as follows:

Similarly, the address object enforces the interface.

The phone object is the same as follows:

What is adorner mode?

Tip: Adorner mode adds dynamic overlay behavior to help us change the behavior of objects at run time.

There are many situations where we need to add dynamic overlay behavior to a class at run time. The superposition is a very important word. For example, let's consider the following example of a hotel selling a bakery: They have 3 main products, and the order can contain the following combinations.

    • Simple bread

    • Bread and Chicken

    • Bread and drink

    • Bread and chicken, drink

In other words, the behavior of the order process and the change in the order cost at run time depend on the type of composition.

The following example is an order with only bread, there are two functions of ' prepare ' and ' calculatecost '. We will dynamically add new products to our base bread orders based on our customers ' needs.

The following are the interfaces for all orders, each with two functions like prepare and calculatecost.

The base product is bread, which implements the Iorder interface. You can add new products to the bread order and change the behavior of the entire order.

We can use the adorner mode to dynamically select bread orders. There are 5 steps to implementing the adorner pattern:

Step 1: Create an Adorner class and aggregate objects or interfaces that need to dynamically increase behavior

This adorner class will hold these objects, and all calls to the main object method call the main object method before calling all the methods that hold the object.

Therefore, if the prepare method is called, the adorner class invokes all prepare methods that hold the class, and finally calls the prepare method of the class. We can see how the output changes after the introduction of the adorner class.

Step 2: The object or interface being held points to the class that needs to be initialized. We can implement the example in a number of ways, and we will open a simple constructor and pass the object to the constructor to initialize the object that we hold.

Step 3: We will implement the Iorder interface and invoke the method of the owning object through the virtual method. You can see how we created the virtual method to invoke the object we are holding.

Step 4: We have completed important steps, such as creating adorners. Now you need to create an object behavior object that can be added to the adorner and alter the behavior of the object at run time.

Here is a simple chicken order that can be added to the bread order to create a different order called Chicken + bread order. The chicken order is created by inheriting the order adorner class.

Any call to the object, the custom function of the chicken order is called first, and then the function that holds the object is called. For example, when calling the Prepare method, he first invokes the prepare method that prepares the chicken method and then calls the owning object.

The cost of the calculation will also add to the cost of the chicken and call the cost of the held order to calculate the total.

In the same vein, prepare the order drink as follows:

Step 5: The next step is to observe the application of the adorner pattern. So at the client we can write some code like create a bread order.

The following is the output of the above code:

Create an order code logic that contains bread, chicken, and drinks as follows:

The output results are as follows:

In other words, we can now attach these behaviors to the main object and dynamically change its behavior at run time.

Here are the combinations of different orders that we can generate so that we can dynamically select the behavior of the order.

What is façade mode?

Façade mode is located on a set of subsystems and allows these subsystems to interact in a uniform way.

Figure 6: Facades and subsystems

The figure "Order façade" shows an example of a façade pattern. In order to place the order system needs and product, payment, invoice class interaction. So the order became a façade to unify the product, payment and invoice classes.

Figure 7: Order façade

The figure "Façade mode practice" shows that class ' Clsorder ' is used to unify the ' clsproduct ', ' clsproduct ', ' Clsinvoice ' classes by implementing the ' PlaceOrder ' function.

Figure 8: Façade mode Practice

What is the responsibility chain model (COR)?

It is more appropriate to use the responsibility chain model when we have a series of processing logic. Let's take a look at the meaning of the sentence below. Suppose that a request requires a series of processing, so the request is handled by the first handle, he can handle some or no processing, and once completed, the handle will request the incoming link to the total next processing. This process will continue until processing is received and processing is completed.

Figure 9: Concept of the responsibility chain model

Let's look at this concept in a simple example. The figure "Simple example" has some processing logic, of which 3 processing will pass, that is, processing 1 to do some processing to the processing 2, processing 2 to do some processing after the processing of three to complete the processing.

Figure 10: A simple example

Figure "Responsibility Chain pattern Class Diagram", 3 of the processing is inherited from an abstract class. One important point is that each processing point invokes the next process that needs to be processed. So in the processing class we add an extra processing object ' objprocess ', which points to a processing object that needs to be called.

Figure 11: Responsibility Chain Pattern Class diagram

After defining all classes, you can write client calls later. We create 3 process objects Process1, Process2, PROCESS3, and use ' setprocess ' to define the processing object call chain. After we link the process2 to Process1, we link the process3 to Process2. When the link is created, it runs in the order that we defined it in the process.

Figure 12: Responsibility chain mode client code

What is proxy mode?

The proxy mode is fundamentally a representation of a class like an interface that points to a real class that has data, where the data can be large pictures or it can be a large object data that cannot be copied. This allows us to create multiple proxies to point to memory-intensive objects to complete the operation. This avoids the duplication of objects and thus saves memory. So a proxy is a reference to a real object.

The figure "Proxies and actual objects" shows the creation of interfaces and real objects. Where the interface ' Iimageproxy ' is the proxy, the implementation class such as ' Clsactualimage ' is the real class. In the client code, you can see how the interface points to the real object.

Figure 13: Proxy and actual class

The advantage of using proxy mode is that it is secure and avoids the duplication of big data objects, instead of using the actual object code instead of using the proxy, which avoids the loading of the actual class's code on the client, and it is more secure to load the proxy class on the client. The 2nd is that transferring these objects between networks or domain names when there are large objects is very memory intensive, so it is better to pass the proxy performance without transmitting these large objects.

What is template mode?

The template pattern belongs to the behavior pattern. It defines a primary processing template, with sub-processing in the main processing template, which invokes word processing in the sequence. and the sub-processing in the main processing can choose to produce different behaviors.

Tip: The usage scenario for template mode is used when we want to create extended behavior between generalization and specialization relationships.

For example, here is an example of simply formatting data and storing it in Oracle. There can be a variety of data sources, such as files, SQL Server, and so on. Regardless of where the data comes from, the overall process is loading data from the data source, parsing the data, and storing the data in Oracle.

Figure 14: Total Processing

Next we can create a CSV file to load data processing or SQL Server data loading processing based on the total processing, by rewriting the ' load ' and ' Parse ' word processing.

Figure 15: How template mode is handled

From here we can see how to change the ' Load ' and ' Parse ' sub out to generate CSV file processing and SQL Server processing. The invocation of the ' Dump ' function and sub-processing throughout the processing has not changed.

Implementing a template pattern requires the following 4 steps:

    1. Create a template or master process by creating a parent abstract class

    2. Create sub-processing by defining abstract methods and functions

    3. Create a method to define the order in which word processing is called. This method should be a normal method, and this way the class can override the method

    4. Finally create subclasses to implement abstract methods or implement new sub-processing processes

' Sqlserverparser ' Inherits ' Generalparser ' and Overrides ' Load ' and ' parse ' to implement SQL Server parsing.

' Fileparser ' Inherits ' Generalparser ' and rewrites ' Load ' and ' parse ' for specific file parsing

At the client we can call these two classes:

The results of the two parser outputs are as follows:


1. This article is translated by the programmer architecture

2. This article is translated from Http://www.codeproject.com/Articles/28402/Design-pattern-FAQ-part

3. Reprint please be sure to indicate this article from: Programmer Architecture (No.:archleaner )

4. More articles please scan the code:


Design pattern Quiz (iv)

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.