Theoretical knowledge of design patterns (seven principles)

Design Patterns


scenes to be used


   Software architecture and programming


purpose


   1. Improve code reusability
   2. Improve code readability
   3. Ensure code reliability


Design principles and characteristics


   1. Extensibility 
   2. Flexibility 
   3. Componentized pluggability (Pluggability)

Guidelines

 

  • Single responsibility principle : The storage function of a Java class cannot realize the functions of different attributes. Dining and sleeping are two things, and objects need to be created separately.
class HandleUser{

    // 增删改查 操作用户 登录注册功能
    public User getUser(){};
    public void updateUser(int id){};
    public void saveUser(int id){};
    public void removeUser(int id){};

    // 用户玩游戏
    public void userPlayGame();

}

/**
 *
 * 这两个功能是不是不应该杂糅在一起,所以需要创建两个Java类,来存放不同功能
 *
 */

class UserHandleDao{

    // 增删改查 操作用户 登录注册功能
    public User getUser(){};
    public void updateUser(int id){};
    public void saveUser(int id){};
    public void removeUser(int id){};
}

class UserBusinessDao{

    // 用户玩游戏
    public void userPlayGame(){};

}
  • Principle of opening and closing : software is open to external expansion, external modification is closed, you have to choose when you eat, there is a choice, there is a demand, and there is a need for your preference, for example, if you like to eat tomatoes, then you still need to taste the world’s food, which you have not eaten Food naturally needs your experimentation to determine whether it is delicious or not, but no matter how good the food is, it will not affect your original favorite food. This is the same.
class UserBusinessImpl{

    @注入
    private ConncetMouxinUtil;

    // 普通用户玩游戏 国王游戏
    public void ordinaryUserPlayGame(){};
    // VIP用户玩游戏
    public void VipUserPlayGame(){};

}

/**
 *
 * 用户玩本公司的游戏,以及登录注册游戏账号,但是呢大家都是有身份的人,怎么会没有某信账号
 * 什么?没有某信注册账号,对不起我不玩辣鸡游戏
 * 这时我们需要外接接口,挽救玩家
 *
 */
class ConncetMouxinUtil{

    // 软件的扩展,但是也拒绝其某信修改或者获取软件数据
    public void connectMouxin(){};    

}
  • Richter substitution principle : Having the same attributes and functions is summarized into a parent class, and the child class inherits the attributes and functions of the parent class. For example, mother and daughter, father and son, four-person family, daughter and son inherit from father and son Mom’s excellent genes, from the moment of birth, all your body looks are provided by your parents. You also have the same life span as your parents, thought, consciousness, and wisdom, all inherited from your parents. Of course, human beings have many different attributes, so they cannot be generalized. In a parent class, your character and future development and so on.
class TravelerKong{

    // 技能:裂空之剑
    public void skillLiekongZhijian(String AKey){
        System.out.println("造成9999伤害!");
    }

}

class TravelerYing{

    // 技能:异邦铁风
    public void skillYibangTiefeng(String BKey){
        System.out.println("造成100000伤害!");
    }    

}

/**
 *
 * 你看空和荧的技能参数一样,功能也一样,只是输出不一样
 * 可要是每个人物都写这样无数的技能,维护和板砖都是极不友好
 * 平A你都要给写一个对象吗
 *
 */

class TravelerTian{

    // 技能:万物归一
    public void skillWanwuGuiyi(String AKey,String hurt){
           System.out.println(hurt);     
    }

}

class TravelerKong extends TravelerTian{

    @Override
    public void skillWanwuGuiyi(String AKey,String hurt){
         // 非空判断省略了
         if( AKey == KEY_FLG )
           System.out.println(hurt);     
    }

}

class TravelerYing extends TravelerTian{
    
    private static final String KEY_FLG = "1";


    @Override
    public void skillWanwuGuiyi(String AKey,String hurt){
         if( AKey == KEY_FLG )
           System.out.println(hurt);     
    }

}

class PersonageTest{

    private static final String KONG_HURT = "造成9999伤害!";

    private static final String YING_HURT = "造成100000伤害!";

    private static final String KEY_FLG = "1";
   
    @Test
    public void GameTest(){
        TravelerTian tk = new TravelerKong();
        // 某某调用了这个方法 判断省略了
        tk.skillWanwuGuiyi(parma , KONG_HURT);

        TravelerTian ty = new TravelerYing();
        // 某某调用了这个方法 判断省略了
        ty.skillWanwuGuiyi(parma , YING_HURT );
        
    }
    
}
  • Relying on the principle of reversal : Interface-oriented programming, the interface is just another name for affirming specific actions, describing what kind of action it is, so there is no specific process of action occurring, and if you want to truly depict the process of action, you need a Java class at this time Implements the interface of a certain action alias. For example, sleeping, sitting and sleeping, sitting and sleeping is just a term. After we need to observe carefully, Ge You fell asleep on a bench , The slightly opened mouth made a rumbling snoring sound, and Harazi flowed into a small river unconvincingly. This is the specific description of the behavior of sitting and sleeping, which completely visualizes the whole sleeping movement, and the same situation when lying down.
class interface MyGame {
    
    void LOL();
    void YuanShen();
    void CF();
    void WangZheRongYang();
    .......

}

/**
 *
 * 接口只是一个万物的名字代称,不具备业务实现
 * 而我们将这类相同属性的事务,归纳到一个接口中,这就是我所理解的面向接口编程
 *
 */
class UserGame implements MyGame {
    
    // 接口需要全部实现方法
    public void  LOL(){};
    public void  YuanShen(){};
    public void  CF(){};
    public void  WangZheRongYang(){}; 

}
  • Interface isolation principle : Similar actions, data processing behaviors, unified use of different interfaces to store separately, similar to the two behaviors of sitting and sleeping, lying down and sleeping are one action, and one interface is stored; while eating hungrily, chewing slowly Tasting food is another action and requires another interface for storage.
class interface MyGame {
    
    void LOL();
    void YuanShen();
    void CF();
    void WangZheRongYang();

    void aOiSoLa();

}

/**
 *
 * aOiSoLa()这是什么鬼?你们细细品会,这两个东西当然不能放在一起,会出大事的!
 *
 */
  • Synthetic reuse principle : Try to use combination and aggregation associations in the system, and use less inheritance relationships, which will cause code coupling. Simply put, will you and your buddies achieve a fact on the matter of acknowledging fathers? At this time, we need to verbalize the facts, mouth and mouthful of the son’s call, but the son will not really recognize you as a father, he really regards you as a father, and then there will be more sons, but also to benefit the son. It's absolutely impossible. A son is a son. You shouldn't want the benefits of your father. So when we call a son, we use our usual style of running errands to the cafeteria to bring food. This is what the son should do. The son is equivalent to a Java class, which directly declares the son object and calls it in subsequent methods.
class TravelerTian{

    // 技能:万物归一
    public void skillWanwuGuiyi(String AKey,String hurt){
          System.out.println(hurt);    
    };

}

class TravelerKong extends TravelerTian{

      public void doRequest(){
        super.skillWanwuGuiyi("1","123");
    }

}

/**
 *
 * 某天你收到用户差评,要求你把钟离伤害拉高,糟糕的是,你写了继承关系
 * 该死,你必须要在父类中添加方法,可是一旦添加方法
 * 子类必须继承一个没有任何用处的方法,造成代码的耦合性
 * 为此,你必须将继承关系改写为组合关系
 * 正因为父类是抽象类
 *
 */
class TravelerKong {

    @注入
    private TravelerTian travelerTian;

    public void doRequest(){
        travelerTian.skillWanwuGuiyi("1","123");
    }

}
  • Dimit's rule : One class reduces other types of business, and the third type is introduced to call this type of business. If a person's daily routine is programmed, eat, sleep, work, study, and entertainment, these happen every day, but they are a bit different , You can’t summarize every day in one Java class, there will be countless Java classes, then you need to put eating and sleeping into separate Java classes, completely independent events, eating is eating, sleeping is sleeping, You can't confuse these two things, so they naturally become two objects. When you need to summarize the things of the day, the main class will call this object to redefine the object's properties for sleeping posture and eating.
    class MyWordGame {
    
        // 现实 虚拟 大脑
    
        // 游戏要远离现实世界
        public void fleeReality(){}
        
        // 游戏要进入虚拟世界
        public void entrySuppositional(){}
    
        // 大脑要分辨现实和虚拟
        public void brainDiscriminateRAE(){}
    }
    
    /**
     *
     * 仔细想想,这三个的关系,一定大脑在前,现实在后,虚拟在末尾
     * 三者不能同时进行,必定有多层处理,需要重新将包结构建立
     * 以前都是在同一个包内
     *
     */
    com.hikktn.reality
    com.hikktn.suppositional
    com.hikktn.brain
    
    继续进行业务细化
    
    com.hikktn.reality.service
    com.hikktn.reality.dao
    com.hikktn.reality.controller
    
    与此类推
    ……
    

     

Guess you like

Origin blog.csdn.net/qq_41520636/article/details/111189561