楼主: Lisrelchen
1738 18

C# Design Patterns: A Tutorial [推广有奖]

11
Lisrelchen 发表于 2016-5-22 02:40:35

Class Containment

  1. private void lsSwimmers_SelectedIndexChanged(
  2.              object sender, System.EventArgs e) {
  3.   //get index of selected swimmer
  4.        int i = lsSwimmers.SelectedIndex ;
  5.        //get that swimmer
  6.        Swimmer swm = (Swimmer)ar[i];
  7.        //display her time
  8.        txTime.Text =swm.getTime ();
  9. }
复制代码

12
Lisrelchen 发表于 2016-5-22 02:43:32

Classes and Properties

  1. public class Swimmer
  2. {
  3.        private string frName, lName;
  4.        private string club;
  5.        private int Age;
  6.        private int place;
  7.        private FormatTime tms;
  8. //-----------
  9. public Swimmer(String dataLine)          {
  10.        StringTokenizer tok = new StringTokenizer (dataLine);
  11.        place =  Convert.ToInt32 (tok.nextElement());
  12.        frName = tok.nextElement ();
  13.        lName = tok.nextElement ();
  14.        string s = tok.nextElement ();
  15.        Age = Convert.ToInt32 (s);
  16.        club = tok.nextElement ();
  17.        tms = new FormatTime (tok.nextElement ());
  18. }
  19. //-----------
  20. public string name {
  21.        get{
  22.        return frName+" "+lName;
  23.        }
  24. }
  25. //-----------
  26. public string time {
  27.        get{
  28.           return tms.getTime();
  29.        }
  30.        set  {
  31.           tms = new FormatTime (value);
  32.        }
  33. }
  34. //-------------------
  35. //age property
  36. public int age {
  37.        get {
  38.           return Age;
  39.        set {
  40.           Age = value;
  41.        }
  42. }
  43. }
复制代码

13
Lisrelchen 发表于 2016-5-22 02:51:03

Factory Patterns in Math Computation

Factory Patterns in Math Computation
  1. public class TrigButterfly:Butterfly  {
  2.       float y, oldr1, oldi1;
  3.       float cosy, siny;
  4.       float r2cosy, r2siny, i2cosy, i2siny;

  5.       public TrigButterfly(float angle) {
  6.              y = angle;
  7.              cosy = (float) Math.Cos(y);
  8.              siny = (float)Math.Sin(y);
  9.       }
  10.       public override void Execute(Complex xi, Complex xj) {
  11.              oldr1 = xi.getReal();
  12.              oldi1 = xi.getImag();
  13.              r2cosy = xj.getReal() * cosy;
  14.              r2siny = xj.getReal() * siny;
  15.              i2cosy = xj.getImag()*cosy;
  16.              i2siny = xj.getImag()*siny;
  17.              xi.setReal(oldr1 + r2cosy +i2siny);
  18.              xi.setImag(oldi1 - r2siny +i2cosy);
  19.              xj.setReal(oldr1 - r2cosy - i2siny);
  20.              xj.setImag(oldi1 + r2siny - i2cosy);
  21.       }
  22. }
复制代码

14
Lisrelchen 发表于 2016-5-22 02:54:50
  1. StraightSeeding

  2. In actually writing this program, we’ll discover that most of the work is done in straight seeding. The changes for circle seeding are pretty minimal. So we instantiate our StraightSeeding class and copy in the Collection of swimmers and the number of lanes.

  3. protected override void seed() {
  4.       //loads the swmrs array and sorts it
  5.       sortUpwards();

  6.       int lastHeat = count % numLanes;
  7.       if (lastHeat < 3)
  8.              lastHeat = 3;   //last heat must have 3 or more
  9.       int lastLanes = count - lastHeat;
  10.       numHeats = count / numLanes;
  11.       if (lastLanes > 0)
  12.              numHeats++;
  13.       int heats = numHeats;
  14.       //place heat and lane in each swimmer's object
  15.       //Add in last partial heat
  16.       //copy from array back into ArrayList
  17.       //details on CDROM
  18. }
复制代码

15
Lisrelchen 发表于 2016-5-22 02:55:24
  1. Circle Seeding

  2. The CircleSeeding class is derived from StraightSeeding, so it starts by calling the parent class’s seed method and then rearranges the top heats.

  3. protected override void seed() {
  4.       int circle;
  5.       base.seed();        //do straight seed as default
  6.       if (numHeats >= 2 ) {
  7.              if (numHeats >= 3)
  8.                     circle = 3;
  9.              else
  10.                     circle = 2;
  11.              int i = 0;
  12.       for (int j = 0; j < numLanes; j++) {
  13.              for (int k = 0; k < circle; k++) {
  14.                     swmrs[i].setLane(lanes[j]);
  15.                     swmrs[i++].setHeat(numHeats - k);
  16.              }
  17.       }
  18. }
复制代码

16
Lisrelchen 发表于 2016-5-22 02:56:37
  1. Other Factories

  2. One issue that we have skipped over is how the program that reads in the swimmer data decides which kind of event to generate. We finesse this here by simply creating the correct type of event when we read in the data. This code is in our init method of our form.

  3. private void init() {
  4.       //create array of events
  5.       events = new ArrayList ();
  6.       lsEvents.Items.Add ("500 Free");
  7.       lsEvents.Items.Add ("100 Free");
  8.       //and read in their data
  9.       events.Add (new TimedFinalEvent ("500free.txt", 6));
  10.       events.Add (new PrelimEvent ("100free.txt", 6));
  11. }
复制代码

17
ReneeBK 发表于 2016-5-22 03:01:55
  1. A GardenMaker Factory

  2. Let’s consider a practical example where you might want to use the abstract factory in your application. Suppose you are writing a program to plan a garden design. This could include annual, vegetable, or perennial gardens. However, no matter which kind of garden you are planning, you will have the same questions.

  3. What are good border plants?

  4. What are good center plants?

  5. What plants do well in partial shade?

  6. (You would probably have a lot more plant questions, but we won’t get into them here.)

  7. We want a base C# Garden class that can answer these questions as class methods.

  8. public class Garden {
  9.       protected Plant center, shade, border;
  10.       protected bool showCenter, showShade, showBorder;
  11.       //select which ones to display
  12.       public void setCenter() {showCenter = true;}
  13.       public void setBorder() {showBorder =true;}
  14.       public void setShade() {showShade =true;}
  15.       //draw each plant
  16.       public void draw(Graphics g) {
  17.              if (showCenter) center.draw (g, 100, 100);
  18.              if (showShade) shade.draw (g, 10, 50);
  19.              if (showBorder) border.draw (g, 50, 150);
  20.       }
  21. }
复制代码

18
ReneeBK 发表于 2016-5-22 03:04:28
  1. Creating Singleton Using a Static Method

  2. The easiest way to make a class that can have only one instance is to embed a static variable inside the class, which we set on the first instance and check for each time we enter the constructor. A static variable is one for which there is only one instance, no matter how many instances there are of the class. To prevent instantiating the class more than once, we make the constructor private so an instance can only be created from within the static method of the class. Then we create a method called getSpooler that will return an instance of Spooler, or null if the class has already been instantiated.

  3. public class Spooler      {
  4.       private static bool instance_flag= false;
  5.       private Spooler()  {
  6.       }
  7.       public static Spooler getSpooler() {
  8.              if (! instance_flag)
  9.                     return new Spooler ();
  10.              else
  11.                     return null;
  12.       }
  13. }
复制代码

19
ReneeBK 发表于 2016-5-22 03:05:24
  1. Exceptions and Instances

  2. The preceding approach has the disadvantage that it requires the programmer to check the getSpooler method return to make sure it is not null. Assuming that programmers will always remember to check errors is the beginning of a slippery slope that many prefer to avoid.

  3. Instead, we can create a class that throws an Exception if you attempt to instantiate it more than once. This requires the programmer to take action and is thus a safer approach. Let’s create our own exception class for this case.

  4. public class SingletonException:Exception     {
  5.       //new exception type for singleton classes
  6.       public SingletonException(string s):base(s) {
  7.       }
  8. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 05:04