面向对象的编程和放大器;交易 [英] Object-oriented programming & transactions

查看:138
本文介绍了面向对象的编程和放大器;交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小介绍:

类包含字段和方法(让我跳过这个特性时间)。结果
字段重新present一个的状态的类的。结果
方法描述的行为的类。

Class contains fields and methods (let me skip properties this time).
Fields represent a state of the class.
Methods describe behavior of the class.

在一个精心设计的类,方法不会改变类的状态,如果它抛出一个异常,对不对? (换言之,无论发生什么事情,一流的国家不应被损坏)

In a well-designed class, a method won't change the class's state if it throws an exception, right? (In other words, whatever happens, class's state shouldn't be corrupted)

问:

有一个框架,一个设计模式,最好的做法还是一种编程语言调用的方法序列中的交易风格,让无论是类的状态没有得到改变(异常的情况下),或一切成功?

Is there a framework, a design pattern, best practice or a programming language to call a sequence of methods in a transactional style, so that either class's state don't get changed (in case of exception), or everything succeeds?

例如:

// the class foo is now in the state S1
foo.MoveToState2();
// it is now (supposed to be) in the state S2
foo.MoveToFinalState();
// it is now (supposed to be) in the state, namely, S3

当然,可能都在 MoveToState2发生异常() MoveToFinalState()。但是,从code的这块我想这个类是无论是在状态S1和S3。

Surely, an exception might occur both in MoveToState2() and MoveToFinalState(). But from this block of code I want the class foo to be either in the state S1 or S3.

这是一个简单的场景所涉及单个类,没有如果的,没有,而的,无副作用,但我希望这个想法是清楚的。

This is a simple scenario with a single class involved, no if's, no while's, no side effects, but I hope the idea is clear.

推荐答案

看看在 Memento模式

Memento模式是提供一个对象恢复到previous状态的能力的设计模式(通过回滚撤销)。

The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).

这篇关于面向对象的编程和放大器;交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆