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('dashucoding', 'red');
function da(x, y, z) {
document.getElementById(x).style.color = y;
document.getElementById(x).style.size = z;
}
da('dashucoding', 'red', '100px');
function da(x, y) {
document.getElementById(x).style.color = y;
}
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;
}
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。<