Which Design Pattern Is Best Suited?

I have been studying Design Patterns and I am looking to implement them into my latest project.

I am working on a Windows Service that regularly checks a database table for a new entry.
Depending on the entry found, the service will perform a number of processes.

Possible processes include:

1) Generate report and send email with report attached

2) Run query and log data to another table

3) Send notification email

What design pattern is best suited? I have been considering the Factory Pattern.

My design process so far…

1) Create Interface

2) Create Abstract class which implements the Interface

3) Abstract Methods:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- CheckTable()
- GenerateReport()
- SendEmail()
</code>
<code>- CheckTable() - GenerateReport() - SendEmail() </code>
- CheckTable()
- GenerateReport()
- SendEmail()

4) Factory Design Pattern:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- The client(windows service) asks the Factory for a new object (based of the database entry found)
- The factory instantiates this and returns to the client
- The client then calls the relevant methods to send email or run query.
</code>
<code>- The client(windows service) asks the Factory for a new object (based of the database entry found) - The factory instantiates this and returns to the client - The client then calls the relevant methods to send email or run query. </code>
- The client(windows service) asks the Factory for a new object (based of the database entry found)
- The factory instantiates this and returns to the client
- The client then calls the relevant methods to send email or run query.

Does this look correct? Or am I making this more difficult than it is?

Or should I be using the Observor Pattern?


UPDATE:

OK. I have taken your advice and looked for the simplest way of doing this.

Is the below correct? I have created an Interface and then a class to implement this Interface.

There could be a number of different reports/emails to generate. Should these be added as new methods in the Interface?

Do I need an Abstract class?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// interface
internal interface IJobs
{
void SendEmail();
void PublishData();
void GenerateTestReport(string userID, DateTime period);
}
// concrete class
public sealed class JobsFactory : IJobs
{
#region SINGLETON
private static JobsFactory INSTANCE;
private JobsFactory() { }
public static JobsFactory GetInstance()
{
if (INSTANCE == null)
{
INSTANCE = new JobsFactory();
}
return INSTANCE;
}
#endregion
private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance();
#region IJobs Members
public void SendEmail()
{
// send email
}
public void PublishData()
{
// publish data
}
public void GenerateTestReport(string userID, DateTime period)
{
string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period);
}
#endregion
}
</code>
<code>// interface internal interface IJobs { void SendEmail(); void PublishData(); void GenerateTestReport(string userID, DateTime period); } // concrete class public sealed class JobsFactory : IJobs { #region SINGLETON private static JobsFactory INSTANCE; private JobsFactory() { } public static JobsFactory GetInstance() { if (INSTANCE == null) { INSTANCE = new JobsFactory(); } return INSTANCE; } #endregion private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance(); #region IJobs Members public void SendEmail() { // send email } public void PublishData() { // publish data } public void GenerateTestReport(string userID, DateTime period) { string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period); } #endregion } </code>
// interface
internal interface IJobs
{
    void SendEmail();
    void PublishData();
    void GenerateTestReport(string userID, DateTime period);
}


// concrete class
public sealed class JobsFactory : IJobs
{
    #region SINGLETON

    private static JobsFactory INSTANCE;

    private JobsFactory() { }

    public static JobsFactory GetInstance()
    {
        if (INSTANCE == null)
        {
            INSTANCE = new JobsFactory();
        }
        return INSTANCE;
    }

    #endregion

    private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance();

    #region IJobs Members

    public void SendEmail()
    {
        // send email
    }

    public void PublishData()
    {
        // publish data
    }

    public void GenerateTestReport(string userID, DateTime period)
    {
        string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period);
    }

    #endregion
}

4

I’ll tell you a story. Many years ago, when I was a young, inexperienced programmer and GoF had not long before published their Design Patterns book, and Java was a new language and the only user interface library generally available for it was AWT, and AWT applications all looked ugly as hell, my company decided to switch development from C++ to Java. But AWT was out of the question: our applications were required to look and feel like native Win 32 apps, and you simply couldn’t achieve that with AWT back then. So, I was tasked with learning JNI and implementing a framework that would let us build native windows user interfaces using Java.

I had not long before read about Design Patterns, and was eager to use them, so I looked at my project and thought about which ones would fit where. The idea I was most proud of was this: I’d have a native Window object that encapsulated a win32 window handle, but in order to allow the application to attach behaviours to the window, I’d use Chain of Responsibility. This would let applications use existing behaviours and combine them in new ways, and everything would be great.

So I implemented Chain of Responsibility, and it was a bit more complex than just having a single Behaviour object that managed everything, but I wrote a handful of tests using it and everything worked beautifully.

Then we deployed the framework and started to write real applications using it, and you know what? Not once did we ever use the facility to chain multiple behaviours in a single window.

The lesson here is simple: don’t start by using the Design Pattern catalogue to shop for extra flexibility in your application. Start by thinking about what flexibility you are really likely to need, and then about how you can implement that flexibility. It might be that a standard pattern is the best way to achieve that. It might be there’s a simpler, more direct way. It may even be the case that you have a rigid, well-defined problem that is small enough that you can design the entire solution simply without needing any scope to change behaviour later, in which case anything even approximating a design pattern would likely be overkill.

To borrow a phrase from Kent Beck, “do the simplest thing that could possibly work”. If that happens to end up looking like a pattern, that’s great. But don’t worry if it doesn’t.

2

I’d just start out using no patterns.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var records = CheckTable();
foreach(var record in records)
{
GenerateReport(record);
SendEmail(record);
}
</code>
<code>var records = CheckTable(); foreach(var record in records) { GenerateReport(record); SendEmail(record); } </code>
var records = CheckTable();
foreach(var record in records)
{
    GenerateReport(record);
    SendEmail(record);
}

Start simple, not everything needs a pattern. If you start doing a lot of different tasks, or allow tasks to be configurable in the order and the number of tasks run per record, then think about abstracting it out by refactoring.

Since you need to do what seems to be polling, you could take a look at the Observer Pattern. This would allow your service to call your methods so that they can take appropriate action.

That being said, as it has been mentioned in the comments, you seem to have design patterns the other way round. The task should lead you to a design pattern. Trying to wrap your requirements around a particular design pattern is not the best way to approach design.

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

Which Design Pattern Is Best Suited?

I have been studying Design Patterns and I am looking to implement them into my latest project.

I am working on a Windows Service that regularly checks a database table for a new entry.
Depending on the entry found, the service will perform a number of processes.

Possible processes include:

1) Generate report and send email with report attached

2) Run query and log data to another table

3) Send notification email

What design pattern is best suited? I have been considering the Factory Pattern.

My design process so far…

1) Create Interface

2) Create Abstract class which implements the Interface

3) Abstract Methods:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- CheckTable()
- GenerateReport()
- SendEmail()
</code>
<code>- CheckTable() - GenerateReport() - SendEmail() </code>
- CheckTable()
- GenerateReport()
- SendEmail()

4) Factory Design Pattern:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- The client(windows service) asks the Factory for a new object (based of the database entry found)
- The factory instantiates this and returns to the client
- The client then calls the relevant methods to send email or run query.
</code>
<code>- The client(windows service) asks the Factory for a new object (based of the database entry found) - The factory instantiates this and returns to the client - The client then calls the relevant methods to send email or run query. </code>
- The client(windows service) asks the Factory for a new object (based of the database entry found)
- The factory instantiates this and returns to the client
- The client then calls the relevant methods to send email or run query.

Does this look correct? Or am I making this more difficult than it is?

Or should I be using the Observor Pattern?


UPDATE:

OK. I have taken your advice and looked for the simplest way of doing this.

Is the below correct? I have created an Interface and then a class to implement this Interface.

There could be a number of different reports/emails to generate. Should these be added as new methods in the Interface?

Do I need an Abstract class?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// interface
internal interface IJobs
{
void SendEmail();
void PublishData();
void GenerateTestReport(string userID, DateTime period);
}
// concrete class
public sealed class JobsFactory : IJobs
{
#region SINGLETON
private static JobsFactory INSTANCE;
private JobsFactory() { }
public static JobsFactory GetInstance()
{
if (INSTANCE == null)
{
INSTANCE = new JobsFactory();
}
return INSTANCE;
}
#endregion
private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance();
#region IJobs Members
public void SendEmail()
{
// send email
}
public void PublishData()
{
// publish data
}
public void GenerateTestReport(string userID, DateTime period)
{
string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period);
}
#endregion
}
</code>
<code>// interface internal interface IJobs { void SendEmail(); void PublishData(); void GenerateTestReport(string userID, DateTime period); } // concrete class public sealed class JobsFactory : IJobs { #region SINGLETON private static JobsFactory INSTANCE; private JobsFactory() { } public static JobsFactory GetInstance() { if (INSTANCE == null) { INSTANCE = new JobsFactory(); } return INSTANCE; } #endregion private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance(); #region IJobs Members public void SendEmail() { // send email } public void PublishData() { // publish data } public void GenerateTestReport(string userID, DateTime period) { string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period); } #endregion } </code>
// interface
internal interface IJobs
{
    void SendEmail();
    void PublishData();
    void GenerateTestReport(string userID, DateTime period);
}


// concrete class
public sealed class JobsFactory : IJobs
{
    #region SINGLETON

    private static JobsFactory INSTANCE;

    private JobsFactory() { }

    public static JobsFactory GetInstance()
    {
        if (INSTANCE == null)
        {
            INSTANCE = new JobsFactory();
        }
        return INSTANCE;
    }

    #endregion

    private readonly WorkbookFactory _workbookFactory = WorkbookFactory.GetInstance();

    #region IJobs Members

    public void SendEmail()
    {
        // send email
    }

    public void PublishData()
    {
        // publish data
    }

    public void GenerateTestReport(string userID, DateTime period)
    {
        string reportTestFilePath = this._workbookFactory.BuildTestReportWorkbook(userID, period);
    }

    #endregion
}

4

I’ll tell you a story. Many years ago, when I was a young, inexperienced programmer and GoF had not long before published their Design Patterns book, and Java was a new language and the only user interface library generally available for it was AWT, and AWT applications all looked ugly as hell, my company decided to switch development from C++ to Java. But AWT was out of the question: our applications were required to look and feel like native Win 32 apps, and you simply couldn’t achieve that with AWT back then. So, I was tasked with learning JNI and implementing a framework that would let us build native windows user interfaces using Java.

I had not long before read about Design Patterns, and was eager to use them, so I looked at my project and thought about which ones would fit where. The idea I was most proud of was this: I’d have a native Window object that encapsulated a win32 window handle, but in order to allow the application to attach behaviours to the window, I’d use Chain of Responsibility. This would let applications use existing behaviours and combine them in new ways, and everything would be great.

So I implemented Chain of Responsibility, and it was a bit more complex than just having a single Behaviour object that managed everything, but I wrote a handful of tests using it and everything worked beautifully.

Then we deployed the framework and started to write real applications using it, and you know what? Not once did we ever use the facility to chain multiple behaviours in a single window.

The lesson here is simple: don’t start by using the Design Pattern catalogue to shop for extra flexibility in your application. Start by thinking about what flexibility you are really likely to need, and then about how you can implement that flexibility. It might be that a standard pattern is the best way to achieve that. It might be there’s a simpler, more direct way. It may even be the case that you have a rigid, well-defined problem that is small enough that you can design the entire solution simply without needing any scope to change behaviour later, in which case anything even approximating a design pattern would likely be overkill.

To borrow a phrase from Kent Beck, “do the simplest thing that could possibly work”. If that happens to end up looking like a pattern, that’s great. But don’t worry if it doesn’t.

2

I’d just start out using no patterns.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var records = CheckTable();
foreach(var record in records)
{
GenerateReport(record);
SendEmail(record);
}
</code>
<code>var records = CheckTable(); foreach(var record in records) { GenerateReport(record); SendEmail(record); } </code>
var records = CheckTable();
foreach(var record in records)
{
    GenerateReport(record);
    SendEmail(record);
}

Start simple, not everything needs a pattern. If you start doing a lot of different tasks, or allow tasks to be configurable in the order and the number of tasks run per record, then think about abstracting it out by refactoring.

Since you need to do what seems to be polling, you could take a look at the Observer Pattern. This would allow your service to call your methods so that they can take appropriate action.

That being said, as it has been mentioned in the comments, you seem to have design patterns the other way round. The task should lead you to a design pattern. Trying to wrap your requirements around a particular design pattern is not the best way to approach design.

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