Patterns in SOME –Abstract Factory
来源:互联网 发布:c语言中abs是什么意思 编辑:程序博客网 时间:2024/04/30 01:52
Code in C#:
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;
}
}
}
Code in SOME:
ADPDocument
a_Dump()
ADPWorkspace
a_Dump()
ADPView
a_Dump()
CMyDocument :ADPDocument
()
o_Dump()
CMyWorkspace :ADPWorkspace
o_Dump()
CMyView : ADPView
o_Dump()
ADPFactory
ADPDocument a_CreateDocument()
ADPWorkspace a_CreateWorkspace()
ADPView a_CreateView()
CMyFactory :ADPFactory
ADPDocument o_CreateDocument()
ADPWorkspace o_CreateWorkspace()
ADPView o_CreateView()
ADPApplication ->ADPDocument[m_doc],->ADPWorkspace[m_workspace],->ADPView[m_view] //referrence list, be deviden by comma
ConstructObjects(ADPFactory)
a_Dump()
DumpState()
CMyApplication :ADPApplication, ->CMyFactory[_myFactory.()] //ref list and inherit list, ".()" means it will be created in defination
o_Dump()
CreateFamily()
CClient
main
CClient.main
{
CMyApplication myApplication.();
myApplication.CreateFamily()
{
CMyFactory myFactory.();
ConstructObjects<ADPApplication>(myFactory) //if this method belongs to one of its ancestors, should use "<AncestorName>"
{
m_doc = myFactory.CreateDocument<CMyFactory>()
{
CMyDocument.();
};
m_workspace = myFactory.CreateWorkspace<CMyFactory>()
{
CMyWorkspace.();
};
m_view = myFactory.CreateView<CMyFactory>()
{
CMyView.();
};
};
};
myApplication.DumpState()
{
m_doc.Dump();
m_workspace.Dump();
m_view.Dump();
};
}
- Patterns in SOME –Abstract Factory
- Patterns in SOME –Factory Method
- Design Patterns in real life: Abstract Factory
- Patterns in SOME –State
- Patterns in SOME –Observer
- Patterns in SOME –Memento
- Patterns in SOME –Mediator
- Patterns in SOME –Iterator
- Patterns in SOME – Command
- Patterns in SOME –Interpreter
- Patterns in SOME –Proxy
- Patterns in SOME –Flyweight
- Patterns in SOME –Facade
- Patterns in SOME –Decorator
- Patterns in SOME –Composite
- Patterns in SOME –Bridge
- Patterns in SOME –Singleton
- Patterns in SOME –Prototype
- 网页特效
- Log4j日志管理系统简单使用说明
- dos和批处理命令
- 程序员诗词之————战斗!意志,伟大的胜利!
- 程序员诗词之——————虞美人 大会三君
- Patterns in SOME –Abstract Factory
- 程序员诗词之————行香子-六月一日逢雨
- 一种常见的需求变更:是/非模式 变成 0/1/.../N模式
- 程序员诗词之————蝶恋花-五月四日见老同学徐惠
- 西游记与系统设计
- Patterns in SOME –Factory Method
- 最新诺顿病毒库报暴风影音7.0有毒![原]
- 我喜欢看的恐怖片
- excel导出跨行问题