设计模式学习笔记
代码书写注意事项
1 命名规范
2 根据语法知识选择适当的代码表示方式,使得程序性能提升;
3 注意容错性的处理和友好的提示;
4 使用面向对象的方式编写代码 使代码可维护----要改只改某一个对象 可复用 可扩展 ---- 直接增加对象 灵活性好的代码
5 大量的复制 粘贴代码会给后期维护带来很大的麻烦;编写代码的时候应当尽可能的避免重复的代码;
6 编写代码的首先的学会分离比如业务和显示界面的分离,然后再对业务里面的扩展业务进行分离根据类的特性进行封装;
当业务和界面分离好啦以后业务对业务进行单独分析,首先对业务进行扩展扩展出来的新的业务和原来的业务进行对比是否具有相同的特性如果没有则需要建新的对象对新的业务进行封装
这里炒菜为例进行说明
一般做菜可分为如下几步
1 买菜 2 洗菜 3 切菜 4 炒菜
那么我们对这个业务进行封装的时候就得考虑该业务的共同点
他们都是涉及到菜
这里我们可以建一个接口
View Code
1 public class Operation
2 {
3 private string _name;
4
5 public string Name
6 {
7 get{return _name;}
8 set {_name = value;}
9 }
10
11 public virtual string Dish()
12 }
public class Operationbuy:Operation
{
public override void Dish()
{Console.writeLine("买菜");}
}
public class Operationchaoc:Operation
{
public override void Dish()
{Console.writeLine("炒菜菜");}
}
但是如果我现在想要增加一个想要做泡菜那么久直接增加一个子类就可以啦!!
记住当把业务和显示分开以后需要对业务进行扩展在根据扩展后的业务知识根据类的几步特性对业务进行松耦合处理;
简单工厂模式
说白啦就是根据你的需要提供不同的实例对象
实例代码如下
public class Operation
{
private double _numberA = 0;
{
private double _numberA = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
{
get { return _numberA; }
set { _numberA = value; }
}
private double _numberB = 0;
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
{
get { return _numberB; }
set { _numberB = value; }
}
public virtual double GetResult()
{
double result = 0;
return result;
}
}
{
double result = 0;
return result;
}
}
public class OperationMul:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
public class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return base.GetResult();
}
}
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return base.GetResult();
}
}
public class OperationAdd : Operation
{
public override double GetResult()
{
double resulet = 0;
resulet = NumberA + NumberB;
return resulet;
}
}
{
public override double GetResult()
{
double resulet = 0;
resulet = NumberA + NumberB;
return resulet;
}
}
public class OperationFactor
{
public static Operation CreatOperation(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
}
return oper;
}
}
{
public static Operation CreatOperation(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
}
return oper;
}
}
class Program
{
static void Main(string[] args)
{
Operation oper = null;
oper = OperationFactor.CreatOperation("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
Console.WriteLine("结果是{0}", result);
Console.Read();
}
}
{
static void Main(string[] args)
{
Operation oper = null;
oper = OperationFactor.CreatOperation("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
Console.WriteLine("结果是{0}", result);
Console.Read();
}
}