AD

Reading notes - six design principles, "Richter replacement principle"

More simply, as long as the parent class to child class where there may appear, but will not replace the sub-class of any error or exception, the user may simply need to know is the parent class or subclass. However, in turn, to die, there are sub-classes where the parent class may not be able to adapt.

Richter replace the definition of the principle of a succession of good standard, a simple definition, including the four meanings:

1, sub-class must fully realize the parent class method

Parent: AbstractGun

public abstract class AbstractGun {

        abstract void shoot();
}

Subclass of the rifle: Rifle

public class Rifle extends AbstractGun {

        @Override
        void shoot() {
                System.out.println(" Rifle shooting  ...");
        }
}

Sub-class of machine guns: MachineGun

public class MachineGun extends AbstractGun {

        @Override
        void shoot() {
                System.out.println(" Machine gun shooting  ...");
        }
}

Soldier classes: Soldier

public class Soldier {

        private AbstractGun gun;

        public void setGun(AbstractGun _gun) {
                this.gun = _gun;
        }

        public void kill() {
                System.out.println(" Soldiers started shooting  ...");
                this.gun.shoot();
        }
}

Application examples:

public class Run {

        public static void main(String[] args) {
                Soldier sanmao = new Soldier(); //  Create a new San Mao  
                sanmao.setGun(new Rifle()); //  Give a rifle  
                sanmao.kill(); //  San Mao begin in space  
        }
}

Note: In class the other classes must call the parent class or interface to use, if it can not be used, it indicates a violation of the principle of class design.

Common abnormal inheritance example:

The application then the above example, if we want a toy gun to the San Mao, then the new class: ToyGun

public class ToyGun extends AbstractGun {

        //  Toy guns is not shooting, but in order to San Mao  , Must inherit from the parent class  
        //  Inheritance and implementation requirements, what to do  ? We continue to be a general compulsory  
        @Override
        void shoot() {
                //  The nature of the toy gun is unable to shoot, so I cannot really implement the method  
        }
}

As often happens is usually applied in the case, according to the principle of LSP, will have to pay attention: If the child class can not fully realize the parent class method, or the parent class method in subclasses of some distortion, it is recommended that broken relationship between father and son inherited, with dependence, aggregation, composition and other relationships instead of inheritance.

So the situation above, the proposed scheme is: ToyGun from the inheritance, the establishment of an independent parent class (such as AbstractToy), in order to achieve code reuse, you can associate with AbstractGun relations commission. For example, you can declare in AbstractToy the sound and shape are entrusted to AbstractGun processing, toy Well, shapes and sounds and the guns must be the same, then the two base classes for extension under the sub-class of free people

2, sub-class can have its own personality

Sub-class of course has its own methods and properties, but in principle in LSP, one can not turn with the use. Occur in sub-class where the parent class does not necessarily be competent.
标签: succession, string args, public void, principle, design principles, abstract class, main string, interface, reading notes, inheritance example, application examples, richter, abstract void, toy gun, san mao, machine guns, machinegun, gun gun, toygun, new soldier
分类: Tech
时间: 2010-07-04

相关文章

  1. Reading notes - six design principles of a "single responsibility principle"

    The definition of a single responsibility principle: there should be one and only one type of change causes. O ...
  2. Design principles of the principle of opening and closing

    Top of page Top】 Itpub http://tech.it168.com/a2009/0727/612/000000612707.shtml Open-close principle is the fou ...
  3. Reading Notes explain design pattern mode (Interpreter pattern)

    Interpreter Pattern: Given a language, define a representation of its grammar, and define an interpreter, the ...
  4. Reading Notes template design pattern approach pattern (Template)

    Template Method Pattern: Define an algorithm / process framework, but will defer some steps to subclasses to i ...
  5. Reading Notes prototype design pattern model (Prototype)

    Prototype model: a prototype by cloning to create new objects. Sample code: package designpattern.prototype; p ...
  6. Software design principles

    Recently read "java desion" This book, which describes 26 kinds of design patterns and several desig ...
  7. Object-oriented software design principles

    Excellent object-oriented software should be designed to achieve the following four objectives: Maintainabilit ...
  8. Some software design principles

    Don't Repeat Yourself (DRY) DRY is a simple rule, and most easily understood. But it is probably the hardest a ...
  9. Design principles - the principle of substitution Richter

    First, what the principles of the Richter magnitude substitution principle of strict substitution expression i ...
  10. "Software Architecture Design" reading notes

    Recently read "software architecture color design" of this book, harvest story, happened to see this ...
  11. java basic design principles and common design patterns notes

    The use of basic design principles and design patterns reasons: Reuse and maintainable design principles: 1. O ...
  12. The Open-Closed Principle (OCP) - OO design principles of open and close

    The Open-Closed Principle (OCP) - OO design principle of opening and closing: Software entities (classes, modu ...
  13. The six principles of design patterns - Single Responsibility Principle (SRP)

    Open Closed Principle (Open Closed Principle) is the Java world, the most basic design principles, which guide ...
  14. javascript DOM programming arts reading notes

    javascript DOM programming arts reading notes The first part of the directory Chapter 1 Origin of JavaScript 1 ...
  15. Richter substitution principle (Liskov Substitution Principle, LSP)

    4 Richter substitution principle (Liskov Substitution Principle, LSP) 4.1 What is the principle of substitutio ...
  16. API的设计原则 API Design Principles

    API Design Principles One of Qt's most reputed merits is its consistent, easy-to-learn, powerfulAPI. This docu ...
  17. Object-oriented design principles 11

    What is object-oriented design? Contains what? The benefits of it are what? Required to pay what you do? In to ...
  18. Object-oriented design principles

    Design patterns are all different packaging variability, thus allowing the system to achieve different angles ...
  19. Design Patterns design principles

    Head First Design Principles in Summary: A, packaging changes in two more with the combination of three less i ...