The basis for reusable object-oriented software in design patterns (1)-Example

Source: Internet
Author: User

I started to learn the design model and posted "design patterns can reuse the basis of object-oriented software" on the Internet. After reading the introduction, I found that I have written so many self-satisfied code before, however, from the perspective of the design model, it's all Spam !!

It seems that it is necessary to charge well. The journey of a thousand miles begins with a step. Let's summarize the legends of the design pattern:

Legend:
1). instantiate
The virtual arrow lines in indicate that one class instantiates an object of another class, and the arrow points to the class of the object to be instantiated.
  
2). Inheritance
We use a vertical line and a triangle to represent the subcategory relationship,
As shown in.
  
3). abstract class
The Class Name of the abstract class is in italic, which is different from the specific class. Abstract operations are also represented in italics. The figure can include the pseudo code that implements the operation. If this is the case, the code will appear in the box with a fold, and the dotted line will be used to connect the fold box with the Operation implemented by the code, as shown below.
  
4). Mixin class)
Mixin class is a class that provides alternative interfaces or functions for other classes. It cannot be instantiated like an abstract class. The mixed classes require multi-inheritance, as shown in the figure below.
  
5). Delegate
A delegate is a combination method that enables a combination to have the same reuse capability as inheritance [lie86, jz91]. In the delegate mode, two objects are involved in processing a request. The object receiving the request delegates the operation to its proxy (delegate ). This is similar to a Child class that gives requests to its parent class for processing. When inheritance is used, the inherited operation can always reference the object that receives the request. In C ++, this member variable is used, and in Smalltalk, self is used. In order to achieve the same effect, the object receiving the request will pass itself to the entrusted person (agent), so that the entrusted operation can reference the object receiving the request.
  
The arrow lines indicate the reference relationship between a class and another class instance. The reference name is optional. In this example, it is "rectangle ".
6). Aggregation and acquaintance)
Aggregation means that one object has another object or is responsible for another object. Generally, an object contains another object or a part of another object. Aggregation means that the aggregate object has the same lifecycle as its owner.
Acquaintance means that one object only knows another object. Sometimes acquaintance is also called "Association" or "Reference" relationship. Acquaintance objects may request operations from each other, but they are not the responsibility of the other party. Acquaintance is a relationship that is weaker than aggregation. It only identifies the loose coupling relationship between objects.
In, the common arrow lines indicate acquaintance, and the arrow lines with diamond at the end indicate aggregation:
  
In C ++, aggregation can be implemented by defining member variables that represent real instances, but more often, these member variables are defined as instance pointers or references; acquaintance is also implemented by pointer or reference. Basically, whether it is aggregation or acquaintance is determined by your intention rather than the explicit language mechanism.

Related Article

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.