using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
class InterfaceTest
{
static void Main(
string[] args)
{
#region 测试Cat,Monkey,Bear类
introduction test;
for (
int i =
1; i<=
3; i++
)
{
switch (i)
{
case 1:
test =
new Cat();
test.speak(); //亲们,我的名字test有魅力么
break;
case 2:
test =
new Bear();
test.speak(); //亲们,我的名字test有魅力么
break;
case 3:
test =
new Monkey();
test.speak(); //亲们,我的名字test有魅力么
break;
}
}
Console.WriteLine();
#endregion
#region 好了,不用多态的方式去达到上边的效果,Thinking,该如何实现
for (
int i =
1; i <=
3; i++
)
{
switch (i)
{
case 1:
Cat cat=
new Cat();
cat.speak(); //亲们,我的名字好别扭o(>﹏<)o呃呃呃
break;
case 2:
Bear bear=
new Bear();
bear.speak(); //亲们,我的名字好别扭o(>﹏<)o呃呃呃
break;
case 3:
Monkey monkey=
new Monkey();
monkey.speak(); //亲们,我的名字好别扭o(>﹏<)o呃呃呃
break;
}
}
Console.WriteLine();
/* 好了,两种方式你发现了什么呢?神马,不会什么都没看出来么,想想,如果,有一天,我也一统天下,我的名字不再叫Cat,Bear和Monkey了,
那上面两种方式各需要做哪些改动呢?对于第二种我想说:天哪,我疯了,幸亏我只用了三次,用多了,这,哪改的完呢
*/
/* 重要:上面的方式之所以可以用接口去调用各个继承了它的类的方法,是因为,听着:父类引用指向子类对象!!!好了,你可以说我
Cat test=new Cat(),Bear test=new Bear(),亲,这,在一工程中,这实际么,可能,写着写着,自己都吐了,感受到接口的魅力了么,
感觉到面向对象爽之处了么,所以,要面向接口化编程!
其实,想想,第一种方式,如果有一天类名字改变了,要改的地也蛮多,这,这,工厂,你该出来了吧。。。
*/
#endregion
#region 利用工厂模式达到上面两个例子的效果
introduction test2;
for (
int i =
1; i <=
3; i++
)
{
switch (i)
{
case 1:
test2 = Factory.createCatInstance();
//和上边第一种和第二种对比下看看有什么区别
test2.speak();
break;
case 2:
test2 = Factory.createBearInstance();
//和上边第一种和第二种对比下看看有什么区别
test2.speak();
break;
case 3:
test2 = Factory.createMonkeyInstance();
//和上边第一种和第二种对比下看看有什么区别
test2.speak();
break;
}
}
Console.WriteLine();
/*想想,用了工厂模式有什么好处:以前,每次都自己去 new Cat()...,现在,工厂里写一个方法,让这个方法去 new Cat();
* 你想出来了么?
* 试着想一下,在不用工厂模式前(像第一和第二种实现方式),项目中有一百多处地方new Cat()(如Cat cat01=new Cat();Cat cat02=new Cat()),
* 当有一天Cat类要改名为BigCat,是不就得将这100多个地方都改了,天哪,崩溃吧,
* 但用了工厂模式后,由工厂这个类的某个方法去new这个对象,所以,我只用把工厂里负责new这个对象的方法改了就可以了,只一次就噢K
* 爽吧,这就是最简单的工厂模式,也是工厂模式的实质,别把它想的多难,那只是优化后的工厂模式而已,之所以用工厂模式,就是为了
* 达到软件设计中的“低耦合”
* 下次发三层架构了哦哦哦
*/
#endregion
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
/// <summary>
/// 假设所有的动物类都有一个自我介绍的方法,为其定义一个共同的接口(introduction:介绍)
/// </summary>
public interface introduction
{
void speak();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
/// <summary>
/// Cat类继承introduction接口
/// </summary>
public class Cat:introduction
{
#region introduction 成员
public void speak()
{
Console.WriteLine("我是机器猫,什么?不信,我给你变出好多吃的。。。");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
/// <summary>
/// Bear类继承introduction接口
/// </summary>
public class Bear:introduction
{
#region introduction 成员
public void speak()
{
Console.WriteLine("知道火焰是我最好的玩具的一定知道我吧,那只可爱的小熊熊就是我啦啦。。。");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
/// <summary>
/// Monkey类实现introduction接口
/// </summary>
public class Monkey:introduction
{
#region introduction 成员
public void speak()
{
Console.WriteLine("我是齐天大圣,记得吗,五百年前大闹天宫的就是俺老孙。。。");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interface
{
public static class Factory
{
/// <summary>
/// 返回Cat对象
/// </summary>
/// <returns></returns>
public static Cat createCatInstance()
{
return (
new Cat());
}
/// <summary>
/// 返回Bear对象
/// </summary>
/// <returns></returns>
public static Bear createBearInstance()
{
return (
new Bear());
}
/// <summary>
/// 返回Monkey对象
/// </summary>
/// <returns></returns>
public static Monkey createMonkeyInstance()
{
return (
new Monkey());
}
}
}
转载于:https://www.cnblogs.com/xiaoyu-well-being/p/3772683.html
相关资源:JAVA上百实例源码以及开源项目