七种设计原则

设计模式七大原则

  1. 单一职责原则

    基本介绍

    对类来说,即一个类应该只负责一项职责,如类A负责两个不同职责:职责1,职责2,当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解为A1,A2

    package com.sun.single;
    
    public class single1 {
        public static void main(String[] args) {
    
            System.out.println(vehicle+"使用单一原则之前");
            Vehicle vehicle = new Vehicle();
            vehicle.run("汽车");
            vehicle.run("飞机");
            vehicle.run("轮船");
        }
    }
    class Vehicle{
        public void run(String vehicle){
            System.out.println(vehicle+"在公路上跑");
        }
    }
    
    
    package com.sun.single;
    
    public class single2 {
        public static void main(String[] args) {
    
            Vehicle1 vehicle1 = new Vehicle1();
            vehicle1.run("汽车");
            Vehicle2 vehicle2 = new Vehicle2();
            vehicle2.runAri("飞机");
            Vehicle3 vehicle3 = new Vehicle3();
            vehicle3.runWater("轮船");
        }
    }
    class Vehicle1{
        public void run(String vehicle){
            System.out.println(vehicle+"在公路上跑");
        }
    }
    class Vehicle2{
        public void runAri(String vehicle){
            System.out.println(vehicle+"在天上跑");
        }
    }
    class Vehicle3{
        public void runWater(String vehicle){
            System.out.println(vehicle+"在水上跑");
        }
    }
    
    
    package com.sun.single;
    
    public class single3 {
        public static void main(String[] args) {
    
            System.out.println(vehicle+"使用单一原则之后");
            Vehicle4 vehicle4 = new Vehicle4();
            vehicle4.run("汽车");
            vehicle4.runAri("飞机");
            vehicle4.runWater("轮船");
            
        }
    }
    class Vehicle4{
        public void run(String vehicle){
            System.out.println(vehicle+"在公路上跑");
        }
        public void runAri(String vehicle){
            System.out.println(vehicle+"在天空上跑");
        }
        public void runWater(String vehicle){
            System.out.println(vehicle+"在水上跑");
        }
    }
    
    
  2. 接口隔离原则

    基本介绍

    客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上

    package com.sun.segregation;
    
    public class Segregation {
    
    }
    interface interface1{
        void operation1();
        void operation2();
        void operation3();
        void operation4();
        void operation5();
    }
    class B implements interface1{
    
        @Override
        public void operation1() {
    
            System.out.println("B实现了operation1");
        }
    
        @Override
        public void operation2() {
    
            System.out.println("B实现了operation2");
        }
    
        @Override
        public void operation3() {
    
            System.out.println("B实现了operation3");
        }
    
        @Override
        public void operation4() {
    
            System.out.println("B实现了operation4");
        }
    
        @Override
        public void operation5() {
    
            System.out.println("B实现了operation5");
        }
    }
    class D implements interface1{
    
        @Override
        public void operation1() {
    
            System.out.println("D实现了operation1");
        }
    
        @Override
        public void operation2() {
    
            System.out.println("D实现了operation2");
        }
    
        @Override
        public void operation3() {
    
            System.out.println("D实现了operation3");
        }
    
        @Override
        public void operation4() {
    
            System.out.println("D实现了operation4");
        }
    
        @Override
        public void operation5() {
    
            System.out.println("D实现了operation5");
        }
    }
    class A {
        public void depend1(interface1 i){
            i.operation1();
        }
        public void depend2(interface1 i){
            i.operation2();
        }
        public void depend3(interface1 i){
            i.operation3();
        }
    }
    class C {
        public void depend1(interface1 i){
            i.operation1();
        }
        public void depend4(interface1 i){
            i.operation4();
        }
        public void depend5(interface1 i){
            i.operation5();
        }
    }
    
    package com.sun.segregation.improve;
    
    public class Segregation1 {
        public 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.depend4(new D());
            c.depend5(new D());
        }
    }
    interface interface1{
        void operation1();
    }
    interface interface2{
        void operation2();
        void operation3();
    }
    interface interface3{
        void operation4();
        void operation5();
    }
    class B implements interface1,interface2{
    
        @Override
        public void operation1() {
    
            System.out.println("B实现了operation1");
        }
    
        @Override
        public void operation2() {
    
            System.out.println("B实现了operation2");
        }
    
        @Override
        public void operation3() {
    
            System.out.println("B实现了operation3");
        }
    
    }
    class D implements interface1,interface3{
    
        @Override
        public void operation1() {
    
            System.out.println("D实现了operation1");
        }
        @Override
        public void operation4() {
    
            System.out.println("D实现了operation4");
        }
    
        @Override
        public void operation5() {
    
            System.out.println("D实现了operation5");
        }
    }
    class A {
        public void depend1(interface1 i){
            i.operation1();
        }
        public void depend2(interface2 i){
            i.operation2();
        }
        public void depend3(interface2 i){
            i.operation3();
        }
    }
    class C {
        public void depend1(interface1 i){
            i.operation1();
        }
        public void depend4(interface3 i){
            i.operation4();
        }
        public void depend5(interface3 i){
            i.operation5();
        }
    }
    
  3. 依赖倒转原则

    package com.sun.inversion;
    
    public class DependecyInversion {
        public static void main(String[] args) {
            Persion persion = new Persion();
            persion.receive(new Email());
        }
    }
    
    class Email{
        public String getInfo(){
            return "email:hello world";
        }
    }
    class Persion{
        public void receive(Email email){
            System.out.println(email.getInfo());
        }
    }
    
    package com.sun.inversion.improve;
    
    public class DependecyInversion {
        public static void main(String[] args) {
            Persion persion = new Persion();
            persion.receive(new Email());
            persion.receive(new WeiXin());
        }
    }
    interface IReceive{
        String getInfo();
    }
    class Email implements IReceive{
        public String getInfo(){
            return "email:hello world";
        }
    }
    class WeiXin implements IReceive{
    
        @Override
        public String getInfo() {
            return "WeiXin:hello world";
        }
    }
    
    class Persion{
        public void receive(IReceive receive){
            System.out.println(receive.getInfo());
        }
    }
    
  4. 里氏替换原则

    package com.sun.liskov;
    
    public class Liskov {
        public static void main(String[] args) {
    
            A a = new A();
            System.out.println("11-3="+a.func1(11,3));
            System.out.println("1-8="+a.func1(1,8));
            System.out.println("**************************");
            B b = new B();
            System.out.println("11-3="+b.func1(11,3));
            System.out.println("1-8="+b.func1(1,8));
            System.out.println("11+3+9="+b.func2(11,3));
        }
    }
    class A {
        public int func1(int num1,int num2){
            return num1 - num2;
        }
    }
    class B extends A {
        @Override
        public int func1(int a, int b) {
            return a+b;
        }
        public int func2(int a, int b) {
            return func1(a,b)+9;
        }
    }
    
    
    package com.sun.liskov.improve;
    
    public class Liskov {
        public static void main(String[] args) {
    
            A a = new A();
            System.out.println("11-3="+a.func1(11,3));
            System.out.println("1-8="+a.func1(1,8));
            System.out.println("**************************");
            B b = new B();
            System.out.println("11+3="+b.func1(11,3));
            System.out.println("1+8="+b.func1(1,8));
            System.out.println("11+3+9="+b.func2(11,3));
            System.out.println("11-3="+b.func3(11,3));
        }
    }
    
    class base{
    
    }
    class A extends base {
        public int func1(int num1,int num2){
            return num1 - num2;
        }
    }
    
    class B extends base {
        private A a = new A();
        public int func1(int a, int b) {
            return a+b;
        }
        public int func2(int a, int b) {
            return func1(a,b)+9;
        }
        public int func3(int a, int b){
            return this.a.func1(a,b);
        }
    }
    
    
  5. 开闭原则

    package com.sun.ocp;
    
    public class Ocp {
        public static void main(String[] args) {
            GraphicEditor graphicEditor = new GraphicEditor();
            graphicEditor.drawShape(new Rectangle());
            graphicEditor.drawShape(new Circle());
        }
    }
    class GraphicEditor {
        public void drawShape(Shape s) {
            if (s.m_type == 1)
                drawRectangle(s);
            else if (s.m_type == 2)
                drawCircle(s);
        }
        public void drawRectangle(Shape r) {
            System.out.println("矩形");
        }
        public void drawCircle(Shape r) {
            System.out.println("圆形");
        }
    }
    class Shape {
        int m_type;
    }
    class Rectangle extends Shape {
        Rectangle() {
            super.m_type = 1;
        }
    }
    class Circle extends Shape {
        Circle() {
            super.m_type = 2;
        }
    }
    
    package com.sun.ocp.improve;
    
    public class Ocp {
        public static void main(String[] args) {
            GraphicEditor graphicEditor = new GraphicEditor();
            graphicEditor.drawShape(new Rectangle());
            graphicEditor.drawShape(new Circle());
            graphicEditor.drawShape(new OtherGraphic());
        }
    }
    
    class GraphicEditor {
        public void drawShape(Shape s) {
            s.draw();
        }
    }
    
    abstract class Shape {
        int m_type;
        public abstract void draw();
    }
    
    class Rectangle extends Shape {
        Rectangle() {
            super.m_type = 1;
        }
    
        @Override
        public void draw() {
            System.out.println("绘制矩形");
        }
    }
    
    class Circle extends Shape {
        Circle() {
            super.m_type = 2;
        }
    
        @Override
        public void draw() {
            System.out.println("绘制圆形");
        }
    }
    class OtherGraphic extends Shape {
    
        OtherGraphic() {
            super.m_type = 4;
        }
        @Override
        public void draw() {
            System.out.println("绘制其他图形");
        }
    }
    
  6. 迪米特法则

    package com.sun.demeter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Demeter {
        public static void main(String[] args) {
            SchoolManager schoolManager = new SchoolManager();
            schoolManager.printAllEmployee(new CollegeManager());
        }
    }
    
    class Employee {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getId() {
            return id;
        }
    }
    
    class CollegeEmployee {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getId() {
            return id;
        }
    }
    
    class CollegeManager {
        public List<CollegeEmployee> getAllEmployee() {
            List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
            for (int i = 0; i < 10; i++) {
                CollegeEmployee emp = new CollegeEmployee();
                emp.setId("学院员工id:" + i);
                list.add(emp);
            }
            return list;
        }
    }
    
    class SchoolManager {
        public List<Employee> getAllEmpoyee() {
            List<Employee> list = new ArrayList<Employee>();
            for (int i = 0; i < 5; i++) {
                Employee emp = new Employee();
                emp.setId("学校总部员工id:" + i);
                list.add(emp);
            }
            return list;
        }
    
        void printAllEmployee(CollegeManager sub) {
            List<CollegeEmployee> list1 = sub.getAllEmployee();
            System.out.println("*******分公司员工**********");
            for (CollegeEmployee e : list1) {
                System.out.println(e.getId());
            }
            List<Employee> list2 = this.getAllEmpoyee();
            System.out.println("*******学校总部员工*********");
            for (Employee e : list2) {
                System.out.println(e.getId());
            }
        }
    
    }
    
    package com.sun.demeter.improve;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Demeter {
        public static void main(String[] args) {
            SchoolManager schoolManager = new SchoolManager();
            schoolManager.printAllEmployee(new CollegeManager());
        }
    }
    
    class Employee {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getId() {
            return id;
        }
    }
    
    class CollegeEmployee {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getId() {
            return id;
        }
    }
    
    class CollegeManager {
        public List<CollegeEmployee> getAllEmployee() {
            List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
            for (int i = 0; i < 10; i++) {
                CollegeEmployee emp = new CollegeEmployee();
                emp.setId("学院员工id:" + i);
                list.add(emp);
            }
            return list;
        }
        public void printEmployee() {
            List<CollegeEmployee> list1 = getAllEmployee();
            System.out.println("*******分公司员工**********");
            for (CollegeEmployee e : list1) {
                System.out.println(e.getId());
            }
        }
    }
    
    class SchoolManager {
        public List<Employee> getAllEmpoyee() {
            List<Employee> list = new ArrayList<Employee>();
            for (int i = 0; i < 5; i++) {
                Employee emp = new Employee();
                emp.setId("学校总部员工id:" + i);
                list.add(emp);
            }
            return list;
        }
    
        void printAllEmployee(CollegeManager sub) {
            sub.printEmployee();
            List<Employee> list2 = this.getAllEmpoyee();
            System.out.println("*******学校总部员工*********");
            for (Employee e : list2) {
                System.out.println(e.getId());
            }
        }
    
    }
    
  7. 合成复用原则

    尽量使用合成/聚合的方式,而不是使用继承

posted @ 2020-09-02 15:32  striver-sc  阅读(84)  评论(0编辑  收藏  举报