Design concept: Changes in requirements, programming for interfaces, preferential use of aggregations

Source: Internet
Author: User

basic elements of a pattern: The name of the pattern, the problem that the pattern solves (the scenario that the pattern applies to), the solution, the results of using the pattern (including pros and cons)Classification of patterns:

Schema mode: The architecture pattern describes the basic structure of the software system organizational strategy, in fact, the macro-organization of software rules and guidelines

Design pattern: A design pattern describes a core solution that is constantly being reproduced in a part of a software system that appears in a well-designed structure that can be applied to a similar context in the future

General duty distribution software model (GRASP Mode): Describes the effective experience and basic principles of assigning responsibilities to different objects in the system in object-oriented design, grasp model is simply a summary and generalization of the design principles in the process of assigning responsibilities, and there is no specific mechanism to provide recommendations for each model.

design principles and concepts contained in the design pattern:

    • The most fundamental intention of the design pattern is to adapt to the change in demand: The variability is essentially an intrinsic feature of the software, and the greatest use of design patterns is to adapt to changes in demand and, with the help of design patterns, We can leave enough space for possible future changes in the design phase (see the robot feeding pig case below); only implement what you need, and under the guidance of this principle to moderate use of design patterns, the application of design patterns should be limited to those in the system obviously unstable , most likely to change the part
    • Programming for interfaces, not for implementation : Components that are programmed for an interface do not need to know the exact type and implementation of the object, just know that the abstract class defines those interfaces, which reduces the dependency on the implementation
    • Use aggregations rather than inheritance first

Robot Feeding Pig Case:

At first the pig farm intends to feed the white pig with a robot:


Changes in demand one: Later, the introduction of Landrace pigs:


The problem now is the changing needs of pig farms, the introduction of other kinds of pigs or other species, the need to constantly modify the code, this is to add a pig's abstract interface, and modify the pig robot code, so that it does not consider the type of pig, only apply abstract pig interface to operate all pig object instances


Demand change two: Let the Robot clean the pigsty (this may hide the need for more change, the future of the robot may also continue to increase the function)

Change one:


But there is a problem here: the inheritance structure in the diagram is determined at the time of compiling the device, there is no change in the runtime, so if the pig farm needs a pig-feeding robot and a cleaning robot, then the two specific robots must be put into the pig farm, and so on, If the future pig farms also need other kinds of robots, the number of objects is very large, and do not add a robot will have to modify a place in the code,

Change two:


comparison of inheritance and aggregation:

    • inheritance reflects the "' ..." between classes. is a ... "Such a relationship, which is statically defined during compilation , the advantage of inheritance is that it is simpler to use, but the disadvantage is that it is not possible to change the inherited structure during the run, the base class often defines the implementation of the partial, the implementation of the base class is exposed to the derived class, The inheritance mechanism destroys the encapsulation of data and operations, making the derived class more dependent on the base class (which is used in relation to aggregations)
    • aggregation reflects the "..." Between classes ". ....." Have a ... "relationship that is dynamically defined during run time , so the type of the object being aggregated can be easily changed during operation, as long as their interfaces are guaranteed to be the same, the full substitution principle can be met, and the use of aggregations to better encapsulate the object, So that each class is concentrated on a single function, the inheritance hierarchy of the class is also kept small; the disadvantage is that he is not a feature that is directly supported by object-oriented languages, and users must write code to complete the aggregation function
    • Aggregation, rather than inheritance, should be preferred without violating the relationship reflected by inheritance and aggregation, and aggregation must also be used in conjunction with interfaces and associated inheritance structures

Design concept: Changes in requirements, programming for interfaces, preferential use of aggregations

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.