[color=red]ABSTRACT FACTORY[/color] (Object Creational)
[color=red]Purpose[/color]
Provide an interface that delegates creation calls to one or
more concrete classes in order to deliver specific objects.
[color=red]Use When[/color]
n The creation of objects should be independent of the system
utilizing them.
1 Systems should be capable of using multiple families of objects.
2 Families of objects must be used together.
3 Libraries must be published without exposing implementation
details.
4 Concrete classes should be decoupled from clients.
[color=red]Example[/color]
Email editors will allow for editing in multiple formats including
plain text, rich text, and HTML. Depending on the format being
used, different objects will need to be created. If the message
is plain text then there could be a body object that represented
just plain text and an attachment object that simply encrypted
the attachment into Base64. If the message is HTML then the
body object would represent HTML encoded text and the
attachment object would allow for inline representation and a
standard attachment. By utilizing an abstract factory for creation
we can then ensure that the appropriate object sets are created
based upon the style of email that is being sent.
[color=red]Purpose[/color]
Provide an interface that delegates creation calls to one or
more concrete classes in order to deliver specific objects.
[color=red]Use When[/color]
n The creation of objects should be independent of the system
utilizing them.
1 Systems should be capable of using multiple families of objects.
2 Families of objects must be used together.
3 Libraries must be published without exposing implementation
details.
4 Concrete classes should be decoupled from clients.
[color=red]Example[/color]
Email editors will allow for editing in multiple formats including
plain text, rich text, and HTML. Depending on the format being
used, different objects will need to be created. If the message
is plain text then there could be a body object that represented
just plain text and an attachment object that simply encrypted
the attachment into Base64. If the message is HTML then the
body object would represent HTML encoded text and the
attachment object would allow for inline representation and a
standard attachment. By utilizing an abstract factory for creation
we can then ensure that the appropriate object sets are created
based upon the style of email that is being sent.
package javaPattern.abstractFactory;
public interface AbstractFactory {
public AbstractProduct createProductA();
public AbstractProduct createProductB();
}
interface AbstractProduct {
public void someMethod();
}
class ConcreteProductA implements AbstractProduct{
public void someMethod(){
System.out.println("具体的商品A");
}
}
class ConcreteProductB implements AbstractProduct{
public void someMethod(){
System.out.println("具体的商品B");
}
}
class ConcreteFactory implements AbstractFactory{
@Override
public AbstractProduct createProductA() {
return new ConcreteProductA();
}
@Override
public AbstractProduct createProductB() {
return new ConcreteProductB();
}
public static void main(String[] args) {
AbstractFactory abstractFactory = new ConcreteFactory();
AbstractProduct abstractProduct = abstractFactory.createProductA();
abstractProduct.someMethod();
}
}
抽象工厂模式详解
本文介绍抽象工厂模式的目的和使用场景,通过电子邮件编辑器的例子展示如何根据不同格式创建相应对象集。该模式确保系统的对象创建独立于具体实现,并允许使用多个对象家族。
1696

被折叠的 条评论
为什么被折叠?



