// Abstract Factory
// Intent: "Provide an interface for creating families of related or
// dependent objects without specifying their concrete classes".
// For further information, read "Design Patterns", p87, Gamma et al.,
// Addison-Wesley, ISBN:0-201-63361-2
/* Notes:
* When the construction needed involves many objects, possible organised
* in multi-faceted arrangements, the entire construction can be delegated
* to an abstract factory. This exposes standardised creation functionality
* which can be customised in concrete implementation to suit your specific
* needs, and avoid embedding this information in higher level code - it
* just needs to know how to call the abstract factory.
*
* In this sample, we have a framework with three abstract operating classes,
* called DPDocument, DPWorkspace and DPView and one abstract construction
* class, called DPFactory. An application-level class, called DPApplication
* is responsible for construction.
*
* We have a series of application-level operating classes derived from this
* framework - MyDocument, MyWorkspace and MyView. For design reasons we
* assume we wish to instantiate these from inside DPApplication. As there
* are multiple objects needed and they could be arranged in different
* lattices, we use a factory, MyFactory (in our example, there are all
* simple siblings), which is called inside DPApplication.
*
*/
namespace AbstractFactory_DesignPattern
{
using System;
// These classes could be part of a framework,
// which we will call DP
// ===========================================
abstract class DPDocument
{
abstract public void Dump();
}
abstract class DPWorkspace
{
abstract public void Dump();
}
abstract class DPView
{
abstract public void Dump();
}
abstract class DPFactory
{
abstract public DPDocument CreateDocument();
abstract public DPView CreateView();
abstract public DPWorkspace CreateWorkspace();
}
abstract class DPApplication
{
protected DPDocument doc;
protected DPWorkspace workspace;
protected DPView view;
public void ConstructObjects(DPFactory factory)
{
// Create objects as needed
doc = factory.CreateDocument();
workspace = factory.CreateWorkspace();
view = factory.CreateView();
}
abstract public void Dump();
public void DumpState()
{
if (doc != null) doc.Dump();
if (workspace != null) workspace.Dump();
if (view != null) view.Dump();
}
}
// These classes could be part of an application
class MyApplication : DPApplication
{
MyFactory myFactory = new MyFactory();
override public void Dump()
{
Console.WriteLine("MyApplication exists");
}
public void CreateFamily()
{
MyFactory myFactory = new MyFactory();
ConstructObjects(myFactory);
}
}
class MyDocument : DPDocument
{
public MyDocument()
{
Console.WriteLine("in MyDocument constructor");
}
override public void Dump()
{
Console.WriteLine("MyDocument exists");
}
}
class MyWorkspace : DPWorkspace
{
override public void Dump()
{
Console.WriteLine("MyWorkspace exists");
}
}
class MyView : DPView
{
override public void Dump()
{
Console.WriteLine("MyView exists");
}
}
class MyFactory : DPFactory
{
override public DPDocument CreateDocument()
{
return new MyDocument();
}
override public DPWorkspace CreateWorkspace()
{
return new MyWorkspace();
}
override public DPView CreateView()
{
return new MyView();
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
MyApplication myApplication = new MyApplication();
myApplication.CreateFamily();
myApplication.DumpState();
return 0;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
C#源代码23个设计程序
共23个文件
txt:23个
5星 · 超过95%的资源 需积分: 9 28 下载量 150 浏览量
2014-09-08
11:42:29
上传
评论
收藏 26KB RAR 举报
温馨提示
初学的小伙伴们,熟练掌握23个设计程序,你就是.net的牛人。相信自己,你能。
资源推荐
资源详情
资源评论
收起资源包目录
C#源代码23个设计程序.rar (23个子文件)
C#源代码23个设计程序
command_designpattern.cs.txt 2KB
singleton_designpattern.cs.txt 2KB
templatemethod_designpattern.cs.txt 2KB
composite_designpattern.cs.txt 3KB
factorymethod_designpattern.cs.txt 2KB
decorator_designpattern.cs.txt 2KB
chainofresponsibility_designpattern.cs.txt 4KB
iterator_designpattern.cs.txt 3KB
adapter_designpattern.cs.txt 3KB
state_designpattern.cs.txt 3KB
strategy_designpattern.cs.txt 1KB
visitor_designpattern.cs.txt 2KB
builder_designpattern.cs.txt 2KB
abstractfactory_designpattern.cs.txt 4KB
flyweight_designpattern.cs.txt 2KB
observer_designpattern.cs.txt 2KB
memento_designpattern.cs.txt 3KB
mediator_designpattern.cs.txt 2KB
interpreter_designpattern.cs.txt 2KB
proxy_designpattern.cs.txt 2KB
facade_designpattern.cs.txt 2KB
bridge_designpattern.cs.txt 3KB
prototype_designpattern.cs.txt 2KB
共 23 条
- 1
资源评论
- qq_200290332014-10-11下载下来 是一些文档 有时间好好研究一下 很好的资源
qiancaitop
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功