Abstract base class implementing an interface, providing a different one to its subclasses

Let’s say I have an interface for the Command pattern with undo functionality:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>interface ICommand
{
void Execute();
void Undo();
}
</code>
<code>interface ICommand { void Execute(); void Undo(); } </code>
interface ICommand
{
    void Execute();
    void Undo();
}

This comes with a contract. Basically the command can be in one of two states – either it has been executed or not, and that determines the only method that can be called.

To add a check for this, I could create an abstract class called SafeCommand or simply CommandBase with this protection:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>abstract class CommandBase : ICommand
{
private bool executed = false;
public void Execute()
{
if (executed)
throw new InvalidOperationException();
ExecuteImpl();
executed = true;
}
public void Undo()
{
if (!executed)
throw new InvalidOperationException();
UndoImpl();
executed = false;
}
protected abstract void ExecuteImpl();
protected abstract void UndoImpl();
}
</code>
<code>abstract class CommandBase : ICommand { private bool executed = false; public void Execute() { if (executed) throw new InvalidOperationException(); ExecuteImpl(); executed = true; } public void Undo() { if (!executed) throw new InvalidOperationException(); UndoImpl(); executed = false; } protected abstract void ExecuteImpl(); protected abstract void UndoImpl(); } </code>
abstract class CommandBase : ICommand
{
    private bool executed = false;

    public void Execute()
    {
        if (executed)
            throw new InvalidOperationException();

        ExecuteImpl();
        executed = true;
    }

    public void Undo()
    {
        if (!executed)
            throw new InvalidOperationException();

        UndoImpl();
        executed = false;
    }

    protected abstract void ExecuteImpl();
    protected abstract void UndoImpl();
}

When I want to implement the actual command functionality, I now have to override the ExecuteImpl and UndoImpl methods instead.

Another way to solve this could be to make the methods virtual, but I prefer this way because I can’t forget to implement the methods and also can’t omit the base call. Also, in other cases where I need to have some code before and after the call, this will be the only solution.

I first encountered this pattern in WPF, where it’s actually used twice in the same inheritance hierarchy with UIElement Measure, virtual MeasureCore and FrameworkElement override MeasureCore and virtual MeasureOverride methods.

My question is, is there a name for this pattern? It seems to me like some sort of a (forced) Adapter using inheritance instead of composition.

8

The only real GOF pattern here I can see in this construct is the “template method pattern”.

The public interface of any CommandBase derivation – as seen from any code outside the class hierarchy – does not change, so I don’t see this as

“providing a different interface to the subclasses”,

as your question title imposes. My interpretation of this is more “providing an easier way to implement the interface with error checking”.

The more interesting question here is: is there a LSP violation? I think not, if the contract is written down exactly as stated in the question, with the additional remark that Execute and Undo will throw exceptions when called in wrong order. Then the template method pattern will actually help to ensure the contract.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật