Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化...

 

ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Machines;
 3None.gif
 4None.gifnamespace Gof.Test.Factory.Plan
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class MixerPlanner : MachinePlanner
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        public MixerPlanner(Machine machine) : base(machine)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        public override bool ContineWork
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            get
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return Machine.HasDone;
16ExpandedSubBlockEnd.gif            }

17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19ExpandedSubBlockEnd.gif    }

20ExpandedBlockEnd.gif}

一个类层次:

ContractedBlock.gif ExpandedBlockStart.gif 接口定义
1None.gifusing System;
2None.gif
3None.gifnamespace Gof.Test.Factory
4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
5InBlock.gif    public interface IAcount
6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
7ExpandedSubBlockStart.gifContractedSubBlock.gif        string UserNamedot.gif{get;set;}
8ExpandedSubBlockEnd.gif    }

9ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 具体类1
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Factory
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class AcountMyself:IAcount
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public AcountMyself(string myselft)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _name = myselft;
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        private string _name;
12InBlock.gif
13ContractedSubBlock.gifExpandedSubBlockStart.gif        IAcount 成员#region IAcount 成员
14InBlock.gif
15InBlock.gif        public string UserName
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            get
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                return _name;
20ExpandedSubBlockEnd.gif            }

21InBlock.gif            set
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                _name = value;
24ExpandedSubBlockEnd.gif            }

25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27ExpandedSubBlockEnd.gif        #endregion

28ExpandedSubBlockEnd.gif    }

29ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 具体类2
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Factory
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// AcountYourself 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class AcountYourself:IAcount
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        public AcountYourself(string yourself)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            _name = yourself;
13ExpandedSubBlockEnd.gif        }

14InBlock.gif
15InBlock.gif        private string _name;
16InBlock.gif
17ContractedSubBlock.gifExpandedSubBlockStart.gif        IAcount 成员#region IAcount 成员
18InBlock.gif
19InBlock.gif        public string UserName
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            get
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                return _name;
24ExpandedSubBlockEnd.gif            }

25InBlock.gif            set
26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
27InBlock.gif                _name = value;
28ExpandedSubBlockEnd.gif            }

29ExpandedSubBlockEnd.gif        }

30InBlock.gif
31ExpandedSubBlockEnd.gif        #endregion

32ExpandedSubBlockEnd.gif    }

33ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 工厂方法
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Factory
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class AcountFactory
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public AcountFactory()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9ExpandedSubBlockEnd.gif        }

10InBlock.gif        public static bool IsAfternoon()
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            DateTime d = DateTime.Now;
13InBlock.gif            if(d.Hour > 12)
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return true;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif            else
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                return false;
20ExpandedSubBlockEnd.gif            }
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        public static IAcount CreateAcount()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            if(IsAfternoon())
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                return new AcountMyself("Myself");
27ExpandedSubBlockEnd.gif            }

28InBlock.gif            else
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                return new AcountYourself("Yourself");
31ExpandedSubBlockEnd.gif            }

32ExpandedSubBlockEnd.gif        }

33ExpandedSubBlockEnd.gif    }

34ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
1None.gif            IAcount acount = AcountFactory.CreateAcount();
2None.gif            Console.WriteLine(acount.UserName);
3None.gif            Console.ReadLine();
双类层次结构:
1.Machine类层次结构;定义不同的类型的机器。
ContractedBlock.gif ExpandedBlockStart.gif 基类Machine
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Plan;
 3None.gif
 4None.gifnamespace Gof.Test.Factory.Machines
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class Machine
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        public Machine()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        public bool HasDone
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            get
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return _hasDone;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif            set
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                _hasDone = value;
20ExpandedSubBlockEnd.gif            }

21ExpandedSubBlockEnd.gif        }
private bool _hasDone;
22InBlock.gif
23InBlock.gif        public virtual MachinePlanner CreatePlanner()
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            return new BasicPlanner(this);
26ExpandedSubBlockEnd.gif        }

27ExpandedSubBlockEnd.gif    }

28ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Mixer
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Plan;
 3None.gif
 4None.gif
 5None.gifnamespace Gof.Test.Factory.Machines
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class Mixer : Machine
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public Mixer()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11ExpandedSubBlockEnd.gif        }

12InBlock.gif        public override MachinePlanner CreatePlanner()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            return new MixerPlanner(this);
15ExpandedSubBlockEnd.gif        }

16ExpandedSubBlockEnd.gif    }

17ExpandedBlockEnd.gif}
2.计划类层次结构,定义机器的执行计划。
ContractedBlock.gif ExpandedBlockStart.gif 抽象基类
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Machines;
 3None.gif
 4None.gifnamespace Gof.Test.Factory.Plan
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public abstract class MachinePlanner
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        public MachinePlanner(Machine machine)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            _machine = machine;
11ExpandedSubBlockEnd.gif        }

12InBlock.gif        public Machine Machine
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            get
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                return _machine;
17ExpandedSubBlockEnd.gif            }

18ExpandedSubBlockEnd.gif        }

19InBlock.gif        private Machine _machine;
20InBlock.gif        public abstract bool ContineWork
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif           get;
23ExpandedSubBlockEnd.gif        }

24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 基本计划类1
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Machines;
 3None.gif
 4None.gifnamespace Gof.Test.Factory.Plan
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class BasicPlanner:MachinePlanner
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        public BasicPlanner(Machine machine) : base(machine)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        public override bool ContineWork
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            get
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return Machine.HasDone;
16ExpandedSubBlockEnd.gif            }

17ExpandedSubBlockEnd.gif        }

18ExpandedSubBlockEnd.gif    }

19ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 基本计划类2
 1None.gifusing System;
 2None.gifusing Gof.Test.Factory.Machines;
 3None.gif
 4None.gifnamespace Gof.Test.Factory.Plan
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6InBlock.gif    public class MixerPlanner : MachinePlanner
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        public MixerPlanner(Machine machine) : base(machine)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10ExpandedSubBlockEnd.gif        }
11InBlock.gif        public override bool ContineWork
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            get
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                return Machine.HasDone;
16ExpandedSubBlockEnd.gif            }
17ExpandedSubBlockEnd.gif        }
18InBlock.gif
19ExpandedSubBlockEnd.gif    }
20ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
1None.gif            Gof.Test.Factory.Machines.Mixer mixture1 = new Gof.Test.Factory.Machines.Mixer();
2None.gif            mixture1.HasDone = true;
3None.gif            Gof.Test.Factory.Plan.MachinePlanner mixerplanner = mixture1.CreatePlanner();
4None.gif            if(mixerplanner.ContineWork)
5ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
6InBlock.gif                Console.WriteLine("Mixer can work again");
7ExpandedBlockEnd.gif            }
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a defer instantiation to subclasses.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值