初学设计模式
来源:互联网 发布:金江软件 编辑:程序博客网 时间:2024/05/07 23:35
在论坛上有个帖子说一道面试题,有30多应徵者没能答对,其题目主要为:写一个桌子类,要求有一个方法求出体积。问题上去很简单,但后来才最后知道那么多人没做对的原因是因为需要用到封装、继续、多态,出题的是有点脑残,真是脱裤子放屁,呵呵。
以下是第一次写简单工厂模式,不过严格来说,这还不是设计模式
using System;
using System.Collections.Generic;
using System.Text;
namespace Tabel
{
class Program
{
static void
{
Tabel tabel;
tabel = Factory.CreateTable("double");
Console.WriteLine(tabel.GetVolume());
tabel = Factory.CreateTable("none");
Console.WriteLine(tabel.GetVolume());
Console.ReadLine();
}
}
///
/// 桌子
///
class Tabel
{
private double length = 0;
private double width = 0;
private double height = 0;
private double result = 0;
public double Result
{
get
{
return result;
}
set
{
result = value;
}
}
public double Length
{
get
{
return length;
}
set
{
length = value;
}
}
public double Width
{
get
{
return width;
}
set
{
width = value;
}
}
public double Height
{
get
{
return height;
}
set
{
height = value;
}
}
public virtual double GetVolume()
{
return Length * Width * Height;
}
}
///
/// 2张桌子
///
class DoubleTable : Tabel
{
public override double GetVolume()
{
return Length * 2 * Width * Height;
}
}
class Factory
{
public static Tabel CreateTable(string result)
{
Tabel table = null;
if (result == "double")
{
table = new DoubleTable();
table.Length = 10;
table.Width = 10;
table.Height = 10;
return table;
}
else
{
table = new Tabel();
table.Length = 10;
table.Width = 10;
table.Height = 10;
return table;
}
}
}
}
- 初学设计模式
- 初学设计模式
- 设计模式初学结束
- 【初学设计模式】序
- 初学设计模式
- 初学设计模式
- 初学设计模式
- 设计模式初学
- 设计模式初学心得(序)
- 【初学设计模式】Builder (生成器)
- 【初学设计模式】Prototype (原型)
- 【初学设计模式】Bridge (桥接)
- 【初学设计模式】Composite (组合)
- 【初学设计模式】Decorator (装饰)
- 【初学设计模式】Facade (外观)
- 【初学设计模式】Proxy (代理)
- 初学JSP MVC设计模式
- 初学设计模式二(策略模式)
- 赚钱新思维赚“第三方”
- regular expression multiple line match
- 怎么对待脾气暴躁爱骂人的女人?
- 那天,我倒霉催的手按错了手机的键……
- 从开始
- 初学设计模式
- 分页的简单讨论
- 函数递归调用
- 求职中不能不知的防骗技巧
- 非窗口类中使用定时器的方法
- 金领职业
- 美国互联网:最后只剩Google与亚马逊
- SimpleFormController 工作流
- TCPMP源代码编译