- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我确实有 Customer 和 Account 业务对象,它们分别实现了 ICustomer
和 IAccount
。我还有每个业务对象的服务层。 AccountService
(addAccount
、deposit()
、withdraw()
方法)和 CustomerService(addCustomer
、addAdressForACustomer
、removeCustomer()
等方法)。
我希望我的系统使用 Observer 模式,以便在其中一项交易(withdraw()
或 deposit()
方法成功)时向客户发送电子邮件通知。我理解客户是观察者,账户是应该通知客户的主体。但是客户可以有不同的帐户,并且应该在交易发生时通知每个帐户。
我应该通过扩展 Observable Java 类将 notifyCustomer()
方法添加到我的 Account 业务对象或服务中吗?我有点困惑如何实现它。在这种情况下,观察者模式应该是一件好事吗?
谢谢
这是我的业务对象的示例代码
package business;
import java.math.BigDecimal;
public class Account implements IAccount {
private int accountNumber;
private IParty owner;
public Account(int accountNumber, IParty owner, BigDecimal balance) {
super();
this.accountNumber = accountNumber;
this.owner = owner;
this.balance = balance;
}
private BigDecimal balance;
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public IParty getOwner() {
return owner;
}
public void setOwner(IParty owner) {
this.owner = owner;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package business;
import java.util.Date;
public class Person extends Party implements IPerson {
private Date birthDate;
public Person(String name, IAddress address,Date birthDate) {
super(name, address);
this.birthDate=birthDate;
// TODO Auto-generated constructor stub
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
package service;
import java.math.BigDecimal;
import java.util.List;
import business.Account;
import business.IAccount;
import business.IAccountEntry;
import business.IParty;
public class AccountService implements IAccountService {
/* (non-Javadoc)
* @see service.IAccountService#createAccount()
*/
IDaoService daoService;
@Override
public void createAccount(){
//IAccount account= new Account(accountNumber,party,amount) ;
//daoService.create();
}
/* (non-Javadoc)
* @see service.IAccountService#removeAccount(business.IAccount)
*/
public void removeAccount(){
}
/* (non-Javadoc)
* @see service.IAccountService#addAccountEntry(business.IAccountEntry)
*/
@Override
public void addAccountEntry(IAccountEntry accountEntry){
}
/* (non-Javadoc)
* @see service.IAccountService#removeAccountEntry(business.IAccountEntry)
*/
@Override
public void removeAccountEntry(IAccountEntry accountEntry){
}
/* (non-Javadoc)
* @see service.IAccountService#deposit(business.IAccount, java.math.BigDecimal)
*/
@Override
public void deposit(IAccount account, BigDecimal amount){
}
/* (non-Javadoc)
* @see service.IAccountService#withdraw(business.IAccount, java.math.BigDecimal)
*/
@Override
public void withdraw(IAccount account, BigDecimal amount){
}
/* (non-Javadoc)
* @see service.IAccountService#getCurrentBalance()
*/
@Override
public BigDecimal getCurrentBalance(){
return null;
}
/* (non-Javadoc)
* @see service.IAccountService#findAccountsByParty(business.IParty)
*/
@Override
public List<IAccount> findAccountsByParty(IParty party){
return null;
}
/* (non-Javadoc)
* @see service.IAccountService#findAccountByAccountNumber(int)
*/
@Override
public IAccount findAccountByAccountNumber(int accountNumber){
return null;
}
@Override
public void removeAccount(IAccount account) {
// TODO Auto-generated method stub
}
}
最佳答案
你应该使用观察者模式吗?这可能是个好主意,因为您可以清楚地分离关注点、简化您的设计并且不会在任何地方困惑业务逻辑。
我应该通过扩展 Observable Java 类向我的帐户业务对象或服务中添加 notifyCustomer() 方法吗?
这是一个明确的否定。 Observable
是 Java 1 类,设计不佳。首先,它是一个类,而不是一个接口(interface),因此您必须扩展它才能使用它。这严重限制了您的设计选择。相反,创建您自己的接口(interface)并让您的类实现该接口(interface)而不是进行观察。
由于您想要一个示例并且您有多个帐户:
public interface AccountObserver{
public void withdrawl(IAccount from, BidDecimal amount);
public void deposit(IAccount to, BigDecimal amount);
}
我包含了一个 IAccount
引用,以便客户知道更新了哪个帐户,因为您要管理多个帐户。
关于java - 当客户的帐户进行存款交易时,我是否应该使用观察者模式通知客户对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054632/
AngularFire2 文档演示了以下将观察者绑定(bind)到您的 Firebase 可观察对象的模式: this.item = af.database.object('/item'); {{ (
我正在使用观察者模式和 BlockingQueue 添加一些实例。现在在另一种方法中,我正在使用队列,但似乎 take() 一直在等待,即使我这样做: /** {@inheritDoc} */ @Ov
我遇到问题 - 创建具有不同属性的订单。在我的工作案例中,用户有大约 10 个不同的属性,每个属性有 3-4 个变体。 我正在使用 Angular js,并编写了两个函数: 二传手 $scope
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
假设我想创建一组基于类型的观察者。也就是说,当他们收到事件通知时,他们会被告知其中一个参数的类型,然后根据是否可以对该类型进行操作来决定是否采取行动。 有没有简单的方法可以做到这一点?我认为这对于泛型
快速提问。 当 setChanged() 和 notifyObservers() 被调用并且 Observer.update() 运行时,它是否在一个新的 Thread,还是在同一个Thread中?
我必须为可观察对象和观察者创建代码。正如您所知,每个观察者都有自己的“更新”方法。 在我的可观察代码中,我使用了“notifyObservers”方法。它的参数是一个字符串数组,其中包含有关我的项目的
是否有一种设计模式可以形成“复合”观察者/可观察对象? 我的意思是我有一个可观察的 A 通知它的听众一些变化。 每个监听器也是一个可观察对象,并通知自己的监听器(在它执行的某些操作中,哪个操作是由第一
我对以下代码为何不起作用感到有点困惑: MutableLiveData mutableTest = new MutableLiveData<>(); MediatorLiveData mediator
场景:我有两个名为 FirstFragment 的 fragment 和 UnitFragment .我来自 FirstFragment至UnitFragment选择一个单位返回FirstFragme
我想了解如何 平面 map 作品。我知道这是处理 Observable> 的一种方式。 无论如何,我正在测试它的行为并陷入困境: let plusDom = document.querySelecto
我有 public class mammal implements Imammle { public String getName() { return "Mammal";
如果我使用 KVO 来观察我的播放器项目,如下所示: AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
我想在正在使用的变量上使用观察者,这是我的代码: const get = Ember.get; uploader:Ember.inject.service('uploader'), progressC
我一直在使用Laravel Observers每当我的模型被删除/更新/保存时处理我的应用程序逻辑。我注意到在某些情况下这些观察者不会被触发。例如,如果我使用 Model::where(active,
我有一个工作人员服务来发送一些简单的通知。但现在,我想在发送之前检查数据库中的一些数据。 但是,我不能在工作线程中使用观察者,它必须在主线程中。我有一个错误类型 java.lang.IllegalSt
我有 2 个字段:美元和日元。当我更改美元字段时,我想在日元字段中显示转换后的数字,反之亦然。 我的问题是这样的: 如果我有 2 个带有观察者的字段,例如: addTextChangedListene
在 View Controller 中,我创建了一个 subview 数组,可以随时从父 View 中删除,因此它们的生命周期比 View Controller 的生命周期短。 创建它们时,我几乎这样
如何在另一个线程上运行 RxJava,因为主线程上的工作太多。 我在一个方法内运行观察者,下面是代码 fragment : public void updatePie() { RxJavaPlugin
我是一名优秀的程序员,十分优秀!