Abstract Factoryパターン:(04)に記載のJavaデザインパターン

この記事の出所:GitHubにはこちらをクリック・ || GitEE・こちらをクリック

まず、Abstract Factoryパターン

1、生活のシーン

自動車生産、赤い車を選択し、別の工場の生産を指定し、ユーザーが選択した車のタイプに基づいて、その後の中国の工場を使用し、アウディの車を選択し、ドイツの工場を使用する必要があります。

2、Abstract Factoryパターン

1)Abstract Factoryパターン:特定のカテゴリを示すことなく、オブジェクトまたは相互依存関連オブジェクトを作成するための定義されたインタフェースと、
2)抽象工場モデルは、単純な工場モードと積分モードファクトリメソッドとすることができる;
3)設計レベルから抽象工場モデルは(さらにまたは抽象と呼ばれる)簡単な変更工場パターンで、参照。
4)は、2つの層に抽象化植物、AbstractFactory(抽象工場)及び植物サブクラス特定の実装では、拡張プログラムを容易にします。

3、図コードUML

Abstract Factoryパターン:(04)に記載のJavaデザインパターン

図4に示すように、ソースコードを達成するために

/**
 * 抽象工厂模式
 */
public class C01_AbstractFactory {
    public static void main(String[] args) {
        CarProductFactory factory = new ChinaCarFactory() ;
        factory.getCar("hq") ;
        factory = new GermanyCarFactory () ;
        factory.getCar("ad") ;
    }
}

// 汽车生产抽象工厂
interface CarProductFactory {
    CarProduct getCar (String type) ;
}
// 中国汽车工厂
class ChinaCarFactory implements CarProductFactory {
    @Override
    public CarProduct getCar(String type) {
        CarProduct product = null ;
        if ("hq".equals(type)){
            product = new HQCar() ;
            product.name="红旗一号" ;
            product.date="1999-09-19" ;
            product.material();
            product.origin();
        } else if ("df".equals(type)){
            product = new DFCar() ;
            product.name="东风一号" ;
            product.date="2019-09-19" ;
            product.material();
            product.origin();
        }
        return product ;
    }
}
// 德国汽车工厂
class GermanyCarFactory implements CarProductFactory {
    @Override
    public CarProduct getCar(String type) {
        CarProduct product = null ;
        if ("ad".equals(type)){
            product = new ADCar() ;
            product.name="奥迪A8" ;
            product.date="2017-09-19" ;
            product.material();
            product.origin();
        } else if ("bm".equals(type)){
            product = new BMCar() ;
            product.name="宝马X8" ;
            product.date="2018-09-19" ;
            product.material();
            product.origin();
        }
        return product ;
    }
}
// 汽车生产抽象类
abstract class CarProduct {
    /**
     * 汽车名称
     */
    protected String name ;
    /**
     * 生产日期
     */
    protected String date ;
    /**
     * 材料
     */
    abstract void material () ;
    /**
     * 产地
     */
    abstract void origin () ;
}
// 红旗车
class HQCar extends CarProduct {
    @Override
    void material() {
        System.out.println(super.name+"材料...");
    }
    @Override
    void origin() {
        System.out.println(super.date+":"+super.name+"在中国北京生产");
    }
}
// 东风车
class DFCar extends CarProduct {
    @Override
    void material() {
        System.out.println(super.name+"材料...");
    }
    @Override
    void origin() {
        System.out.println(super.date+":"+super.name+"在中国南京生产");
    }
}
// 奥迪车
class ADCar extends CarProduct {
    @Override
    void material() {
        System.out.println(super.name+"材料...");
    }
    @Override
    void origin() {
        System.out.println(super.date+":"+super.name+"在德国柏林生产");
    }
}
// 宝马车
class BMCar extends CarProduct {
    @Override
    void material() {
        System.out.println(super.name+"材料...");
    }
    @Override
    void origin() {
        System.out.println(super.date+":"+super.name+"在德国慕尼黑生产");
    }
}

二、春Frameworkアプリケーション

1、シーン記述

設定ファイルBeanのSpringフレームワークを取得するためのさまざまな方法で。

図2に示すように、コア構成

<bean id="carBean" class="com.model.design.spring.node04.abstractFactory.CarBean">
    <property name="name" value="中国红旗" />
</bean>
<bean id="carBean1" class="com.model.design.spring.node04.abstractFactory.CarBean">
    <property name="name" value="德国奥迪" />
</bean>

3、テストファイル

ここでは、取得を使用するには、2つの方法があります。

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations = {"classpath:/spring/spring-abstract-factory.xml"})
public class SpringTest {

    @Resource
    private BeanFactory beanFactory ;

    @Test
    public void test01 (){
        CarBean carBean = (CarBean)beanFactory.getBean("carBean") ;
        System.out.println(carBean.getName());
    }

    @Test
    public void test02 (){
        ApplicationContext context01 = new ClassPathXmlApplicationContext(
                "/spring/spring-abstract-factory.xml");
        CarBean carBean = (CarBean)context01.getBean("carBean1") ;
        System.out.println(carBean.getName());
    }
}

図4に示すように、構造解析

Abstract Factoryパターン:(04)に記載のJavaデザインパターン

Abstract Factoryパターン:(04)に記載のJavaデザインパターン

抽象ファクトリオブジェクトは、パッケージを作成します。春には、たBeanFactoryを実装すること。豆容器は、春の多様から得ることができます。豆の構成、getBean方法は、異なるタイプのオブジェクト(シングルトンスコープ)を返すか、または新しいオブジェクト(プロトタイプスコープ)を初期化することができます。その上ClassPathXmlApplicationContext、XmlWebApplicationContextと:たBeanFactoryの実装では、我々は区別することができます。

第三に、工場出荷時のパターンの概要

ファクトリパターン(単純な工場パターン、ファクトリメソッドパターン、抽象工場モード)、オブジェクトをインスタンス化することを意図コア工場モデルコード3種類のファクトリクラス一元管理とメンテナンス、完全デカップリングコードの依存関係に、カプセル化します。これにより、プログラムの拡張性と保守性を向上させることができます。

第四に、送信元アドレス

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Abstract Factoryパターン:(04)に記載のJavaデザインパターン

おすすめ

転載: blog.51cto.com/14439672/2436939