Application framework with C # development Program

Source: Internet
Author: User
Tags abstract

Brief introduction:

A framework is a custom, generic application development infrastructure that enables the development of complete and full-featured software. In recent years, the development framework of some application areas has been successfully implemented, such as user interface development and data access development. If you can successfully develop a framework, this will be a breakthrough, because it means that development will no longer implement a feature from scratch: for example, if a framework can successfully generate a user interface, it can also generate arbitrary user interface. For example, if we use Java Applets and Servlets, we just need to rewrite certain methods to implement our own code. This is where the Java applets and Servlets are the framework platforms we use. Microsoft has also implemented MFC class inventory, as well as the. NET Framework covered in this article (with Borland VCL, of course).

Prerequisite:

Readers of this article should have experience with C #, or have a basic understanding of Java applets and servlet so that some of the basic concepts covered in this article can be understood.

Tools:

The code written in this article is tested under the Windows 2000+.net Framework release. Because the graphics interface is not required in this article, I use WordPad and the C # command-line compiler to implement it.

Body:

Below, you will see how to implement the infrastructure of an application framework model. The basic core of the development of the application framework is the template approach, which is hidden inside the application, controlling the operation of the application. It is only implemented within the base class and cannot be changed.

The first step is to build the base class for the framework. The base class is the most important class when building a framework.

It has methods that can be overridden, and end users can rewrite these methods to implement their own applications. In addition to these, there is a template method for controlling in the framework process. The framework we are going to build includes three abstract methods that require end user implementations. They are init,run and destroy. They have to be implemented sequentially. Here is the code we implemented:

// 这个类之所以被定义为抽象类,是因为用户方法还没有被实现
abstract class AppFramework
{
 // 构造器调用template方法
 public AppFramework()
 {
  templateMethod();
 }
 // 下面的方法需要最终用户实现
 public abstract void init();
 public abstract void run();
 public abstract void destroy();
 //template方法是框架的核心
 private void templateMethod()
 {
  Console.WriteLine("Initializing Template Engine");
  // template 方法顺序调用所需要的方法
  init();
  run();
  destroy();
  Console.WriteLine("Ending Template Engine");
 }
}

Do not write the template method as a virtual method, as this will give end users the ability to modify the template method, thereby altering the foundation of the entire framework. That is, this is just a task that framework development needs to accomplish, and what the end user does is inherit from the framework base class and rewrite the abstract classes defined in the framework to implement their own custom functionality.

// 从基类继承
class MyClass : AppFramework
{
 // 将抽象方法重写以实现定制的功能
 override public void init()
 {
  Console.WriteLine("MyClass::init");
 }
 override public void run()
 {
  Console.WriteLine("MyClass::run");
 }
 override public void destroy()
 {
  Console.WriteLine("MyClass::destroy");
 }
 // the main method defined
 public static void Main(String [] arg)
 {
  MyClass myClass = new MyClass();
 }
}

Although the main () function is feasible in a class with these overridden correspondence, it is best to place it in a separate class. Here's the whole code.

<code>
using System;
abstract class AppFramework
{
 public AppFramework()
 
 {
  templateMethod();
 }
 public abstract void init();
 public abstract void run();
 public abstract void destroy();
 private void templateMethod()
 {
  Console.WriteLine("Initializing Template Engine");
  init();
  run();
  destroy();
  Console.WriteLine("Ending Template Engine");
 }
}
class MyClass : AppFramework
{
 override public void init()
 {
 
  Console.WriteLine("MyClass::init");
 }
 override public void run()
 {
  Console.WriteLine("MyClass::run");
 }
 override public void destroy()
 {
  Console.WriteLine("MyClass::destroy");
 }
 public static void Main(String [] arg)
 {
  MyClass myClass = new MyClass();
 }
}

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.