Rookie look at the beginning of design mode series notes: Principles and introduction of design pattern

Source: Internet
Author: User
Tags dashed line

A pattern is a solution to a problem in one context.

The design pattern is to a large extent to solve the software reusability, and according to a lot of engineering practice summarizes the software architecture, which includes the object-oriented idea of software engineering: encapsulation, inheritance, polymorphism.

Why design patterns are required: Design patterns are a set of reusable, most known, categorized purposes, code design experiences,

Design patterns are used to reuse code, make code easier for others to understand, and ensure the reliability of your code.


Design patterns generally have the following basic elements: schema name, problem, purpose, solution, effect, instance code and related design patterns, key factors include the following four aspects:

(1) Pattern name (pattern names) (2) problem (problem) (3) Solution (solution) (4) effect (consequences)

Everything is treated as objects in an object-oriented approach. Object is the unification of functional abstraction and data abstraction, which models the solution problem itself.

Object-oriented common nouns: object-oriented Analysis Ooa, object-oriented design Ood, object-oriented programming oop, object-oriented programming oop, object-oriented testing oot

Object-oriented Basic principles: (1) abstract abstraction (2) package Encapsulation (3) module modularity (4) Hierarchical hierarchy

Several basic concepts of object-oriented: Object, class, interface interface, polymorphic polymorphism, inheritance inheritance, Class (and object) relationships

Relationships between classes (and objects):

(1) Static contact ———— Associated Association

An association is a connection between a class and a class, so that one class knows the properties and methods of another class. Is the most common type of relationship between classes and classes, which is a structured relationship between a class of objects and another type of object.

In UML Class diagrams, the classes that correspond to associated objects are concatenated with solid lines, and when you use programming languages, such as Java, C #, and C + +, to represent association relationships, the object of one class is typically used as a property of another class.

Includes three related relationships: bidirectional, one-way, self-correlating

Bidirectional Association Class diagram:


One-Way Association class diagram:


Self-correlating class diagram:

(2) Constituent relationship ———— Aggregation aggregation (whole-part)

The aggregation relation is one kind of association relation, is the strong association relation, is the relationship between the whole and the individual. Represents the relationship of a whole to a part.

The whole class forms an aggregation relationship with the member class, which is part of the overall class.

If you cannot determine that a relationship is an aggregation relationship, you can set them as an association relationship.

The aggregation relationship in UML is represented by a straight line with a hollow diamond.


A combinatorial relationship is one of the relationship relationships, which is stronger than the aggregation relationship, and can be set as an aggregation relationship or even an association relationship if you cannot determine whether a relationship is a composite relationship.

It also represents the relationship between the whole and the part of the class, but the part and the whole of the composition relationship have the same life cycle, once the whole object does not exist, some objects will not exist, and some objects have die relationship with the whole object.

The combination of relationships in UML is represented by a straight line with a solid diamond.


Categorical relationships ———— generalization generalization (general-Special)

A generalization relationship (generalization) is an inheritance relationship that describes a relationship between a parent class and a subclass, which is also known as a base class or a superclass, and a subclass is also known as a derived class.

Generalization relationships in UML are represented by a straight line with a hollow triangle

The Java language is implemented using the extends keyword, using the colon ":" in c++/c#.


Interfaces and implementations, interfaces can also have inheritance relationships and dependencies similar to those between classes, but there is also an implementation relationship (realization) between the interface and the class, in which the class implements the pretext that the operations in the class implement the actions stated in the interface. In UML, the implementation relationship between a class and an interface is represented by a dashed line with a hollow triangle.



Use relationship ———— dependency dependency (behavioral dependency)

A dependency is a usage relationship, and a change in a particular thing can affect other things that use that thing, and use dependencies when it is necessary to represent one thing using another.

In most cases, a dependency is embodied in a method of a class that uses an object from another class as a parameter.

In UML, a dependency relationship is represented by a dashed line with arrows


A good software system needs to contain three features: Extensibility (extensibility), flexibility (flexibility), pluggable (pluggability)

The seven principles of software design:

(1) Single duty principle (SRP)

In the case of a class, there should be only one cause for its change.

The responsibilities of the class include two aspects: data responsibility and behavior responsibility, data responsibility is embodied by its attribute, and behavior responsibility is manifested by its method.

The principle of single responsibility is to achieve the guideline of high cohesion and low coupling.

The corresponding design mode: Facade, Proxy

(2) Open-close principle (OCP)

When designing a module, the module should be extended without modification, changing the behavior of the module without having to modify the source code.

Abstraction is the key to open and close principle, and the opening and closing principle can also be described by a more specific "principle of variability encapsulation", which can be encapsulated by the changeable packaging principle which requires the system to be found.

A variability should not be scattered in many corners of the code, but should be encapsulated in an object. (Inheritance can be seen as a method of package changes)

A kind of denatured English another kind of variability is mixed together. (The hierarchy of inheritance should not be too much)

Corresponding design Patterns: strategy, simple Factory, Factory Method, Abstract Factory, Builder, Bridge, facade, mediator,

(3) Liskov substitution principle (LSP)

If a software entity is using a base class, it must be applied with its subclasses, and it is not possible to perceive the de-distinction between the base class object and the Subclass object.

That is, subtypes (subtype) must be able to replace their base type (Basetype).

In software design, if you can use a base class object, you must be able to use its child class object.

Use the base class type as much as possible in your program to define the object, and at run time, in determining its subclass type, replace the parent class object with the subclass object.

If there is a hierarchical structure formed by an inheritance relationship, then all the leaf nodes above the tree graph of the hierarchy should be specific classes,

And all the tree nodes should be abstract classes or interfaces.

Corresponding design pattern: strategy, Composite, Proxy

(4)


The design pattern of Gof mainly includes: 23 kinds of Design pattern summary


Rookie look at the beginning of design mode series notes: Principles and introduction of design pattern

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.