Object-Oriented Analysis Design learning and exploration (6): Object-oriented disaster (OO catastrophe)

Source: Internet
Author: User

What is an interface?

TheseCodeThe structure has two types of roles: one is to define some actions to provide more types, and the other is to focus on the classes to use these types. (This code construct has two dual role of defining behavior that applies to multiple types, and also being the preferred focus of classes that use those types)

Using Interfaces makes your software easier to expand (coding to an interface, rather than to an implementation, make your software easier to extend)

Using interfaces, your code can use any inheritance type under this interface, and before implementing them (by coding to an interface, your code will work with all of the interface's subclasses-even ones that haven't been created yet .)

Let's take a look at this example:

This is an example of an athlete: There is an athlete interface, which implements some actual types, such as: football players, baseball players, basketball players .... There are two options when writing code. You can use the inheritance type implementation, such as football players, or the athlete interface. When you have such a choice, you prefer to use interfaces instead of implementation types. Because the actual type is limited.

 

This is important because it can be enhanced.ProgramFlexibility,

What is encapsulation?

Compared with other object-oriented principles, it can better prevent problems and make changes in object behavior require localization. (It's been responsible for preventing more maintenance problems than any other oo principle in history, by localizing the changes required for the behavior of an object to vary .)

Encapsulation can avoid copying and pasting a large amount of code and avoid unnecessary changes to the type. At any time, you think a behavior in the program may change. You want to move this behavior from a program with less frequent changes. In other words:Encapsulation changes.

Let's take a look at this example:

 

Painter type has two relatively stable methods, but painting () often changes in this implementation. So let's encapsulate the changes and remove the paint () method from the painter type.

 

What is change?

Each type has only one change point (every class shocould attempt to make sure that it has only one reason to this, the death of each badly designed piece of software .)

What remains unchanged in the software is changes. Poor design will cause the software to crash in changes, but good software can be easily changed.

Each type has only one reason for change, which will make your software relatively simple to cope with changes. In other words, reduce the change points in the type to reduce the possibility of the type change.

Let's take a look at this example:

 

There may be many changes in this type, which may cause this type to be constantly modified. But if it is changed to the following structure, each change is only a certain type of change.

 

These methods can be used to solve the code problems in the query tool. Let's summarize the learned object-oriented principles:

1. encapsulate what varies)

2. The code should depend on the Interface rather than implementation (code to an interface rather than to an implementation)

3. Each type in the program has only one change point (each class in your application shocould have only one reason to change)

Next we will go back to the code of the query tool (not finished, To be continued ...)

Object-Oriented Analysis Design learning and exploration (6): good design = software flexibility (good design = flexible software)

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.