七大软件设计原则
- 1. 开闭原则
- 2. 依赖倒置原则
- 3. 单一职责原则
- 4. 接口隔离原则
- 5. 迪米特法则
- 6. 里氏替换原则
- 7. 合成复用原则
任何行业或者圈子都有自己的规则,遵从规则是在这个行业立足的根本,不然你就会被这个行业淘汰。软件设计同样有自己的原则。遵循这样的原则,你的代码软件才能更容易被接受,才会使你更专业。
1. 开闭原则
定义
优点
代码
/*** The interface Boo k.*/
public interface Book {/*** Gets id.** @return the id*/Integer getId();/*** Gets name.** @return the name*/String getName();/*** Gets price.** @return the price*/Double getPrice();
}
/*** The type Java book.*/
public class JavaBook implements Book {private Integer id;private String name;private Double price;/*** Instantiates a new Java book.** @param id the id* @param name the name* @param price the price*/public JavaBook(Integer id, String name, Double price) {this.id = id;this.name = name;this.price = price;}/*** Sets id.** @param id the id*/public void setId(Integer id) {this.id = id;}/*** Sets name.** @param name the name*/public void setName(String name) {this.name = name;}/*** Sets price.** @param price the price*/public void setPrice(Double price) {this.price = price;}@Overridepublic Integer getId() {return this.id;}@Overridepublic String getName() {return this.name;}@Overridepublic Double getPrice() {return this.price;}
/*** The type Discount java book.*/
public class DiscountJavaBook extends JavaBook {/*** Instantiates a new Java book.** @param id the id* @param name the name* @param price the price*/public DiscountJavaBook(Integer id, String name, Double price) {super(id, name, price);}@Overridepublic Double getPrice() {return super.getPrice() * 0.5;}
}
public class Test {public static void main(String[] args) {Book book = new JavaBook(1, "《Java开发指南》", 79.9);System.out.println("书本Id: " + book.getId() +"\n书本名字: " + book.getName() +"\n书本售价: " + book.getPrice() + "元");Book book1 = new DiscountJavaBook(1, "《Java开发指南》", 79.9);System.out.println("书本Id: " + book1.getId() +"\n书本名字: " + book1.getName() +"\n书本打折后售价: " + book1.getPrice() + "元");}
}
书本Id: 1
书本名字: 《Java开发指南》
书本售价: 79.9元
书本Id: 1
书本名字: 《Java开发指南》
书本打折后售价: 39.95元
类图
2. 依赖倒置原则
定义
优点
代码
/*** The interface Boo k.*/
public interface Book {/*** Buy.*/void buy();
}
/*** The type Java book.*/
public class JavaBook implements Book {@Overridepublic void buy() {System.out.println("我要买一本Java的书籍");}
}
/*** The type Python book.*/
public class PythonBook implements Book {@Overridepublic void buy() {System.out.println("我要买一本Python的书籍");}
}
/*** The type James.*/
public class James {private Book book;/*** Instantiates a new James.** @param book the book*/public James(Book book) {this.book = book;}/*** Instantiates a new James.*/public James() {}/*** Buy java book.*/public void buyJavaBook() {System.out.println("我要买一本Java的书籍");}/*** Buy python book.*/public void buyPythonBook() {System.out.println("我要买一本Python的书籍");}/*** Buy.** @param book the book*/public void buy(Book book) {book.buy();}/*** Buy.*/public void buy() {this.book.buy();}/*** Sets book.** @param book the book*/public void setBook(Book book) {this.book = book;}
}
/*** The type Test.*/
public class Test {/*** The entry point of application.** @param args the input arguments*/public static void main(String[] args) {//--------------v1-------------------James james1 = new James();james1.buyJavaBook();james1.buyPythonBook();//--------------v2-------------------James james2 = new James();james2.buy(new JavaBook());james2.buy(new PythonBook());//--------------v3-------------------James james3 = new James(new JavaBook());james3.buy();//--------------v4-------------------James james4 = new James();james4.setBook(new PythonBook());james4.buy();}
}
我要买一本Java的书籍
我要买一本Python的书籍
我要买一本Java的书籍
我要买一本Python的书籍
我要买一本Java的书籍
我要买一本Python的书籍
类图
3. 单一职责原则
定义
优点
代码
/*** The interface Book.*/
public interface IBook {/*** Read.*/void read();/*** Write.*/void write();
}
/*** The interface Book read.*/
public interface IBookRead {/*** Read.*/void read();
}
/*** The interface Book write.*/
public interface IBookWrite {/*** Write.*/void write();
}
/*** The type Book.*/
public class BookImpl implements IBookRead, IBookWrite {@Overridepublic void read() {}@Overridepublic void write() {}
}
类图
4. 接口隔离原则
定义
优点
代码
/*** The interface Fly aminal.*/
public interface IFlyAminal {/*** Fly.*/void fly();
}
/*** The interface Eat aminal.*/
public interface IEatAminal {/*** Eat.*/void eat();
}
/*** The interface Swim aminal.*/
public interface ISwimAminal {/*** Swim.*/void swim();
}
/*** The type Bird.*/
public class Bird implements IFlyAminal, IEatAminal {@Overridepublic void eat() {}@Overridepublic void fly() {}
}
/*** The type Dog.*/
public class Dog implements IEatAminal, ISwimAminal {@Overridepublic void eat() {}@Overridepublic void swim() {}
}
类图
5. 迪米特法则
定义
优点
代码
/*** The type Crouse.*/
public class Crouse {
}
/*** The type Team leader.*/
public class TeamLeader {/*** Check crouse num.** @param employee the employee*/public void checkCrouseNum(Employee employee){employee.crouseNum();}
}
/*** The type Employee.*/
public class Employee {/*** Crouse num.*/public void crouseNum(){List<Crouse> list = new ArrayList<>();for (int i = 0; i < 20; i++) {list.add(new Crouse());}System.out.println("课程总数为:" + list.size());}
}
/*** The type Test.*/
public class Test {/*** The entry point of application.** @param args the input arguments*/public static void main(String[] args) {TeamLeader leader = new TeamLeader();Employee employee = new Employee();leader.checkCrouseNum(employee);}
}
类图
6. 里氏替换原则
定义
优点
代码
/*** The type Base.*/
public class Base {/*** Method.** @param map the map*/public void method(HashMap map) {System.out.println("执行父类");}
}
public class Child extends Base {/*@Overridepublic void method(HashMap map) {System.out.println("执行子类HashMap");}*/public void method(Map map) {System.out.println("执行子类");}
}
public class Test {public static void main(String[] args) {Child child = new Child();HashMap hashMap = new HashMap();child.method(hashMap);Base child1 = new Child();HashMap hashMap1 = new HashMap();child1.method(hashMap1);}
}
执行父类
执行父类
类图
7. 合成复用原则
定义
优点
代码
public abstract class DBConnection {abstract String getConneciotn();
}
public class MysqlConneciotn extends DBConnection {@OverrideString getConneciotn() {return "获取mysql连接";}
}
public class OracleConnection extends DBConnection {@OverrideString getConneciotn() {return "获得oracle连接";}
}
public abstract class DBConnection {abstract String getConneciotn();
}
public class Issue {private DBConnection dbConnection;public void setDbConnection(DBConnection dbConnection) {this.dbConnection = dbConnection;}public void addIssue() {String conn = dbConnection.getConneciotn();System.out.println(conn);}
}
public class Test {public static void main(String[] args) {Issue issue = new Issue();issue.setDbConnection(new MysqlConneciotn());issue.addIssue();}
}
类图
感谢您阅读本文,如果您觉得文章写的对您有用的话,请您点击上面的“关注”,点个赞,这样您就可以持续收到《JAVA架构师之路》的最新文章了。文章内容属于自己的一点点心得,难免有不对的地方,欢迎在下方评论区探讨,你们的关注是我创作优质文章的动力。
JAVA架构师之路二:设计模式之工厂模式
本文链接:https://my.lmcjl.com/post/16792.html
展开阅读全文
4 评论