Assignment 2 | reading notes -- software design principles and design patterns

Which course does this assignment belong to 2022 software code development technology
What are the requirements for this assignment Reading notes -- software design principles and design patterns
The goal of this assignment 1. Learn the classification of software design patterns and the basic contents of various patterns
2. Understand software design principles

Reading books

Zheng Archie, software Secrets: Design Patterns

Design pattern classification

Design pattern principle

  • "Opening and closing" principle
    • A software entity such as classes, modules and functions should be open to extensions and closed to modifications. Try to solve the demand change by extending the software entity, rather than modifying the existing code.
  • Single responsibility principle
    • A method and a class are only responsible for one responsibility. The program changes of each responsibility will not affect other programs.
  • Dependency Inversion Principle
    • High level modules should not rely on low-level modules. They should all rely on abstraction. Abstraction should not rely on concrete implementation, and concrete implementation should rely on abstraction.
  • Interface isolation principle
    • Classes and classes should be based on the smallest interface.
  • Richter substitution principle
    • The used base class can use inherited subclasses anywhere to perfectly replace the base class.
  • Dimitt's law
    • A class should minimize its dependence on other objects. The principle is low coupling and high cohesion. Only by making the coupling between each module as low as possible can the reuse rate of code be improved.

Builder pattern

In the past, the builder model was more often used.
Builder mode: Builder mode is to separate the complex object structure, so that each part can complete relatively independent work. The construction process is constrained by abstract classes and interfaces.

solve the problem

  • When the generated product object has complex internal structure.
  • When complex objects need to be separated from the representation and different representations may need to be created.
  • When it is necessary to hide the internal structure of the product from customers.

Advantages and disadvantages

  • Advantages: good encapsulation, using the builder mode can make the client do not need to know the details of the internal composition of the product; The builder is independent and easy to expand.
  • Disadvantages: redundant Builder objects and Director objects will be generated, consuming memory.


Code structure

Key components

  • builder: create and provide instances.
  • director: manage the dependencies of the built instances.

Entity of commodity

package com.demo.builder.model;
 * Product entity
public class MobilePackage {
    private float money;
    private int shortInfo;
    private String music;

    public float getMoney() {
        return money;
    public void setMoney(float money) { = money;
    public int getShortInfo() {
        return shortInfo;
    public void setShortInfo(int shortInfo) {
        this.shortInfo = shortInfo;
    public String getMusic() {
        return music;
    public void setMusic(String music) { = music;

Builder's abstract class

package com.demo.builder.base;
import com.demo.builder.model.MobilePackage;
 * Builder abstract class
public abstract class AbstractBasePackage {
    protected MobilePackage mobilePackage;
    public AbstractBasePackage() {
        this.mobilePackage = new MobilePackage();

Abstract builder's behavior

package com.demo.builder.itf;
import com.demo.builder.model.MobilePackage;
 * Abstract builder behavior
public interface IMobileBuilder {
    void buildMoney();
    void buildShortInfo();
    void buildMusic();
    MobilePackage getMobilePackage();

Builder entity

package com.demo.builder.itf;
import com.demo.builder.base.AbstractBasePackage;
import com.demo.builder.model.MobilePackage;
 * Builder entity
public class MobileBuilderImpl1 extends AbstractBasePackage implements IMobileBuilder{
    public void buildMoney() {
    public void buildShortInfo() {
    public void buildMusic() {
        this.mobilePackage.setMusic("Song 1");
    public MobilePackage getMobilePackage() {
        return this.mobilePackage;


package com.demo.builder.director;
import com.demo.builder.itf.IMobileBuilder;
import com.demo.builder.model.MobilePackage;
 * Construction director
public class MobileDirector {
    public MobilePackage createMobilePackage(IMobileBuilder mobileBuilder) {
        if (mobileBuilder != null) {
            return mobileBuilder.getMobilePackage();
        return null;

Implementation method

package main;
import com.demo.builder.director.MobileDirector;
import com.demo.builder.itf.MobileBuilderImpl1;
import com.demo.builder.itf.MobileBuilderImpl2;
import com.demo.builder.model.MobilePackage;
 * Created by Administrator on 2016/8/8.
public class MainApp {
    public static void main(String[] args) {
        MobileDirector mobileDirector = new MobileDirector();

        MobileBuilderImpl1 mobileBuilderImpl1 = new MobileBuilderImpl1();
        MobileBuilderImpl2 mobileBuilderImpl2 = new MobileBuilderImpl2();

    public static void printMessage(MobilePackage mobilePackage) {
        System.out.println("--Telephone charges: " + mobilePackage.getMoney() + "\t short message: " + mobilePackage.getShortInfo() + "\t RBT: " + mobilePackage.getMusic());

Personal experience

Builder mode is a common mode in the development process. This mode is easy to be decoupled, easy to accurately control the creation of objects, easy to expand, and has good encapsulation. It is relatively friendly to users. However, it should also be noted that products must have common ground and limited scope.

Screenshot of background editing

Added by Shai-Hulud on Sun, 06 Mar 2022 18:53:56 +0200