[Repost] a summary of oprinciples

xiaoxiao2021-03-06  77

Introduction

OO Design is more than just using an OO language. Over the years many bright programmers have built up a collection of rules that help to build well designed maintainable code. This article lists the main rules of OO programming. The intention is to inspire the reader To Think About these rules and make further reading. There is a lot of material on the Web That Drills Down Into More Details with Plenty Of Examples.

Ivar Jacabson said "All systems change during their life cycles. This must be borne in mind when developing systems expected to last longer than the first version". In other words software requirements change with time. The goal of Object Orientated Design is to program in Such a Way That Such Changes to the Software Area Predictable and do not make a large Impact in The Program. in Other Words It Should Be Stable in The Presence of Change

Bad design is characterized by

A Single Change Affects Many Other Parts of The System (Rigidity)

A Single Change Affects Unexpected Parts of The System (Fragility)

IT is hard to reuse in another application (immobility)

THE Dependency Inversion PrinciPle DIP (dependencies)

Imagine you have a simple database program. You do not want to change the entire application when changing the database. This principle is targeted at removing such unwanted interdependency that can cause a design to be fragile. The rule states

High Level Modules Should Not Dend Upon Low Level Modules. Both SHOULD Depend Upon Abstracts

Abstractions Should Not Dend Upon Details. Details Should Depend Up on Abstracts

Booch said "All well structured object orientated architectures have clearly-defined layers with each layer providing some coherent set of services through a well defined and controlled interface." In other words design applications in layers where high level layers call lower level layers using Abstract interfaces ..........................

GOOD Dependencies Aree. In Other Words They Are Stable. We Would Like To Base Our Architectural Design Around Stable, Non-Volatile Modules.

The open-close PrinciPle OCP (KTR)

Software Entities (Classes, Modules, Functions etc) Should Be Open for Extension, But Closed for Modification

In other words design classes that never change. When a new requirements come add new code and do not edit existing code. It is not possible to close against all possible changes. Therefore an experienced developer needs to understand the possible future wishes of users in Order to make Strategic Closure. There Are Two Ways of Closure:

.................... ..

Using Data Driven Approach To Achieve Closure

Liskov Substitution PrinciPle LSP (Principles of Li Shi)

Every function that operates upon a reference or pointer to a base class should be able to operate on derivatives of that base class without knowing it. This means that virtual member functions of the derived class must expect only all the corresponding member functions of the base class . In other words any function that uses a base class must not be confused when a derived class is substituted for the base classThis is a difficult principle to apply. to conform avoid overwriting base class functions because this involves programming with details, instead try to program In abstracts

IF this is violated kiln.

Heuristics and convention

Make all member variables Private: Otherwise no function that calls the class can be closed to change For example a status variable can change from Boolean to an enumeration, if this is not handled as a property then we can not close status This is called encapsulation.. .

No Global Variables. Because misbehaving modules may write erroneous data to such global variables whose effect can be felt in many places throughout the program. Sometimes Global variables are useful eg cout and cin in c . If they do not violate the open close principle then sometimes They area

Stability Dependencies PrinciPle SDP (Stable Dependence)

The Dependencies Between Packages in A Design Of The package. A packages..............

Some volatility is necessary if the design is to be maintained. This is achieved by using the Common Closure Principle, in this way we design packages to be volatile and we expect them to change. Any package that we expect to be volatile should not be depended upon by a package that is difficult to change.Some things we do not want to change. For example architectural decisions should be stable and not at all volatile. Therefore classes that encapsulate the high level design should be stable.

The Stable Abstractions PrinciPle SAP (Stable Abstract Principle)

Packages That Are Maximally Stable Should Be Maximally Abstract. Instable packages SHOULD BE CONCRETE. The Abstract Of a package shop be in proportion to it's stability

Common Reuse PrinciPle CRP (common general principles)

If you reuse one class of a package, you reuse the. This Because Any Package Delivered Contains A Released Set of Class, Therefore a change, of the entire package.

The Reuse / Release Equivalence PrinciPle Rep (Reuse Publishing Equipment Principle)

The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This principle is important when there are several teams working on an application. To avoid one team disrupting another all packages used are tested and Released. in this way. INTRODUCTION OF MODIFIED PACKAGES IS IN A Controlled Way.

The Common Closure Princi CCP (commonly closed principle)

The classes in a package should be closed together against the same kinds of changes. Any change in a package affects all classes in the package. Just like a well organized team has a common goal because they all have to work together. This principle means that YOULD HAVE A Common Strategic Closure Concept Used Through All Classes in a Package Because The Have To Be Released All TOGETHER.THE Stability Dependencies PrinciPle SDP (Stable Dependence)

The Dependencies Between Packages in A Design Of The package. A packages..............

Designs That Are Highly Interdependent Tend To Be Rigid, Not Reusable and Hard To Maintain

The Acyclic Dependencies PrinciPle (ADP) (Wu Ring Dependence)

The dependency structure between package must be a Direct Acyclic Graph (DAG). This means that if you plot out all packages it should be possible to arrange the dependencies to always point from top to bottom. Also it should not be possible to follow any lines Because Such Packages Would Berap Such Packages Would The Be Released All At The Same Time Defeating The Object of Having The as Separate Packages

The Interface SegRegation PrinciPle ISP (Interface Isolation Principle)

CLIENTS SHOULD NOT BE forced to de Depend Upon interfaces That they don't.

This principle deals with the disadvantages of fat interfaces Fat interfaces are not cohesive In other words the interfaces of classes should be broken into groups of member functions Each groups servers a different set of clients Separation can be achieved by....:

Separation THROUGH DELEGATION

Separation Through Multiple Inheritance

IF this principle is violated kiln all clientspolyad vs monad

Monad is when properties are grouped into 1 single object that is then passed in a function parameter. Unfortunately this brings a dependency across all properties in that single object. Therefore its better to pass smaller objects (Polyad), in this way the dependencies are broken INTO SMALLER Groups.

Interface pollution

As we build up classes there is a tendency for us to add functionality that is specific for a particular implementation. In this way the interface gets populated by functions and properties that are not required if the class was in a different context, thus making the interface Fat. in this Way this Violates The Lisk Substitution Principle. Separate Clients Means Separate Interfaces

THERE IS A Backward Forces. For Example a User May Wish To Add A Trivial Extra Function That Cannot Be EXACTLY POSIONED IN EXISTING Interface

转载请注明原文地址:https://www.9cbs.com/read-91425.html

New Post(0)