Initial Android design principles, Design patterns

Source: Internet
Author: User

Design principle one : single principle of responsibility

Definition: There should be only one reason for a class to cause it to change.

Advantages: Reduce the complexity of the class, improve the readability of the class, improve the maintainability of the system.

Example:

Super Human public class Superman {public void Maintains world peace () {//Superman maintains world Peace}} Extension: public class Animal {Publi    c void Breathe (String animal) {System.out.println (animal + "breathes in Air");    The public void Breathe2 (String animal) {System.out.println (animal + "breathes in water"); }    }

A condition that can violate a single responsibility principle: it requires simple enough logic to violate the principle of single responsibility at the code level .

The number of methods required in a class is small enough to violate the principle of single responsibility at the method level .

Design principles Two : the principle of substitution on the Richter scale (Liskov Substitution Principle, LSP)

Definition: subtypes must be able to replace their parent types.

This principle reflects the relationship between the parent class and the subclass, and is one of the most important and basic principles in object-oriented design.

Pros: Make code reuse easier

Old Carpenter class public class Oldcarpenter {public void Makefurniture () {System.out.println ("Make a chair!"); }}//Old Carpenter Apprentice class public class Youngfurniture () extends oldcarpenter{//Override parent class method @Override public void make            Furniture () {super.makefurniture ();        System.out.println ("Decorate the chair!"); }    }


Initial Android design principles, Design patterns

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.