转自:http://www.aspiringcraftsman.com/2008/01/art-of-separation-of-concerns/
Introduction
In software engineering, Separation of Concerns refers to the delineation and correlation of software elements to achieve order within a system. Through proper separation of concerns, complexity becomes manageable.
The goal of this article is to promote the understanding of the principle of Separation of Concerns and to provide a set of foundational concepts to aid software engineers in the development of maintainable systems.
阅读全文 The Art of Separation of Concerns
一. 抽象工厂模式
抽象工厂:提供一个创建一系列相关或相互依赖对象的借口,而无需指定它们具体的类
Role
This pattern supports the creation of products that exist in families and are designed to be produced together. The abstract factory can be refined to concrete factories,each of which can create different products of different types and in different combinations. The pattern isolates the product definitions and their class names [...]
一、单例模式
The purpose of the Singleton pattern is to ensure that there is only one instance of a class, and that there is a global access point to that object. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object. Moreover, the [...]
一、工厂方法的职责
The Factory Method pattern is a way of creating objects, but letting subclasses decide exactly which class to instantiate. Various subclasses might implement the interface; the Factory Method instantiates the appropriate subclass based on information supplied by the client or extracted from the current state.
The design of this pattern enables the decision-making [...]
一、简单工厂(Simple Factory)模式
Simple Factory模式根据提供给它的数据,返回几个可能类中的一个类的实例。通常它返回的类都有一个公共的父类和公共的方法。
二、 Simple Factory模式角色与结构:
工厂类角色:工厂类在客户端的直接控制下(Create方法)创建产品对象。 抽象产品角色:定义简单工厂创建的对象的父类或它们共同拥有的接口。可以是一个类、抽象类或接口。 具体产品角色:定义工厂具体加工出的对象。
三、示例代码
最简单的工厂类:
public enum Category { A, B } public static class SimpleFactoryWithPara { public static IProduct Create(Category category) { switch (category) { case Category.A: return new ConcreteProductA(); case Category.B: return new ConcreteProductB(); default: throw new NotSupportedException(); } } }
测试代码:
[...]