Design Mode learning notes-Basic Knowledge articles, design mode learning notes

Source: Internet
Author: User

Design Mode learning notes-Basic Knowledge articles, design mode learning notes

1. Importance of design patterns

1.1 The design pattern addresses how specific software functions are implemented during the software process. There are many ways to implement the same function. Which design is easy to expand, easy to reuse, loose coupling, and maintenance? The design pattern guides us to find the optimal solution.

1.2 there are often design defects in the design, including:

Rigidity: It is difficult to modify the software, even though it is a small change in functionality.

Vulnerability: a small change may cause problems in many places.

Stubborn: the effort and risk of separating some common functions in the system are enormous.

Sticky: when there are many changes, some will maintain the design, and some will damage the design. When the design method is adopted, it is more difficult to cope with changes than the design method, it indicates that the original design has a high viscosity.

Obscure: Hard to understand modules

Unnecessary repetition: Code cannot be reused, and similar functions are often implemented through Copy-Paste.

Unnecessary complexity: The design contains useless components, often caused by over-design.

1.3 if you find the above problems (defects) during development, you need to use the design pattern to improve the initial design, that is, rebuild the original design. If you are the original designer, you also need to apply the design pattern to find an optimal solution.The design pattern is not a programming language, but a programmer's Internal Skill.Therefore, it is necessary for a developer to learn the design model.

2. For Beginners, the necessary knowledge preparation is still necessary. Without these foundations, it is difficult to thoroughly understand them.

2.1 Basic Object-Oriented Knowledge

The design mode is the design guide of object-oriented programming. Therefore, before learning the design mode, you must first understand what object-oriented is. Here, we only briefly list the main concepts of object-oriented, if you are a beginner, you still have to read the relevant materials. For veterans who already know about it, you can review and sort it out.

2.1.1 object-oriented features: encapsulation, inheritance, and Polymorphism

2.1.2 class and instance

2.1.3 Constructor (Structure Analysis) Method

2.1.4 heavy load

2.1.5 access modifier

2.1.6 attributes/fields/methods

2.1.7 abstract class

2.1.8 Interface

2.2 UML class diagram

When learning the design pattern, only class diagrams are usually used. Therefore, understanding the UML class diagram is very helpful for understanding the pattern. The following describes the relationships in UML class diagrams.

  

2.2.1 Dependency, expressed by dotted lines and arrows. For example, an Animal (Animal) depends on Air ). The following code indicates dependency:

1) as a parameter

Public class Air {public void GetOxygen () {Console. writeLine ("Get oxygen from air. ") ;}} public abstract class Animal {/// <summary> // the Animal can breathe only when it depends on the air, input as a parameter /// </summary> /// <param name = "air"> </param> public void Breathe (Air air) {air. getOxygen ();}}

2) Internal method definition

/// <Summary> /// the animal depends on the air to Breathe, and the object is converted into a new object in the method. /// </summary> public void Breathe () {Air air = new Air (); air. getOxygen ();}

3) static method call

/// <Summary> /// call the static method in the method /// </summary> public void Test () {ClassName. UseStaticMethode ();}

2.2.2 the inheritance relationship (Inherit) is expressed by a solid line and a hollow arrow. For example, Eagle inherits the Automatic Object (Animal)

/// <Summary> /// inherited from Animal // </summary> public class Eagle: Animal {}

2.2.3 implement the relationship (Realize), which is expressed by a dotted line and a hollow arrow. Eagle achieves the flight capability (IFlyAble)

/// <Summary> /// inherited from Animal // </summary> public class Eagle: Animal, IFlyAble {// implement the method defined in the IFlyAble interface public void Fly () {Console. writeLine ("The e can fly. ");}}

2.2.4 composite relationship. before talking about a composite relationship, you have to talk about the Association and aggregation relationship.

1) Association: For two relatively independent objects, when an object instance and some specific instances of another object have a fixed ing relationship, the relationship between the two objects is the association relationship. Example: Relationship between company and employee

      

Code performance through instance fields or attributes

Public class Emplolyee {public string Name {get; set ;}} public class Company {// <summary> /// a Company can have multiple employees /// </summary> private Emplolyee [] employees ;}

2) Aggregate: a type of association, a strong association, emphasizing the relationship between the whole and the part. For example, the relationship between the computer and the monitor is the relationship between the whole and the part, that is, the aggregation relationship.

      

Code performance is also achieved through instance fields or attributes

Public class Displayer {// <summary> // display Model /// </summary> public string Model {get; set ;}} public class Computer {/// <summary> /// indicates the aggregation relationship through fields /// </summary> private Displayer displayer ;}

3) composite relationship is a special form of aggregation, indicating that a thing instance cannot be owned by two things at the same time. For example, an e has a pair of wings, and its wings cannot belong to other e at the same time.

Code performance is also achieved through instance fields or attributes

Public class Wing {}/// <summary> // inherits from Animal // </summary> public class Eagle: Animal, IFlyAble {private Wing leftWing; private Wing rightWing; public Eagle () {// instantiate the wings in the constructor to prevent the wings from being changed leftWing = new Wing (); rightWing = new Wing ();} public void Fly () {Console. writeLine ("The e can fly. ");}}

      Differences between a link and an aggregation link:The two objects involved in the association relationship are at the same level. For example, the relationship between the programmer and the computer is an association rather than an aggregation relationship, because the programmer is not composed of computers. The two objects involved in the aggregation relationship are at an unequal level. One represents the whole and the other represents the part. For example, the relationship between a computer and a monitor is a clustering relationship because the monitor is part of a computer.

      Differences between an aggregation link and a combination link:An object in an aggregation relationship can be held by another object. For example, a multi-state computer can share the same monitor. In a composite relationship, the held object can only be referenced by one object and cannot be shared with other objects. In addition, the lifecycle of the Held Object is also controlled by the owner. When the owner analyzes the structure, all its things must be analyzed along with it.

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.