没有合适的资源?快使用搜索试试~ 我知道了~
首页Creating a Class Factory with C# and .NET
资源详情
资源评论
资源推荐
Introduction
Developing dynamic, flexible, and maintainable applications these days is a
necessary, yet difficult task. Systems grow at exponential rates as companies
require more and more advanced logic to run their businesses. The ability of a
programmer to understand and maintain such systems diminishes as a systems
complexity increases. The creative programmer can come up with ways to combat
the difficulties in maintaining a complex system, but often those ways are
ignored when new functionality must be developed in a very short time (which, as
any programmer who has worked in the corporate world will know, is always the
case....time doesn't exist, only the deadline).
There are many ways to make systems extensible. The best way is to take the time
up front to design and plan, doing what you can to predict what additions to a
system may be necessary in the future, and plan for them. Quite often, systems
are designed with placeholders or hooks that in the future will hold other,
already known code. Since a deadline prevents that code from being developed
initially, it must be added later on. Since the required functionality is
already known, its fairly easy to plan for it and make the system ready for it.
Prior knowledge of what future functionality a system may require is not always
possible, though.
In the situations where you know the system will need to be extended, but what
extensions will be necessary are not known, the ability to plan for those
extensions is greatly reduced. You can't build in fixed placeholders or hooks
for the future functionality. If you do build in hooks, they must be generic,
capable of loading code without prior knowledge of its inner workings. This
article will outline one of the most useful tools for designing extensible
systems, regardless of whether the necessary extensions are already known or
not.
Design Patterns
In the last 15-20 years, many innovations and improvements in the way programs,
applications, and systems are developed. Old, monolithic, flat systems design
has fallen in the face of n-tier systems, object oriented programming,
inheritance and polymorphism, modular development, and design patterns. Greater
strides in project management have been made to make better use of these better
ways of thinking about development. This article focuses on the aspect of Design
Patterns, and in particular, a specific pattern called a Class Factory.
Design Patterns aren't as tangible a thing as a class or method. A design
pattern is more a way of thinking, about the code you intend to write and how
that code will be used in a system. Design patterns help fuel creative thinking
when planning all or part of a larger system, and can help save time by
providing something of a template to begin working from. There are many design
patterns that are well defined and frequently used today. The Facade pattern
provides a template that allows one system to communicate with another without
requiring changes in either. Similarly, the Adapter pattern provides a template
that allows one class to talk with one or more classes of different design and
communications methods. The Singleton pattern makes it possible to use a single
instance of a class anywhere in a system. And the Abstract Factory, similar to
the topic of this article, allows the creation and use of classes without
knowing all the details of those classes, and without needing to directly
instantiated those classes.
The Abstract Factory: Some History
This article will focus on the creation of a Class Factory, which is similar in
concept to the Abstract Factory. The definition of an abstract factory is as
follows:
"Provide an interface for creating families of related or dependent objects
without specifying their concrete classes."
The development of an abstract factory requires a fair amount of forethought and
planning to develop the necessary infrastructure. Numerous classes, both
abstract and concrete, are required to allow a client to create and use classes
it does not have direct knowledge of. Before providing you with in-depth details
of how an abstract factory works, take a moment to examine the following
diagram. This diagram outlines the structure and interactions in an abstract
class factory system:
The theory behind the Abstract Factory is simply "implicit use", as opposed to
explicit use. A class that is marked as abstract is non-instantiable, an
implicit representation of all the classes that derive from it. When one of
those derived classes is instantiated with new, the class is being "explicitly
used". With an abstract factory, the client can implicitly use any classes
derived from the abstract products that are producible with the abstract
factory. The client does not have explicit knowledge of either ProductA1 or
ProductB1, and any additional functionality in those classes not defined in
AbstractProductA or AbstractProductB will not be available to the client.
MethodA and MethodB can be called, but MethodX and MethodY are unknown to the
client. The client also never directly uses ConcreteFactory1 or
ConcreteFactory2, rather using an abstract instance of their parent class,
AbstractFactory.
// Abstract Factory
public abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA();
public abstract AbstractProductB CreateProductB();
}
// Contrete Factory #1
public class ConcreteFactory1: AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA1();
}
public override AbstractProdictB CreateProductB()
{
return new ProductB1();
}
}
// Concrete Factory #2
public class ConcreteFactory2: AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA2();
}
public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}
// Abstract product A
public abstract class AbstractProductA
{
public abstract void MethodA();
}
// Abstract product B
public abstract class AbstractProductB
{
public abstract void MethodB();
}
// Product A1
public class ProductA1: AbstractProductA
{
public override void MethodA()
{
Console.WriteLine("ProductA1.MethodA() Called");
}
}
// Product A2
public class ProductA2: AbstractProductA
{
public override void MethodA()
{
Console.WriteLine("ProductA2.MethodA() Called");
}
}
// Product B1
public class ProductB1: AbstractProductB
{
public override void MethodB()
{
Console.WriteLine("ProductB1.MethodB() Called");
}
}
// Product B2
public class ProductB2: AbstractProductB
{
public override void MethodB()
{
Console.WriteLine("ProductB2.MethodB() Called");
}
}
// Client class, consumer of products and
// user of Abstract Factory
public class Client
{
public static void Main()
{
AbstractFactory factory;
AbstractProductA prodA;
AbstractProductB prodB;
// Create instances of products from factory A
factory = new ConcreteFactoryA();
prodA = factory.CreateProductA();
prodB = factory.CreateProductB();
prodA.MethodA();
prodB.MethodB();
// Create instances of products from factory B
factory = new ConcreteFactoryB();
prodA = factory.CreateProductA();
prodB = factory.CreateProductB();
prodA.MethodA();
prodB.MethodB();
}
}
The Class Factory: A .NET Improvement
While the Abstract Factory provides a very powerful framework for implicitly
instantiating classes, its design is both arbitrary and rigid. The arbitrary
nature of Abstract Factory allows the pattern to be used in any object oriented
language, and its rigidity is a natural byproduct of that portability. In this
article, I'll introduce the Class Factory, a design pattern based on the
Abstract Factory, but tuned more to the .NET way of design and thinking. (NOTE:
If any other patterns exist with the name "Class Factory" they are likely
different than this pattern. This pattern is derived from a task I have had to
go several times, and appropriately, I developed a pattern for that task.) The
Class Factory will use many core .NET features such as reflection, interfaces,
dynamic class creation, etc.
The goals of the Class Factory pattern are similar but also different than those
of the Abstract Factory pattern. While the Abstract Factory aims to provide a
way to instantiated objects implicitly from a strict class higherarchy, the
Class Factory aims to provide a way to instantiated objects dynamically from an
arbitrary class pool. Rather than requiring a specific root abstract class or
classes, like the Abstract Factory, the Class Factory requires the
implementation of one or more interfaces, using interface instances to allow
access to each object, rather than an entire class instance. Another difference,
and possibly an advantage, of the Class Factory is it requires no prior
knowledge of the product classes whatsoever, relying on keys to instantiated
剩余13页未读,继续阅读
javatemptation
- 粉丝: 65
- 资源: 324
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0