In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the six principles of Android design pattern SOLID". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the six principles of Android design pattern Solid"?
Single responsibility principle definition
Definition:
Make sure that the singleton class has only one instance, and that the singleton class provides a functional interface for other classes to get the unique instance.
Explanation: a class is responsible for only one responsibility, and there should be no more than one cause of class change.
Code interpretation
For example, a class records the names of some foods, but also records the practices of foods. The former belongs to the business object and the latter belongs to the business logic. According to the principle of single responsibility, we need to separate the business from the data.
Failure to observe single principle public class Foods {private String fish; private String meat; public String getFish () {return fish;} public void setFish (String fish) {this.fish = fish;} public String getMeat () {return meat;} public void setMeat (String meat) {this.meat = meat;} public void RedCookedFish () {/ / do something... } public void RedCookedMeat () {/ / do something... }} abide by the single principle public class Foods {private String fish; private String meat; public String getFish () {return fish;} public void setFish (String fish) {this.fish = fish;} public String getMeat () {return meat;} public void setMeat (String meat) {this.meat = meat;}} public class Practices {public void RedCookedFish () {/ / do something... } public void RedCookedMeat () {/ / do something... }} definition of opening and closing principle
Definition: a software entity such as classes, modules, and functions should be open to extensions and closed to modifications.
When a system has new needs, we do not want to modify the original functional class, resulting in the destruction of the original logic, so that the emergence of a new BUG, so we design the system as far as possible through extension to achieve new functions, that is to say, let us make good use of inheritance and interface.
Code interpretation
For example, there is an animal with two attributes of name and movement. when it is an adult, it needs to make friends with the opposite sex and increase the function of reproduction. We extend new functions by means of inheritance or interface, and abide by the principle of opening and closing.
Public class Animal {private String mName; private String mMovementMode; public Animal (String mName,String mMovementMode) {this.mName = mName; this.mMovementMode = mMovementMode;} public String getmName () {return mName;} public String getmMovementMode () {return mMovementMode;}} public class Multiply extends Animal {public Multiply (String mName,String mMovementMode) {super (mName, mMovementMode) } public void MultiplyMethod () {/ / do something... Definition of Richter substitution principle
Definition 1: if for every object o1 of type T1, there is an object of type T2 such that all programs defined in T1
P when all objects o1 are replaced by O2, the behavior of program P does not change, so type T2 is a subclass of type T1.
Type.
Definition 2: all references to the base class must be able to transparently use the objects of its subclasses
Explanation: for example, abstract methods of abstract classes, subclasses must implement
Code interpretation
For example, an animal has an attribute of a mode of movement, while other specific animals, such as fish and eagles, need to inherit their methods and realize their own way of movement.
Public abstract class Animal {public abstract void MultiplyMethod (String Multiply);} public class Fish extends Animal {@ Override public void MultiplyMethod (String Multiply) {/ / set MultiplyMethod}} dependency inversion principle definition
Definition: high-level modules should not rely on lower-level modules, both should rely on their abstractions; abstractions should not rely on details; details should rely on abstractions.
The core idea of the principle of dependency inversion is interface-oriented programming.
Code interpretation
For example, the popular programming language at this stage, java,c,python, etc., but with the passage of time, with the development of science and technology, new languages will be produced according to different needs. If we need to add them one by one in a class, that's too troublesome, and it's a lot easier to configure with interfaces.
Failure to comply with dependency leads to class C {public String get () {return "C";}} class Java {public String get () {return "Java";}} class Python {public String get () {return "Python";}} class GetLanguage {public GetLanguage () {} public void getLanguage (C c) {Log.d ("Language", c.get ()) } public void getLanguage (Java java) {Log.d ("Language", java.get ());}} GetLanguage language = new GetLanguage (); language.getLanguage (new C ()); language.getLanguage (new Java ()); abide by the principle of dependence leads to
Define Interfac
Public interface ILanguage {String get ();} public class Language {public void getLanguage (ILanguage iLanguage) {Log.d ("Language", iLanguage.get ());}} Language language = new Language (); language.getLanguage (new ILanguage () {@ Override public String get () {return "C";}})
Definition of interface isolation principle
Definition: 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.
To put it simply, a class only needs to implement the methods it needs, and irrelevant methods do not need to implement
Code interpretation
For example, an interface realizes all the movements of animals, such as running, climbing, swimming, flying and jumping. The eagle only needs to fly, and the dog only needs to run, but if you implement this interface, you must implement all the methods, it will be very bloated, and some methods do not need to be implemented. We just need to split them into four interfaces, different animals, to achieve different ways of movement.
Failed to comply with interface isolation principle public interface IAnimal {void run (); void swim (); void climb (); void fly ();} class Dog implements IAnimal {@ Override public void run () {} @ Override public void swim () {} @ Override public void climb () {} @ Override public void fly () {} abide by interface isolation principle public interface Swim {void swim () } public interface Run {void run ();} public interface Climb {void climb ();} public interface Fly {void fly ();} class Dog implements Run {@ Override public void run () {}} Demeter principle definition
Definition: an object should have the least understanding of other objects.
Code interpretation
Suppose a class implements the four methods of addition, subtraction, multiplication and division, and at the same time determines the type of the value of the operation in the method of addition, subtraction, multiplication and division. The scope judgment is realized in the type judgment. We only need to expose four methods of addition, subtraction, multiplication and division, and the rest do not need to be exposed.
Public class Operation {public Object Add (Object num1,Object num2) {Object flag = JudgeType (num1,num2); int num = (Integer) flag; switch (num) {case 0: return (Integer) num1 + (Integer) num2; case 1: return (Double) num1 + (Double) num2; default: return null } private void Sub (Object num1,Object num2) {} private void Ride (Object num1,Object num2) {} private void Division (Object num1,Object num2) {} private Object JudgeType (Object num1,Object num2) {if (num1 instanceof Integer) {return 0;} else if (num1 instanceof Double) {return 1;} return 3 } / / private boolean JudgeIntRange (int num) {/ / if (num
< 65535 && num >-65535) {/ / intFlag = true;// return true;//} / / intFlag = false;// return false;//}} Operation operation = new Operation (); Log.d ("Result=", operation.Add (1) + "); at this point, I believe you have a deeper understanding of" what are the six principles of Android design pattern Solid ". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.