Template Method c # simple example

Source: Internet
Author: User

Template Method c # simple example

Template Method c # simple example

Key points: skeleton, step delay to subclass

In this example, the player skeleton is created, and the specific actions are performed from latency to subclass.

Namespace adapterpattern {public partial class TemplateMethodForm: Form {public TemplateMethodForm () {InitializeComponent ();} private void btndisplay_Click (object sender, EventArgs e) {Play p1 = new play1 (); p1.Run (); listBox1.Items. add (BaseData. displayString); Play p2 = new play2 (); BaseData. displayString = ""; p2.Run (); listBox1.Items. add (BaseData. displayString) ;}} public static class BaseData // data transfer station {public static string DisplayString {get; set;} public static int EnemyNumber {get; set ;}} public abstract class Play // template {// skeleton protected abstract void start (); protected abstract void move (); protected abstract void stop (); public void Run () {start (); // delay to subclass move (); stop () ;}} public class play1: Play // implement template method 1 {protected override void start () {BaseData. displayString + = "Player 1 starts running .... ";} protected override void move () {BaseData. displayString + = "move ....... ";} protected override void stop () {BaseData. displayString + = "Stop running ..... ";}} public class play2: Play // method of implementing the template 1 {protected override void start () {BaseData. displayString + = "Player 2 starts running .... ";} protected override void move () {BaseData. displayString + = "move ....... ";} protected override void stop () {BaseData. displayString + = "Stop running ..... ";}}}





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.