<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    設計模式六大原則在 JS 中的運用


    發現錯別字 2年前 提問
    回答
    1
    瀏覽
    311
    請勿發布不友善或者負能量的內容。與人為善,比聰明更重要!
    回答數量: 1
    風險管理(專業級)RM/PL CICSA

    1.單一職責原則

    不同的類具備不同的職責,各司其職。做系統設計是,如果發現有一個類擁有了兩種職責,那么就要問一個問題:可以將這個類分成兩個類嗎?如果真的有必要,那就分開,千萬不要讓一個類干的事情太多。

    // 單一職責原則
    // 類
    public class People {
     public void work() {
      System.out.println("work");
     }
     public void eat() {
      System.out.println("eat");
     }
     public void play() {
      System.out.println("play");
     }
    }
    // 一個類, 三個職責
    // 單一職責原則
    public interface workInter {
     public void work();
    }
    public interface eatInter {
     public void eat();
    }
    public interface playInter {
     public void play();
    }
    // 繼承接口
    public class People implements workInter, eatInter, playInter {
     public void work() {
      System.out.println("work");
     }
     public void eat() {
      System.out.println("eat");
     }
     public void play() {
      System.out.println("play");
     }
    }
    public class Test {
     public static void main(String args[]) {
      People people = new People();
      workInter worker = new People();
      worker.work();
    
      eatInter eater = new People();
      eater.eat();
    
      playInter player = new People();
      player.play();
     }
    }

    2.開放封閉原則

    類、模塊、函數,可以去擴展,但不要去修改。如果要修改代碼,盡量用繼承或組合的方式來擴展類的功能。

    // 定義一個方法
    function da(x, y) {
     document.getElementById(x).style.color = y;
    }
    // 調用方法da
    da('dashucoding', 'red');
    // 開發封閉原則 -> 錯誤
    // 定義方法
    function da(x, y, z) {
     document.getElementById(x).style.color = y;
     document.getElementById(x).style.size = z;
    }
    
    // 調用方法da
    da('dashucoding', 'red', '100px');
    // 定義一個方法
    function da(x, y) {
     document.getElementById(x).style.color = y;
    }
    
    // 不去動da這個方法
    function dada(x, y, z) {
     da(x,y);
     document.getElementById(x).style.size = z;
    }
    
    // 正確使用開發封閉原則
    
    function da(x, y) {
     document.getElementById(x).style.color = y;
    }
    
    da('dashucoding', 'red');
    
    function dada(x, y, z) {
     da(x,y);
     document.getElementById(x).style.size = z;
    }
    
    dada('dashucoding', 'red', '100px');

    3.里氏替換原則

    對開發封閉原則進行補充,講的是基類和子類的關系。理解里氏替換原則的最經典的例子是“正方形是長方形”,“鴕鳥不是鳥”等,拿正方形來說,上數學課的時候,我們就知道,正方形是長方形,它是一個長寬相等的長方形,那么由此可以看出,應該讓正方形繼承自長方形。

    public class Rectangle {
     private int height;
     private int width;
    
     // 省略getter setter
    }
    
    // 正方形長和寬始終一樣 覆寫
    public class Square extends Rectangle {
     @Override
     public void setWidth(int width) {
      super.setWidth(width);
      super.setHeight(width);
     }
    
     @Override
     public void setHeight(int height) {
      super.setWidth(height);
      super.setHeight(height);
     }
    }
    public class Test {
     public static void main(String[] args) {
      Test test = new Test();
       Rectangle rectangle = new Rectangle();
       rectangle.setHeight(5);
       rectangle.setWidth(4);
       test.zoom(rectangle, 2, 3);
    
       Square square = new Square();
       square.setHeight(5);
       square.setWidth(4);
       test.zoom(square, 2, 3);
     }
    
     public void zoom(Rectangle rectangle, int width, int height) {
      rectangle.setWidth(rectangle.getWidth() + width);
      rectangle.setHeight(rectangle.getHeight() + height);
     }
    }

    4.依賴倒置原則

    定義:高層模塊不應該依賴低層模塊,二者都應該依賴其抽象;抽象不應該依賴細節,細節應該依賴抽象。

    class Book {
        public string getContent() {
            return "很久很久以前。。。。。";
        }
    }
    class Mother {
        public void narrate(Book book)
        {
            Console.WriteLine(book.getContent());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Mother monther = new Mother();
            monther.narrate(new Book());
            Console.ReadLine();
        }
    }

    如果讀的對象是報紙,雜志,卻發現客戶端不適用了。

    interface IReader{
        public string getContent();
    }

    這樣Mother類與接口IReader發生依賴關系,而Book和Newspaper都屬于讀物的范疇,他們各自都去實現IReader接口,這樣就符合依賴倒置原則了,修改代碼如下:

    interface IReader {
             string getContent();
        }
        class Newspaper: IReader
        {
        public string getContent()
        {
            return "切爾西豪取12連勝";
        }
    }
    class Book:IReader
    {
    
        public string getContent()
    {
        return "很久很久以前。。。。";
    }
    }
    class Mother
    {
        public void narrate(IReader reader)
        {
            Console.WriteLine(reader.getContent());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Mother monther = new Mother();
            monther.narrate(new Book());
            monther.narrate(new Newspaper());
            Console.ReadLine();
        }
    }

    采用依賴倒置原則給多人并行開發帶來極大的便利,比如上列中Mother類與Book類直接耦合,Mother必須等Book類編碼完成后才可以進行編碼,因為Mother類依賴于Book類。修改后的程序可以同時開工,互不影響。
    依賴關系的傳遞有三種方式,接口傳遞,構造方法傳遞和setter方法傳遞。

    interface IDriver{
        public void drive(ICar car);
    }
    public class Driver:IDriver{
        public void drive(ICar car){
            car.run();
        }
    }

    構造方法傳遞:

    interface IDriver{
        public void drive();
    }
    public class Driver implements IDriver{
        public ICar car;
        public Driver(ICar _car){
            this.car=_car;
        }
        public void drive(){
            this.car.run();
        }
    }

    setter方式傳遞:

    interface IDriver{
        public void setCar(ICar car);
        public void drive();
    }
    public class Driver:IDriver{
        PRIVATE ICar car;
        public void setCar(ICar car){
            this.car=car;
        }
        public void drive(){
            this.car.run();
        }
    }

    5.接口分離原則

    如果一個類實現一個接口,但這個接口中有它不需要的方法,那么就需要把這個接口拆分,把它需要的方法提取出來,組成一個新的接口讓這個類去實現。

    interface I{
        void method1();
        void method2();
        void method3();
        void method4();
        void method5();
    }
    class A{
        public void depend1(I i){
            i.method1();
        }
        public void depend2(I i){
            i.method2();
        }
        public void depend3(I i){
            i.method3();
        }
    }
    class C{
        public void depend1(I i){
            i.method1();
        }
        public void depend2(I i){
            i.method4();
        }
        public void depend3(I i){
            i.method5();
        }
    }
    class B:I{
        public void method1(){
            Console.WriteLine("類B實現接口I的方法1");
        }
        public void method2(){
            Console.WriteLine("類B實現接口I的方法2");
        }
        public void method3(){
            Console.WriteLine("類B實現接口I的方法3");
        }
        public void method4(){}
        public void method5(){}
    }
    class D:I{
        public void method1(){
            Console.WriteLine("類B實現接口I的方法1");
        }
        public void method2(){}
        public void method3(){}
        public void method4(){
            Console.WriteLine("類B實現接口I的方法4");
        }
        public void method5(){
            Console.WriteLine("類B實現接口I的方法5");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A a=new A();
            a.depend1(new B());
            a.depend2(new B());
            a.depend3(new B());
    
            C c=new C();
            c.depend1(new D());
            c.depend2(new D());
            c.depend3(new D());
            Console.ReadLine();
        }
    }

    可以看到,接口中出現的方法,不管對依賴于它的類有沒有作用,實現類中都必須去實現這些方法。于是我們將原接口I拆分為三個接口:

    interface I1{
        void method1();
    }
    interface I2{
        void method2();
        void method3();
    }
    interface I3{
        void method4();
        void method5();
    }
    class A{
        public void depend1(I1 i){
            i.method1();
        }
        public void depend2(I2 i){
            i.method2();
        }
        public void depend3(I2 i){
            i.method3();
        }
    }
    class C{
        public void depend1(I1 i){
            i.method1();
        }
        public void depend2(I3 i){
            i.method4();
        }
        public void depend3(I3 i){
            i.method5();
        }
    }
    class B:I1,I2{
        public void method1(){
            Console.WriteLine("類B實現接口I1的方法1");
        }
        public void method2(){
            Console.WriteLine("類B實現接口I2的方法2");
        }
        public void method3(){
            Console.WriteLine("類B實現接口I2的方法3");
        }
    }
    class D:I1,I3{
        public void method1(){
            Console.WriteLine("類B實現接口I的方法1");
        }
        public void method4(){
            Console.WriteLine("類B實現接口I的方法4");
        }
        public void method5(){
            Console.WriteLine("類B實現接口I的方法5");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A a=new A();
            a.depend1(new B());
            a.depend2(new B());
            a.depend3(new B());
    
            C c=new C();
            c.depend1(new D());
            c.depend2(new D());
            c.depend3(new D());
            Console.ReadLine();
        }
    }

    6.最少知識原則

    一個對象應該對其他對象保持最少的了解。類與類關系越密切,耦合度越大。

    迪米特法則又叫最少知道原則,即一個類對自己依賴的類知道的越少越好。也就是說,對于被依賴的類不管多么復雜,都盡量將邏輯封裝在類的內部。對外除了提供的public 方法,不對外泄露任何信息。

    舉例額說明如下,有一個集團公司,下屬單位有分公司和直屬部門,現要求打印出所有下屬單位的員工ID。<

    回答所涉及的環境:聯想天逸510S、Windows 10。

    2年前 / 評論
    亚洲 欧美 自拍 唯美 另类