ABSTRACT FACTORY (Object Creational)
Purpose
Provide an interface that delegates creation calls to one or
more concrete classes in order to deliver specific objects.
Use When
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.
Example
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();
- }
- }
分享到:
相关推荐
java设计模式之抽象工厂模式实例,希望对大家有所帮助。
JAVA设计模式之抽象工厂模式,一个不错的列子,逻辑比好清晰,让你更好的理解抽象工厂
Java设计模式之抽象工厂模式
Java设计模式之抽象工厂模式.pdf
抽象工厂模式 JAVA设计模式
一个具体工厂创建一个产品族,一个产品族是不同系列产品的组合,产品的创建的逻辑分在在每个具体工厂类...如果没有应对“多系列对象创建”的需求变化,则没有必要使用抽象工厂模式,这时候使用简单的静态工厂完全可以。
主要介绍了Java设计模式之抽象工厂模式,结合实例形式分析了抽象工厂模式的概念、功能、定义与使用方法,需要的朋友可以参考下
Java设计模式,抽象工厂的Demo,具体的思想与实现有很多讲得很好的老师,我是看厉风行老师的视频学的,讲得不错,简单易懂
java,设计模式中的一种,抽象工厂设计模式。
NULL 博文链接:https://f303153041.iteye.com/blog/1679092
java常用设计模式-抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它通过定义一个创建对象的接口来创建对象,但将具体实现的决定留给子类来决定。在抽象工厂模式中,接口是负责创建...
Java实现的工厂模式,抽象工厂,工厂方法模式
Java23种设计模式06抽象工厂模式.pdf
JAVA设计模式研究之抽象工厂模式,工厂设计模式
设计模式-抽象工厂模式javademo java小程序