[design mode | C ා] design mode learning notes

Posted by jaimitoc30 on Wed, 15 Jan 2020 13:58:02 +0100

Article directory

1. Opening and closing principle

2. Single principle

When and only when the method in the class is very simple, we can only adopt the principle of single method, otherwise we should strictly adopt the principle of single class function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignMode.principle
{
    class SingleResponsibility
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            vehicle.run("Car");
            vehicle.dive("Ship");
            vehicle.fly("Airplane");
            RoadVehicle car = new RoadVehicle();
            car.run();
            WaterVehicle ship = new WaterVehicle();
            ship.run();
            AirVehicle plane = new AirVehicle();
            plane.run();
        }
    }

    #region Single method principle
    public class Vehicle
    {
        public void run(string vehicle)
        {
            Console.WriteLine(vehicle + "running on the road...");
        }
        public void fly(string vehicle)
        {
            Console.WriteLine(vehicle + "flying in the air...");
        }
        public void dive(string vehicle)
        {
            Console.WriteLine(vehicle + "diving in the water...");
        }
    }
    #endregion
    #region Strict single principle
    public class RoadVehicle
    {
        public void run()
        {
            Console.WriteLine("Vehicle is runing on the road...");
        }
    }
    public class WaterVehicle
    {
        public void run()
        {
            Console.WriteLine("Vehicle is surfing in the water...");
        }
    }
    public class AirVehicle
    {
        public void run()
        {
            Console.WriteLine("Vehicle is flying in the air...");
        }
    }
    #endregion
}

3. Interface isolation principle

All dependent interface methods should be implemented, and those that cannot be implemented should be separated from interfaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignMode.principle
{
    public interface Interface
    {
        void operation1();
        void operation2();
        void operation3();
        void operation4();
        void operation5();
    }
    #region problem
    public class A : Interface
    {
        public void operation1()
        {
            Console.WriteLine("Class B Implements operation1");
        }
        public void operation2()
        {
            Console.WriteLine("Class B Implements operation2");
        }
        public void operation3()
        {
            Console.WriteLine("Class B Implements operation3");
        }
        public void operation4()
        {
            Console.WriteLine("Class B Implements operation4");
        }
        public void operation5()
        {
            Console.WriteLine("Class B Implements operation5");
        }
    }
    public class B//Implementation methods 1, 2, 3 based on interface A
    {
        public void dependency1(Interface i)
        {
            i.operation1();
        }
        public void dependency2(Interface i)
        {
            i.operation2();
        }
        public void dependency3(Interface i)
        {
            i.operation3();
        }
    }
    public class C//Implementation method 1, 3, 4 depending on A
    {
        public void dependency1(Interface i)
        {
            i.operation1();
        }
        public void dependency3(Interface i)
        {
            i.operation3();
        }
        public void dependency4(Interface i)
        {
            i.operation4();
        }
    }
    #endregion
    #region Interface isolation
    public interface Interface1
    {
        void operation1();
        void operation2();
        void operation3();
    }
    public interface Interface2
    {
        void operation1();
        void operation3();
        void operation4();
    }
    public class D : Interface1
    {
        public void operation1()
        {
            //
        }
        public void operation2()
        {
            //
        }
        public void operation3()
        {
            //
        }

    }
    public class E : Interface2
    {
        public void operation1()
        {
            //
        }
        public void operation3()
        {
            //
        }
        public void operation4()
        {
            //
        }
    }
    public class F//Implementation of interface 1 method based on D
    {
        public void dependency1(Interface1 I)
        {
            I.operation1();
        }
        public void dependency2(Interface1 I)
        {
            I.operation2();
        }
        public void dependency3(Interface1 I)
        {
            I.operation3();
        }
    }
    public class G//Implementation of interface 2 method based on E
    {
        public void dependency1(Interface2 I)
        {
            I.operation1();
        }
        public void dependency3(Interface2 I)
        {
            I.operation3();
        }
        public void dependency4(Interface2 I)
        {
            I.operation4();
        }
    }
    #endregion
}


4.4. Dependency Inverse Principle

a) High level depends on low level, and both depend on abstraction
b) Abstraction does not depend on details
c) Interface oriented and Abstract Programming
d) The underlying modules should have interfaces or abstractions as much as possible, and the program should be stable
e) The declaration of variables should be abstract classes or interfaces to achieve polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignMode.principle
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.receive(new Email());
            Person1 person1 = new Person1();
            person1.receive(new Wechat());
            person1.receive(new Phone());
        }
    }
    class DependenceInversionPrinciple
    {
    }

    #region problem
    public class Email
    {
        public String getinfo()
        {
            return "This is an email";
        }
    }
    public class Person
    {
        public void receive(Email email)
        {
            Console.WriteLine(email.getinfo()); 
        }
    }
    #endregion
    #region Rely on inversion
    public interface Message
    {
        String getinfo();
    }
    public class Wechat: Message
    {
        public String getinfo()
        {
            return "This is WeChat";
        }
    }
    public class Phone : Message
    {
        public String getinfo()
        {
            return "This is Phone";
        }
    }
    public class Person1
    {
        public void receive(Message message)
        {
            Console.WriteLine(message.getinfo());
        }
    }
    #endregion
}


5. Richter's replacement principle

a) Where the base class can appear, the subclass must appear. When inheritance is emphasized, the subclass should not modify the parent method as much as possible;
b) Let the original parent class and child class inherit a more popular base class. Remove the original inheritance relationship and replace it with dependency, aggregation, combination and other relationships.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignMode.principle
{
    class Program
    {
        static void Main(string[] args)
        {
            Plus plus = new Plus();
            plus.plus(2, 5);
        }
    }
    class LiskowPrinciple
    {
    }
    public abstract class Operator
    {
        
    }
    public class BasicPlus: Operator
    {
        public String plus(int a, int b)
        {
            return (a + b).ToString();
        }
    }
    public class Plus: Operator
    {
        /// <summary>
        ///Combination, no direct inheritance
        /// </summary>
        private BasicPlus basicPlus = new BasicPlus();
        public void plus(int a, int b)
        {
            Console.WriteLine(this.basicPlus.plus(a, b));
        }
    }

}


6. Dimitar's Law (the least known principle)

a) One object has minimal knowledge of other objects
b) Direct friends, as long as there is a coupling relationship between two classes, the two classes are friends. When a class appears in the form of a member variable, a method parameter, or a return value in another class, these two classes are called direct friends. When a class appears as a local variable, these two classes call each other strange friends, and strange classes try not to appear.
c) As for direct friend correspondence;
d) Reduce coupling between classes.

7. Principle of composite reuse

a) Try to use synthetic aggregations instead of inheritance

Published 8 original articles, won praise 11, visited 3506
Private letter follow

Topics: Programming