Six principles of object-oriented C # Comments

Property; what data needs to encapsulation class!

Methods: To achieve class which features (task)!

Design class is to complete the design attributes and methods!

Taking into account the angle of thinking of each person is different, there are differences in the methods used, the final design of the properties and methods Guangdong Ou difference. As long as the truth makes sense,

It is reasonable.

Object-oriented learning is learning a set of principles, methods, skills, experience, design patterns and so on! Advanced skills later point, relatively easy!

OOP principles:

1. Single Responsibility (Object clear responsibility principle): SRP, an object to be accomplished the task should be clear! Universal design is not class.

  Responsibilities problem: form class ---- obtaining user behavior and data operation, display relevant data.

            Data class ---- encapsulates data we need.

            ---- traffic class encapsulates various actions requested by the user.

Application: The main impact is that we think the perspective and content.

2. Open Closed Principle :( open extended, modified closed principle) OCP, that is to say after we design a project, the needs of users is constantly changing,

  If your project design is very good, when the change with the needs of your project's code changes very little or no modification. Only increase the pride of the module or class (extension).

  This is then everywhere in our development, we later learn various design patterns, design methods, many of which are extensions to expand around.

Application: The main impact is the overall design of our program, or the design of some modules. It means to learn the various modes, using a method.

For example, a simple factory will be mentioned!

Selector mySelector = new Selector (); that is to say when we are in a new object, the object in the programming stage is clear. When did it this way? If the object you want to use is constant, then the method to use as soon as possible.

However, we object if you want to use is not in itself may also select other objects Selector uniquely determined. At this time, we can not direct new. It is necessary to consider the object creation package! To a third party to complete. The third party may be a method, class, or some of the modules, third-party libraries.

Selector mySelector- [module] factory method or so, I was in the preparation stage of the proceedings are not sure when this object, the program is run stage is determined. This is also called delayed object is created.

Results: How to change within the factory method, there is no impact on our caller.

Changes in user demand is encapsulated into a factory method or module, so, we modify the program itself is not the time to changes in demand, this approach would follow the open closed principle.

3. Richter substitution principle (this is what we talked about to inherit used): ISP, after we explain this inheritance, you will understand where that is the parent class appears subclass can be used to replace. (This versatility, can be extended to the interface) That is, where the interface appears, can be replaced with an interface implementation class.

For example: Class A {}

      Class B:A{}

      Class C:A{}

[1] If A is a general category, we A a = new A ();

[2] If A is an abstract class, we have used A a = new B (); abstract class that is a type of Dress, must be followed is that subclass object point.

4. Demeter: LoD, the principle of minimum coupling (smallest known principles), mainly to complete decoupling. In fact, we explain various principles are interdependent and influence each other, which is why OOP difficult to learn and difficult to use.

Decoupling: in fact, the principle of opening and closing, but also to achieve decoupling method. All extension mode, all around decoupling.

Applications: behind us to project a layered, wind module, are decoupled.

The principle of isolation interfaces (interfaces minimization principle): When A and B module associated with the module, the interface is very large, this approach will not work!

Interface is a broad concept! Interface is a broad concept. If the method parameter too, when invoked very troublesome! When the method is defined, the general parameters of not more than 4 to 5.

Solution: entity class, with the object as an argument!

Broadly: For example, we packaged a function module, the first interface design, interface design, you do not design too much. Excessive interface design, means that objects are many, many ways.

6. Dependency Inversion Principle (DIP): that is, the upper, or "detail" since the underlying dependency "abstract."

  Objective: 1 may be extended as necessary.

        2. teamwork needs. Our architect or project leader, first create the framework of the project, completed in accordance with the core business writing business components various interfaces, it projects. Then the specific developer, to complete the development of specific details obtained in accordance with this framework and pull interfaces.          

Guess you like

Origin www.cnblogs.com/LiyuLi/p/12298209.html