Move properties between objects

Classes tend to become bloated from taking on too many responsibilities.

(Move Method) Indicates the Move function

What it means: In a program, if a function is called or called in close proximity to a class or function outside its class, it needs to move the representation inside its function and create a new function in the reference class that behaves like the function.

When two classes have complex associations or too much cooperation between functions and form a high degree of coupling, this is very fatal, we need to timely deconstruct this coupling, that is, split it, make it loose, and form a desired pattern in software development, “high cohesion, low coupling”.

  • Examine all features (fields/functions) in the source class that are used by the source function and consider whether they should also be moved.
  • Check the subclass or superclass of the source class to see if there are other declarations for the function.
  • Declare this function in the target class, copy the code from the source function into the target function, and adjust it to work properly.
  • Compile the target class, decide how to properly reference the target object from the source function, modify the source function to make it a pure delegate function, and compile the test.

If the function to be moved from the source class uses a function from the source class or another field, there are several ways to do this:

  1. Move this feature to the target class as well.
  2. Create or use a reference relationship from the target class to the source class.
  3. Pass the source object as a parameter.
  4. If all you need is a variable, pass it as a parameter.

Move Field

Meaning: When a field is referenced multiple times by other classes, it is migrated. Create a new field in the target class and modify all users of the source field to use the new field.

Extract Class

What it means: When a class is bloated or has too many meanings and does what both classes do, you need to create a new class and move related fields and functions from the old class to the new class.

A class should be a clear abstraction that handles some clear responsibilities.

Inline Class Inline Class Inline Class

What it means: If the functions in the class are clear or not being called too often, you can remove the class and put the functions directly in the original class.

Introduce Local Extension

What it means: If you provide functions for a service class, but you cannot modify the class, you need to create a new class that contains these functions and make the extension a subclass or wrapper of the source class.

A native extension is a separate class, but a subtype of the extended class that provides all the features of the source class, plus new additions. Wherever the source class is used, it can be replaced.

Principle: “Functions and data should be uniformly encapsulated”,

Good strategy: Since users of the wrapper class really shouldn’t care that the wrapper class exists, they should be able to treat the wrapper class the same as the primitive class.

Small remarks

A person’s struggle, like pregnancy, a long time, will always be seen.

Life is short, I don’t want to pursue what I can’t see, I just want to grasp what I can see.

I am zai said, thank you for reading, if it is helpful to you, trouble point like, forwarding thank you.