博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式 建造者模式
阅读量:4971 次
发布时间:2019-06-12

本文共 3653 字,大约阅读时间需要 12 分钟。

先写一个建造者的抽象类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BuildPattern{    abstract class PersonBuilder    {        protected Graphics g;        protected Pen p;        public PersonBuilder(Graphics g, Pen pen)        {            this.g = g;            this.p = pen;        }        public abstract void BuildHead();        public abstract void BuildBody();        public abstract void BuildArmLeft();        public abstract void BuildArmRight();        public abstract void BuildLegLeft();        public abstract void BuildLegRight();    }}

在写一个建造者的导演类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BuildPattern{    class PersonDirector    {        private PersonBuilder pb;        public PersonDirector(PersonBuilder pb)        {            this.pb = pb;        }        public void CreatePerson()        {            pb.BuildHead();            pb.BuildBody();            pb.BuildArmLeft();            pb.BuildArmRight();            pb.BuildLegLeft();            pb.BuildLegRight();        }    }}

在写两个类继承建造者类,只需写一些细节。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BuildPattern{    class PersonThinBuilder:PersonBuilder    {        public PersonThinBuilder(Graphics g, Pen p) : base(g, p)        {        }        public override void BuildHead()        {            g.DrawEcllipse(p, 50, 20, 30, 30);        }        public override void BuildBody()        {            g.DrawRectangle(p, 60, 50, 10, 50);        }        public override void BuildArmLeft()        {            g.DrawLine(p, 60, 50, 40, 100);        }        public override void BuildArmRight()        {            g.DrawLine(p, 70, 50, 90, 100);        }        public override void BuildLegLeft()        {            g.DrawLine(p, 60, 100, 45, 150);        }        public override void BuildLegRight()        {            g.DrawLine(p, 70, 100, 85, 150);        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BuildPattern{    class PersonFatBuilder : PersonBuilder    {        public PersonFatBuilder(Graphics g, Pen p) : base(g, p)        {        }        public override void BuildHead()        {            g.DrawEcllipse(p, 50, 20, 30, 30);        }        public override void BuildBody()        {            g.DrawRectangle(p, 60, 50, 100, 80);        }        public override void BuildArmLeft()        {            g.DrawLine(p, 60, 50, 40, 100);        }        public override void BuildArmRight()        {            g.DrawLine(p, 70, 50, 90, 100);        }        public override void BuildLegLeft()        {            g.DrawLine(p, 60, 100, 45, 150);        }        public override void BuildLegRight()        {            g.DrawLine(p, 70, 100, 85, 150);        }    }}

在写测试类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BuildPattern{    class Program    {        static void Main(string[] args)        {            Pen p = new Pen(Color.Yellow);            PersonThinBuilder ptb = new PersonThinBuilder(pictureBox1.CreateGraphics(), p);            PersonDirector pdThin = new PersonDirector(ptb);            pdThin.CreatePerson();            PersonFatBuilder pfd=new PersonFatBuilder(pictureBox1.CreateGraphics(), p);            PersonDirector pdFat = new PersonDirector(pfb);            pdFat.CreatePerson();        }    }}

转载于:https://www.cnblogs.com/yufenghou/p/6025165.html

你可能感兴趣的文章
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
webdriver api
查看>>
apache 实现图标缓存客户端
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
P3565 [POI2014]HOT-Hotels
查看>>
MongoDB的简单使用
查看>>
hdfs 命令使用
查看>>
prometheus配置
查看>>
【noip2004】虫食算——剪枝DFS
查看>>
python 多进程和多线程的区别
查看>>
sigar
查看>>