CodeJourney Blog

website·Indexed 31 days ago

https://blog.codejourney.top/

A technical blog by Antvictor featuring tutorials on Java, Python, and software design principles. Content includes a 'Seven Day Python' introductory series, guides on Nacos integration with SpringBoot and SpringCloud, and explanations of design principles such as the Open-Closed Principle (OCP), Liskov Substitution Principle, and Single Responsibility Principle.

Tokens
16.6K
Snippets
87
Records
110
Agent score
48%

What's inside CodeJourney

  1. Understand the Single Responsibility Principle (SRP)

    The Single Responsibility Principle (SRP) states that a class should have one, and only one, reason to change. In practice, this means a class should be responsible for a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated within the class.
  2. Understand and apply the Single Responsibility Principle (SRP)

    The Single Responsibility Principle (SRP) states that a class should have one, and only one, reason to change. This means a class should be responsible for only one type of functionality.

    When a class has too many responsibilities, changes to one responsibility may inadvertently introduce bugs or errors into others. It is important to note that SRP does not mean a class can only perform one single action, but rather that it should perform actions of the same type or category (e.g., a Chef class handles various cooking tasks, but should not handle order taking or payment).

  3. Understand the Single Responsibility Principle (SRP)

    The Single Responsibility Principle (SRP) states that a class should have one, and only one, reason to change. In practical terms, this means a class should be responsible for a single part of the functionality provided by the software, and that responsibility should be entirely defined by the class.
  4. Understand the relationship between Classes and Objects in Java

    In Java, a class is a blueprint used to define a new type by abstracting common characteristics and behaviors of a group of entities. An object is a concrete instance of a class that exists in memory and possesses state, identity, and behavior. While the class defines the structure, the object is the actual entity created by the developer during runtime.
  5. Understand and apply the Liskov Substitution Principle (LSP)

    The Liskov Substitution Principle (LSP) is a fundamental principle of object-oriented design used to constrain inheritance. It states that if for every object o1, there exists an object o2 such that replacing o1 with o2 does not change the program's behavior, then o2 is a subtype of o1.

    In practical terms: a parent class should be replaceable by its child class without affecting the business logic or correctness of the application. If a subclass cannot fully implement the business logic of the parent class, inheritance should be avoided in favor of dependency, aggregation, or composition.

  6. Understand and apply the Liskov Substitution Principle (LSP)

    The Liskov Substitution Principle (LSP) is a fundamental principle of object-oriented design that constrains the use of inheritance. It states that if for every object o1, there exists an object o2 such that replacing o1 with o2 does not change the program's behavior, then o2 is a subtype of o1.

    In practical terms: a derived class must be substitutable for its base class without affecting the correctness or business logic of the application. If a subclass cannot fully implement the business logic of the parent class, inheritance should be avoided in favor of dependency, aggregation, or composition.

  7. Create private methods and attributes

    To make a method or attribute private (hidden from outside the class), prefix its name with two underscores (__). Private members can still be called by other methods within the same class.
    class Penguin(Bird):
        def __cry(self):
            print("I only bark at home") # Private method
    
        def cry(self):
            print("This is my public cry")
            self.__cry() # Internal call is allowed
    
    p = Penguin("small wings", "villus")
    p.cry()    # Works
    # p.__cry() # Raises AttributeError
  8. Install Python on macOS and Windows

    Download the installation package (version 3.6 or higher) from the official Python website (python.org).

    • macOS: Run the installer and then verify the installation by opening the terminal and typing python3.
    • Windows: Run the installer and ensure the option to 'Add Python to environment variables' (配置环境变量) is checked. Verify the installation by typing python in the command prompt (cmd).
    # macOS verification
    python3
    
    # Windows verification
    python
  9. Control loop execution with break and continue

    Use break to terminate a loop entirely when a specific condition is met. Use continue to skip the remainder of the current iteration and jump immediately to the next cycle of the loop.
    boxes = [1, 2, 3, 4, 5, 6]
    
    # Example of break: stops loop when i is 3
    for i in boxes:
        if i == 3:
            break
        print(i) # Prints 1, 2
    
    # Example of continue: skips 3 and continues with 4
    for i in boxes:
        if i == 3:
            continue
        print(i) # Prints 1, 2, 4, 5, 6
  10. Install Nacos from source or binary

    Nacos can be installed by cloning the source code and compiling it with Maven, or by downloading the pre-compiled binary distribution. Using the pre-compiled zip/tar.gz package is recommended for easier version selection and to avoid manual compilation.
    # Option 1: Install from source
    git clone https://github.com/alibaba/nacos.git 
    cd nacos/
    mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U 
    cd distribution/target/nacos-server-$version/nacos/bin
    
    # Option 2: Install from binary (Recommended)
    unzip nacos-server-$version.zip # or tar -xvf nacos-server-$version.tar.gz
    cd nacos/bin
  11. Import and use Python modules

    A Python file acts as a module that can be imported into other files to reuse its functions. You can import a module directly, use an alias for the module, or import specific functions from a module.
    # say.py
    def say(name):
        print(f"hello, {name}")
    
    def say1(name):
        print(f"Hi, {name}")
    
    # call.py
    # Option 1: Direct import
    import say
    say.say("AntVictor")
    
    # Option 2: Import with alias
    import say as s
    s.say("AntVictor")
    
    # Option 3: Import specific functions
    from say import say, say1
    say("AntVictor")
    say1("AntVictor")