Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The definition of Seven principles of java Software Design

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the definition of the Seven principles of java Software Design". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Open and close principle 1.1. Define that a software entity such as classes, modules and functions should be open to extension and closed to modification. Build the framework with abstraction and extend the details with implementation. Advantages: improve the reusability and maintainability of the software system, code demonstration public interface ICourse {Integer getId (); String getName (); Double getPrice ();} public class JavaCourse implements ICourse {private Integer Id; private String name; private Double price; public JavaCourse (Integer id, String name, Double price) {this.Id = id; this.name = name; this.price = price } public Integer getId () {return this.Id;} public String getName () {return this.name;} public Double getPrice () {return this.price }} / / if you need to add a function to get a discount price on the basis of the original function, it is recommended not to modify the original API. Here you can inherit the original JavaCourse class, / / extend public class JavaDiscountCourse extends JavaCourse {public JavaDiscountCourse (Integer id, String name, Double price) {super (id, name, price);} public Double getDiscountPrice () {return super.getPrice () * 0.8 on top of the original function. 2. Rely on inversion principle 2.1, define `high-level modules should not rely on lower-level modules`, and both should rely on their abstraction. Abstraction should not rely on details, details should rely on abstractions. Program against the interface, not against the implementation. Advantages: it can reduce the coupling between classes, improve the stability of the system, improve the readability and maintainability of the code, and reduce the risk caused by modifying the program. 2.2, code demonstration

Public interface ICourse {void studyCourse ();} public class JavaCourse implements ICourse {@ Override public void studyCourse () {System.out.println ("Geely is taking Java courses");}} public class PythonCourse implements ICourse {@ Override public void studyCourse () {System.out.println ("Geely is learning Python courses");}} public class Geely {public void setiCourse (ICourse iCourse) {this.iCourse = iCourse;} private ICourse iCourse Public void studyImoocCourse () {iCourse.studyCourse ();}} public class Test {public static void main (String [] args) {Geely geely = new Geely (); geely.setiCourse (new JavaCourse ()); geely.studyImoocCourse (); geely.setiCourse (new FECourse ()); geely.studyImoocCourse () }} 3, single responsibility principle 3.1, definition there should be no more than one cause of class change. A class / interface / method is responsible for only one responsibility. Advantages: reduce the complexity of the class, improve the readability of the class, improve the maintainability of the system, and reduce the risk caused by change. Code demonstration / / deprecated writing public class Bird {public void mainMoveMode (String birdName) {if ("ostrich" .equals (birdName)) {System.out.println (birdName+ "walk on feet");} else {System.out.println (birdName+ "fly with wings") } public class FlyBird {public void mainMoveMode (String birdName) {System.out.println (birdName+ "fly with wings");} public class WalkBird {public void mainMoveMode (String birdName) {System.out.println (birdName+ "walk on feet");}} public class Test {public static void main (String [] args) {/ / not recommended for Bird bird = new Bird () Bird.mainMoveMode ("wild goose"); bird.mainMoveMode ("ostrich"); / / recommended writing: single function FlyBird flyBird = new FlyBird (); flyBird.mainMoveMode ("wild goose"); WalkBird walkBird = new WalkBird (); walkBird.mainMoveMode ("ostrich") 4. Interface isolation principle 4.1, defining multiple specialized interfaces instead of a single master interface. The client should not rely on interfaces it does not need. The dependency of one class on another should be based on the smallest interface. Build a single interface, not a big, bloated interface. `refine the interface as much as possible, and minimize the number of methods in the interface`. The principle of moderation must be moderate. Advantages: it accords with the design idea of high cohesion and low coupling, so that the class has good readability, expansibility and maintainability. Code demonstration public interface IAnimalAction {void eat (); void fly (); void swim ();} / / split an interface into three interfaces public interface IEatAnimalAction {void eat ();} public interface IFlyAnimalAction {void fly ();} public interface ISwimAnimalAction {void swim () } public class Dog implements ISwimAnimalAction,IEatAnimalAction {@ Override public void eat () {} @ Override public void swim () {}} 5, Demeter principle 5.1, defining an object should have the least understanding of other objects, also known as the least knowledge principle. `minimize the coupling between classes. Advantage: reduce the coupling between classes. Code demonstration public class Boss {public void commandCheckNumber (TeamLeader teamLeader) {teamLeader.checkNumberOfCourses ();}} public class Course {} public class TeamLeader {public void checkNumberOfCourses () {List courseList = new ArrayList (); for (int I = 0; I < 20; iTunes +) {courseList.add (new Course ()) } System.out.println ("the number of online courses is:" + courseList.size ());}} public class Test {public static void main (String [] args) {Boss boss = new Boss (); TeamLeader teamLeader = new TeamLeader (); boss.commandCheckNumber (teamLeader) 6, Richter substitution principle 6.1, defining all references to the base class (parent class) must be able to transparently use the objects of its subclasses. Generally speaking, a subclass can extend the function of the parent class, but it cannot change the original function of the parent class. The Richter substitution principle means that if a base class object (parent class) is replaced with its subclass object in software, the program will not produce any errors and exceptions, and vice versa, if a software entity uses a subclass object, then it may not be able to use the base class object. The Richter substitution principle is one of the important ways to realize the opening and closing principle. Because the subclass object can be used wherever the base class object is used, the base class type is used to define the object in the program as far as possible, and the subclass type is determined when the program is running, and the parent class object is replaced by the subclass object. For example: I like animals, then I must like dogs, because dogs are a subclass of animals. But I like dogs, I can not conclude that I like animals, because I do not like mice, although it is also an animal. 6.2. Implementation principle 1) `A subclass can implement the abstract methods of the parent class, but cannot override / override the non-abstract methods of the parent class`. 2) you can add your own unique methods to the subclass. 3) when the subclass overrides or implements the method of the parent class, the precondition of the method (that is, the formal parameter of the method) is more relaxed than the input parameters of the parent method. 4) when the method of the subclass implements the abstract method of the parent class, the post condition of the method (that is, the return value of the method) is more stringent than the parent class.

Reference: seven design principles of Java

7. Composition / aggregation reuse principle 7.1, define the use of composition and aggregation as far as possible, rather than integration to achieve the purpose of reuse. The principle is to use some existing objects in a new object and make it part of the new object, which can reuse existing functions by delegating to these objects. this is the end of the definition of the Seven principles of java Software Design. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report