Six principles of object-oriented design
(Reference) Six principles of object-oriented design _fblog CSDN blog _object-oriented design principles)
- 1. Single function principle, a class can change for only one reason
- 2. The opening and closing principle is open to expansion and closed to modification
- 3. Richter replacement principle: for a class, in the software, replace the parent class with its child class, and the behavior of the program does not change
- 4. The principle of dependency switching, "design should rely on abstraction rather than concretization", and think with abstraction
- 5. Interface isolation principle: large interfaces are broken up into multiple small interfaces and decoupled
- 6. According to the Demeter principle, an object should know as few other objects as possible, and the object should contain as few other objects as possible
Room database (error records made during use)
The id value returned when insert ing data is not assigned to the data source list, resulting in an error when the newly created data is modified and deleted immediately, because the modification and deletion of room database are based on id
Use of TabLayout+ViewPager (Reference) Tablayout uses complete solutions, one is enough - simple book)
Tablayout inherits from HorizontalScrollView and is used as a page switching indicator. It is widely used in App because of its simple use and powerful function.
Common attributes:
app:tabIndicatorColor : Indicates the color of the line app:tabIndicatorHeight : Indicates the height of the line app:tabSelectedTextColor : tab Font color when selected app:tabMode="scrollable" : The default is fixed,changeless; scrollable: Scrollable
Singleton mode: this mode involves a single class, which is responsible for creating its own objects and ensuring that only a single object is created. This class provides a way to access its unique object, which can be accessed directly without instantiating the object of this class.
Implementation of singleton mode in kotlin (Reference) Five singleton modes under Kotlin - brief book)
- Hungry Han style
-
//Java implementation public class SingletonDemo { private static SingletonDemo instance=new SingletonDemo(); private SingletonDemo(){ } public static SingletonDemo getInstance(){ return instance; } } //Kotlin implementation object SingletonDemo
-
- Lazy style
-
//Java implementation public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo(){} public static SingletonDemo getInstance(){ if(instance==null){ instance=new SingletonDemo(); } return instance; } } //Kotlin implementation class SingletonDemo private constructor() { companion object { private var instance: SingletonDemo? = null get() { if (field == null) { field = SingletonDemo() } return field } fun get(): SingletonDemo{ //Careful friends must have found that getInstance is not used as the method name here because there is a getInstance method inside when the associated object is declared, so you can only use other names return instance!! } } }
-
- Thread safe lazy
-
//Java implementation public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo(){} public static synchronized SingletonDemo getInstance(){//Use sync lock if(instance==null){ instance=new SingletonDemo(); } return instance; } } //Kotlin implementation class SingletonDemo private constructor() { companion object { private var instance: SingletonDemo? = null get() { if (field == null) { field = SingletonDemo() } return field } @Synchronized fun get(): SingletonDemo{ return instance!! } } }
-
- Double check lock
-
//Java implementation public class SingletonDemo { private volatile static SingletonDemo instance; private SingletonDemo(){} public static SingletonDemo getInstance(){ if(instance==null){ synchronized (SingletonDemo.class){ if(instance==null){ instance=new SingletonDemo(); } } } return instance; } } //kotlin implementation class SingletonDemo private constructor() { companion object { val instance: SingletonDemo by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { SingletonDemo() } } }
-
- Static inner class
-
//Java implementation public class SingletonDemo { private static class SingletonHolder{ private static SingletonDemo instance=new SingletonDemo(); } private SingletonDemo(){ System.out.println("Singleton has loaded"); } public static SingletonDemo getInstance(){ return SingletonHolder.instance; } } //kotlin implementation class SingletonDemo private constructor() { companion object { val instance = SingletonHolder.holder } private object SingletonHolder { val holder= SingletonDemo() } }
-