ruby学习笔记系列(4)

Access Control

When designing a class interface, it’s important to consider just how much access to
your class you’ll be exposing to the outside world. Allow too much access into your
class, and you risk increasing the coupling in your application—users of your class will
be tempted to rely on details of your class’s implementation, rather than on its logical
interface. The good news is that the only easy way to change an object’s state in Ruby
is by calling one of its methods. Control access to the methods, and you’ve controlled
access to the object. A good rule of thumb is never to expose methods that could leave
an object in an invalid state. Ruby gives you three levels of protection.
• Public methods can be called by anyone—no access control is enforced.Methods
are public by default (except for initialize, which is always private).
• Protected methods can be invoked only by objects of the defining class and its
subclasses. Access is kept within the family.
• Privatemethods cannot be called with an explicit receiver—the receiver is always
self. This means that private methods can be called only in the context of the
current object; you can’t invoke another object’s private methods.
The difference between “protected” and “private” is fairly subtle and is different in
Ruby than in most common OO languages. If a method is protected, it may be called
by any instance of the defining class or its subclasses. If a method is private, it may
be called only within the context of the calling object—it is never possible to access
another object’s private methods directly, even if the object is of the same class as the
caller.

Ruby differs from other OO languages in another important way. Access control is
determined dynamically, as the program runs, not statically. You will get an access
violation only when the code attempts to execute the restricted method.
Specifying Access Control
You specify access levels to methods within class or module definitions using one or
more of the three functions public, protected, and private. You can use each function
in two different ways.
If used with no arguments, the three functions set the default access control of subsequently
defined methods. This is probably familiar behavior if you’re a C++ or Java
programmer, where you’d use keywords such as public to achieve the same effect.
class MyClass
def method1 # default is 'public'
#...
end
protected # subsequent methods will be 'protected'
def method2 # will be 'protected'
#...
end
private # subsequent methods will be 'private'
def method3 # will be 'private'
#...
end
public # subsequent methods will be 'public'
def method4 # and this will be 'public'
#...
end
end
Alternatively, you can set access levels of named methods by listing them as arguments
to the access control functions.
class MyClass
def method1
end
# ... and so on
public :method1, :method4
protected :method2
private :method3
end
It’s time for some examples. Perhapswe’remodeling an accounting system where every
debit has a corresponding credit. Because we want to ensure that no one can break this
rule, we’ll make the methods that do the debits and credits private, and we’ll define our
external interface in terms of transactions.

class Accounts
def initialize(checking, savings)
@checking = checking
@savings = savings
end
private
def debit(account, amount)
account.balance =
amount
end
def credit(account, amount)
account.balance += amount
end
public
#...
def transfer_to_savings(amount)
debit(@checking, amount)
credit(@savings, amount)
end
#...
end
Protected access is used when objects need to access the internal state of other objects
of the same class. For example, we may want to allow individual Account objects to
compare their raw balances but may want to hide those balances from the rest of the
world (perhaps because we present them in a different form).
class Account
attr_reader :balance # accessor method 'balance'
protected :balance # and make it protected
def greater_balance_than(other)
return @balance > other.balance
end
end
Because the attribute balance is protected, it’s available only within Account objects.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值