抽象工厂模式(abstract)创建型模式 c#简单例子

抽象工厂模式(abstract)创建型模式 c#简单例子

适用于玩家用户数play1、play2....有变化,而行为move、jum无变化时

玩家playone的行为:向左移动和向上跳跃

玩家playtwo的行为:向右移动和向下跳跃

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace adapterpattern
{
    public partial class abstractForm : Form
    {
        public abstractForm()
        {
            InitializeComponent();
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            playFactory pf1 = new playone();//隐藏行为move和jump.
            playWorld p1 = new playWorld(pf1);//行为向左移动和向上跳跃
            this.listBox1.Items.Add(p1.createPlay());
            playWorld p2 = new playWorld(new playtwo());//行为向右移动和向下跳跃
            this.listBox1.Items.Add(p2.createPlay());
        }

    }
    public class playWorld//实体工厂和抽象工厂的交接
    {
        imove move;
        ijump jump;
        public playWorld(playFactory pp)
        {
            this.move = pp.move();
            this.jump = pp.jump();
        }
        public string createPlay()
        {
            move.move();
            jump.jump();
            return move.movestring + jump.jumpstring;

        }

    }
    public abstract class playFactory//抽象工厂
    {
        public abstract imove move();
        public abstract ijump jump();

    }
    public class playone : playFactory//抽象工厂实例1
    {

        public override imove move()
        {
            return new leftmove();
        }

        public override ijump jump()
        {
            return new topJump();
        }
    }
    public class playtwo : playFactory//抽象工厂实例2
    {


        public override imove move()
        {
            return new rightmove();
        }

        public override ijump jump()
        {
            return new downJump();
        }
    }
    public abstract class imove//抽象行为imove
    {
        public string movestring;
        public abstract void move();
    }
    public abstract class ijump//抽象行为jump
    {
        public string jumpstring;
        public abstract void jump();
    }
    public class leftmove : imove//move行为实例1
    {

        public override void move()
        {
            movestring = "左移动";
        }
    }
    public class rightmove : imove//move行为实例2
    {
        public override void move()
        {
            movestring = "右移动";
        }
    }
    public class topJump : ijump//jump行为实例1
    {
        public override void jump()
        {
            jumpstring = "向上跳";
        }
    }
    public class downJump : ijump//jump行为实例2
    {
        public override void jump()
        {
            jumpstring = "向下跳";
        }
    }
}
当需要增加新玩家时,只要再增加一个继承playfacotry的类,其他都不需要改动

新玩家行为:向左移动和向下跳跃

public class newplay:playFactory//新增加玩家:向左移动和向下跳跃
    {
        public override imove move()
        {
            return new leftmove();
        }

        public override ijump jump()
        {
            return new downJump();
        }
    }

最后在客户端增加一句就可以了,很是方便。

            playWorld newp = new playWorld(new newplay());
            this.listBox1 .Items .Add (newp .createPlay ());

抽象工厂模式(abstract)创建型模式 c#简单例子

上一篇:memcached—如何在Windows操作系统中安装、启动和卸载memcached


下一篇:C#基础