To a foreign language about the Design Model

Source: Internet
Author: User
Address: http://www.codeproject.com/dotnet/Design_Pattern_Usage.asp

Article Title:Use of design patterns in web projects to decouple the UI and business logic implementations

Download source-143 KB

Introduction

As the title suggests, this is an article describing possible design pattern usage in medium/large scale web projects in order to decouple the communication between the user interface layer and the business/application layer.

Design Patterns: In software engineering parlance, design patterns are standard solutions to common problems in software design. design Patterns usually describe abstract systems of interaction between classes, objects, and communication flows. so, a description of a set of interacting classes that provide a generalized solution framework to a generalized/specific design problem in a specific context can be said as a design pattern. in other words, a pattern suggests a time tested solution (involving a set of interacting classes) to a particle software design problem. it is assumed that the readers have exposure to fundamental design patterns and are acquainted with C # And. NET environment. the two major patterns that will be used in this sample program are the Facade Pattern and the Strategy pattern.

Background

Facade Pattern: Facade is an object oriented design pattern; essential, an object that provides a simplified/specified lidated interface to a larger piece of functionality. when designing good programs, Good Designers tend to attempt to avoid excess coupling between modules, and the Facade Pattern is a popular structural pattern for doing the same. A very good example of classical Facade Pattern usage is described here.

Strategy Pattern: Strategy is also an object oriented, structural design pattern, essential tially concerned with decoupling of an object and its behavior so that a single object can perform several different behaviors depending on the run-time scenarios. A lot of variables designers try to implement the Strategy pattern by simple subclassing (subclass an object to achieve a different functionality ). however, that is 'static 'strategy implementation as you have to create a different object in order to replace the behavior of the original object. with the actual strategy Pattern Implemented, changing the object's strategy will make it behave differently, and there is no need for providing any conditional statements to do this. check this example here.

The code in this article will explain the usage of the above mentioned patterns in. net Based Web projects to decouple the user interface layer (developed by specialized UI developers) from the business logic layer (developed by a different set of developers who are good in implementing business logic ). let's say our program has two different business logic implementations and the implementation that will be called depends on the input provided from the UI. the independent business logic implementations are modeled using the strategy pattern. the UI program need not know what business implementation shocould actually be called. the implementation is hidden from the user interface by a facade class object. the responsibility of the user interface ends in sending the input parameter SS to the facade object. the facade object, in turn, initializes the strategy object based on the user input intercepted.

Interpreting the code

The attached code contains a sample ASP. NET Web client and a set of class libraries. the Web client accepts a character value, which triggers a participant business process to execute; if the input is X, then the business process implemented in the class libraryIndependantEntity1Shocould be executed, else the Business Process encapsulated inIndependantEntity2Shocould be executed.

This is a seemingly easy requirement; however, it does 'model' The requirements of a medium to large scale enterprise-wide web application where different business processes need to be triggered based on inputs received ed from the Web users. here, a conscious attempt has been made to decouple the Web Client completely from the business layer class libraries; the Web Client has no knowledge of what business process will execute depending on the input. the only responsibility of the Web Client is to create a proper input message and send that message to the facade object. the facade object intercepts the requests, parses the requests, and acts as the client to the business layer logic algorithms implemented in the assembliesIndependantEntity1AndIndependantEntity2(May be implemented by different teams). So we have designed the client in a manner that decouples it from the implementation details of any specific business logic or algorithm.

The interface for implementing the Strategy pattern is calledIStrategy, And it contains only a single method calledProcessMessage. Since an interface is like a contract, the classes which implement this interface must provide the implementation body for this method.IA1AndIA2Are two classes which implement two independent and unrelated business process functionalities for our sample project. Both these classes implementProcessMessageMethod ofIStrategyInterface.

Collapse
/*Interface definition. Interface.cs*/using System;namespace InterfaceProj{public interface IStrategy{bool ProcessMessage(string strXML, out string strMsg);}}//-------------------------------------------------------------//-------------------------------------------------------------/*Interface implementation. IA1.cs*/using System;using InterfaceProj;namespace IndependantEntity1{public class IA1: IStrategy{public IA1(){//// TODO: Add constructor logic here//}public bool ProcessMessage(string strXML,out string strMsg){//parse XMl and do independant activity 1strMsg = "Inside IA1";return true;}private void DoSomeOtherIndependantActivity(){}}}

TheRequestFacadeClass acts as the interceptor of all the Web requests. This class is responsible for initializing the strategy object and callingProcessMessageFunctionality. This class is the implementation of the facade pattern.

Collapse
using System;using InterfaceProj;using IndependantEntity1;using IndependantEntity2;namespace RequestFacadeIntercept{/// <SUMMARY>/// Summary description for RequestFacade./// </SUMMARY>public class RequestFacade{private IStrategy objStrategy;public RequestFacade(){//// TODO: Add constructor logic here//}/// <SUMMARY>/// Process the input XML /// Depending on the parsing instantiate the strategy object///</SUMMARY>public string SendRequest(string strInputXML){string strMsg;processXML(strInputXML);//call ProcessobjStrategy.ProcessMessage(@"Input string extracted from XML",out strMsg);return strMsg;}private void processXML(string strInputXML){//parse the xml and take appropriate decisionsif (strInputXML.Equals("X"))objStrategy = new IA1();elseobjStrategy = new IA2();}}}

The interface for implementing the Strategy pattern is calledIStrategy, And it contains only a single method calledProcessMessage. Since an interface is like a contract, the classes which implement this interface must provide the implementation body for this method.IA1AndIA2Are two classes which implement two independent and unrelated business process functionalities for our sample project. Both these classes implementProcessMessageMethod ofIStrategyInterface.

Collapse
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using RequestFacadeIntercept;namespace SampleWebproject{.............public class WebForm1 : System.Web.UI.Page{..........................private void Button1_Click(object sender, System.EventArgs e){string strSendXML = string.Empty;strSendXML = TextBox1.Text.ToUpper();//populate the string with some valid input XML//create the RequestFacade object//and send the XML input string to the //SendRequest method for appropriate actionRequestFacade reqfacade = new RequestFacade();lblResponse.Text  = reqfacade.SendRequest(strSendXML);//the web ui developer need to just create the //XML based on pre supplied (out of scope of this demo)//and call SendRequest}}}
Points of interest

The use of design patterns is very common in any medium to large scale project. this sample just extends strates the use of strategy and facade patterns in web projects. and the fact that the Web Client can be decoupled from the business logic implementation so easily by use of design patterns shocould encourage more use of such patterns in projects during the design phase. this will also free up any sort of dependency between UI designers and business logic developers; specialized people can keep themselves busy doing their own stuff without bothering about how to communicate with each others. such loose coupling between the UI and the business logic also allows the business logic to be modified without the knowledge of the UI as long as the input message intercepted by the facade layer remains the same. the only code which needs to be altered for any future changes is the facade object. so, application maintenance and scalability issues are also addressed by the use of such patterns.

This is just a sample. There are other ways to achieve the same objectives. So keep processing ing design patterns.

There are many people in the garden who are writing design patterns. I have read a series of articles from this aspect by teacher Lu Zhenyu and Teacher Li huijun, and I think it is wonderful to write, although I have not yet had a deep understanding of the design model, I also benefited a lot from getting started!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.