A app architecture learning notes

32 阅读2分钟

Key Point 1:Keep in mind that you don't own implementations of Activity and Fragment;rather,these are just glue classes that represent the contract between the Android OS and your APP.The OS can destory them at any time based on user interaction or because of system conditions like low memory.To provide a satisfactory user experience and a more manageable app maintenance experience,is's best to minimize your dependency on them.

Key Point 2:Another important principle is that you should drival your UI form data models,preferably persistent models.Data models represent the data of an APP,They're independent from the UI elements and other components in your app,this means that they are not tied to the UI and app component lifecycle,but will still be destoryed when the OS decides to remove the app's process from memory. Persistent models are ideal for the following reasons:

  1. Your users don't lose data if the Android OS destorys your app to free up resources

  2. Your app continues to work in cases when a network connection is flaky or not available.

Key Point 3:When a new data type is defined in your app,you should assign a Single Source of Truth(SSOT) to it.SSOT is the owner of that data,and only the SSOT can modify or mutate it,To achieve this,the SSOT exposes the data using an immutable type,and to modify the data,the SSOT exposes functions or receive events that other types can call. This pattern brings multiple benefits:

  1. It centralize all the changes to a particular type of data in one place.

  2. It protects the data so thart other types cannot tamper with it

  3. It makes changes to the data more traceable.Thus,bugs are easier to spot

Summarize:This models principle are based 6 principle of software design

1.Single Responsibility Principle:The single responsibility principle is also knwon as the single-function principle,thit is,there is no more than one reason for class change,In laymans's terms:a class is only responsible for one responsibility.

  1. Liskov Subsititution Principle LSP:One of the basic principles of object-oriented design where any ase class can appear,subclasses must appear.LSP is the cornerstone of inheritance and reuse.Only when the derived class can replace the base class,the function of the suftware unit is not affected,the base class can be truly reused,and the derived class can also add new behavior based on the base class.(ie,if the parent class is a part of a function module,the child class is used instead of the parent class and the function module can run normally[The child class instance cal replace the parent class instance] Inheritance contains such a meaning:all methods that have been implemented in the parent class(as opposed to abstract methods)are actually setting a series of specifications and contracts,although it does not mandate that all subclasses must follow these Contracts,but if subclasses arbitrarily modify these non-abstract methods,they will cause damage to the entire inheritance system.The principle of Lee's replacement is to express this meaning.Inheritance,as one of the three characteristics of object-oriented,brings great convenience to programming,but also brings desadvantages,For example,using inheritance can bring intrusion to the program,the portability of the program is reduced,and the coupling between objects is increased.if a class is inherited by other classed,all the children must be considered when the class needs to be modified.Classes and after the parents class is modified,all functions involving subclasses may fail.

  2. Dependency Inversion Principle:Hight-level modules should not rely on low-level modules,both of which should rely on their abstractions;abstractions should not rely on details;details should rely on abstractions

  3. Interface Isolation Principle ISP(ISP-Interface Segregation Principle):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

  4. Law of Demeter Definition:

  5. Open-Closed Principle Definition:A software entity such as classes,modules,and functions should be open to extensions and closed modifications.