Basic Design Patterns-code implementation of UML relationships

Source: Internet
Author: User

Basic Design Patterns-code implementation of UML relationships

The predecessors said: A picture is better than a thousand words. UML class diagrams undoubtedly provide convenience for the code implementation process. Therefore, understanding the conversion between some UML class diagrams and codes is the basis of learning.

[Inheritance & Implementation] pipeline + udjBqiZhbXA7vtu6zyZhbXA71 + m6z6G/PC9oMj4KICAgICAgIL7bus/T69fpus +/Pipeline "http://www.2cto.com/uploadfile/Collfiles/20141215/20141215083417196.png" alt = "\"> Method 1: direct reference
In the penguin category: class penguin {private Climate climate; // directly reference the Climate class}
Method 2: instantiate referenceYou can use the association and composite relationship in, but the combination is more specific than the association. Facade class: class Facade {SubSystemOne sbo; SubSystemTwo sbw; SubSystemThree sbe; public Facade () {sbo = new SubSystemOne (); sbw = new SubSystemTwo ();}} pay attention to the application type during the reference process. If there is a triple inheritance relationship and the association class is on the second layer, the protect type is used;



Method 3: Use of ArraysWhen a class is composed of a group of the same classes, consider the array reference: class WideGooseAggregate {private WideGoose [] arrayWideGoose;} is associated in the classic proxy mode, on the one hand, the proxy must implement interfaces, on the other hand, it must be associated with the pursuer, and on the other hand it is associated with Schoolgirl class Pursuit: IGiveGift // inheritance and implementation interfaces are the same {SchoolGirl mm; public Persuit. mm = mm ;}} class Proxy: IGiveGift {Pursuit gg; // reference the puisuit class public Proxy (SchoolGirl mm) when associating with the target. // The Proxy must finally introduce the method to implement the target, therefore, by referencing the girl object to the suer {gg = new Pursuit (mm); // the final implementation of double Association} } [Dependency] dependency is particularly prominent in the factory family. The specific method class is equivalent to the processing and manufacturing machine, and the specific factory class is equivalent to the specific processing workshop. The workshop depends on the machine for processing, function. Interface Factory {operation createoperation (); // directly reference the operation class to define the specific implementation method;} class AddFactory: Factory {public operation createopreation () // similar to rewriting {return new operationAdd (); // call a specific machine directly through an interface (specific operation class) methods} [inherit VS associated VS dependency] inheritance and implementation are strongly related. Subclass inherits the parent class method, so it is not necessary to reference the parent class method in the subclass. However, constructor cannot be inherited, so that sub-classes can inherit the parent class method while maintaining their own personality. Association is relative to the relationship between inheritance and dependent classes in a medium position. Therefore, in code implementation, the parent class is instantiated and then introduced to the constructor, in this way, the entire parent class can be used. Dependency is the weakest relationship among the three. Therefore, the reference process is placed in a specific implementation method, rather than being used by all function members in the class like Association. [Summary] The code implementation process of a UML class diagram is relatively easy to understand but not easy to remember. You can master it only when you use multiple functions! It is the basis of programming and design patterns and should be paid attention!

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.