Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6922996
  • 博文数量: 700
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12011
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(700)

分类: Java

2013-12-27 16:35:26

 SOLID (单一功能、开闭原则、里氏替换、接口隔离以及依赖反转)
 

S(Single Responsible Principle)一个对象只实现一个功能。

O:类可以扩展但不能修改。

L:子类型应该能够替换它们的基类型。

即子类中重写方法时使用的类型必须是父类的类型或子类型。

举例:

抽象方法 Public abstract deposit(int amt) throws AmountException

子类实现时:Public void deposit(int amt) throws InvalidAmountException  -> OK

Public void deposit(int amt) throws AmountException -> OK

Public void deposit(int amt) throws Exception -> NG

I:众多的接口要优于单一的、通用型的接口。

    接口实现单一功能。要实现多功能时可以实现多个接口。不要试图做一个大而全的接口。

D于抽象而不是一个具体实现类。的一种实现方式

    可以抽象耦合,不要实例耦合。  --> 低耦合原则

    类不应该耦合于另一个具体的类或者可以实例化的类。应该耦合于其他的基类或抽象类。

    Class c = Class.forName(“具体类名”)

    父接口 = (父接口)c.newInstance();

package atest.solid.OpenClosedPrinciple;

public class OCPTest {

    /**

     * [メソッドの説明を書きましょう]

     * @param args

     */

    public static void main(String[] args) {

        // TODO 自動生成されたメソッド?スタブ

        int amt = 300;

        AccountType saves = new SavingsAccount();

        saves.deposit(amt);

        AccountType check = new CheckingAccount();

        check.deposit(amt);

    }

}

package atest.solid.OpenClosedPrinciple;

public class Account {

    private AccountType _act;

    public Account(String act){

        try{
                   

            Class c = Class.forName(act);

            this._act = (AccountType)c.newInstance();

        } catch (Exception e){

            e.printStackTrace();

        }

    }

    public void deposit(int amt){

        this._act.deposit(amt);

    }

}

package atest.solid.OpenClosedPrinciple;

public interface AccountType {

    public void deposit(int amt);

}

package atest.solid.OpenClosedPrinciple;

public class SavingsAccount implements AccountType {

    @Override

    public void deposit(int amt) {

        // TODO 自動生成されたメソッド?スタブ

        System.out.println("Amount deposited in " + "savings acount(\\):" + amt);

    }

}

package atest.solid.OpenClosedPrinciple;

public class CheckingAccount implements AccountType {

    @Override

    public void deposit(int amt) {

        // TODO 自動生成されたメソッド?スタブ

        System.out.println("Amount deposited in " + "checkings acount(\\):" + amt);

    }

}

执行结果:

Amount deposited in savings acount(\):300
Amount deposited in checkings acount(\):300


测试类稍微改动了一下:

package atest.solid.OpenClosedPrinciple;

public class OCPTest {

    /**
     * [メソッドの説明を書きましょう]
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自動生成されたメソッド?スタブ
        int amt = 300;
        int amt2 = 400;
        AccountType saves = new SavingsAccount();
        saves.deposit(amt);

        AccountType check = new CheckingAccount();
        check.deposit(amt);

        System.out.println("-----------------------------");

        //应用上Account类
        Account saves2 = new Account("atest.solid.OpenClosedPrinciple.SavingsAccount");
        saves2.deposit(amt2);
        Account check2 = new Account("atest.solid.OpenClosedPrinciple.CheckingAccount");
        check2.deposit(amt2);
    }

}

输出结果:
Amount deposited in savings acount(\):300
Amount deposited in checkings acount(\):300
-----------------------------
Amount deposited in savings acount(\):400
Amount deposited in checkings acount(\):400

阅读(2762) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~