0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

abstract Factory Pattern

Last updated at Posted at 2020-11-11

userはabstractクラスやinterfaceだけを使い目的物を得る
userはFactory Patternを利用ことで、目的物の組成に必要な各種オブジェクトや、その組み合わせを意識する必要がない

以下ではabstract Factory Patternのサンプルコードを記述します

Design Pattarm MENU

以下のクラス構成とします

クラス 説明
abstract
factory.class
abstract createメソッドを定義
samFactoryA.class factoryを拡張する
目的物を組成するに必要なパーツクラスを生成し組み合わせる
samFactoryB.class 同上
interface face1
interface face2
パーツ用インターフェイス
parts1.class ~ parts4.class 目的物の組成に必要なパーツクラス
user(Main.class) 動作を確認
*user 他の開発者がこのパターンを利用する、という意味合いを含みます

下記のサンプルコードでは2つのFactoryを使っています
abstract Factory Patternは容易に複数のFactoryを作れるため
userの利便性が上がる特徴があります

factory.class
abstract class factory{
  String msg;
  int    len;

  factory(String msg){
    this.msg = msg;
    len =msg.length();
  }

  abstract String create();
}
samFactoryA.class
class samFactoryA extends factory{
  samFactoryA(String msg){super(msg);}

  String create(){
    String partsA = new partsA().s;
    String partsB = new partsB().s;
    return partsA+super.msg+partsB;
}}
samFactoryB.class
class samFactoryB extends factory{
  samFactoryB(String msg){super(msg);}
  
  String create(){
    String partsA = new partsA().s;
    String partsB = new partsB().s;
    String partsC = new partsC().s;
    String partsD = new partsD().s;
    return partsA+partsC + super.msg + partsD+partsB;
}}
interface_face1
interface face1{}
class partsA implements face1{String s="<<< ";}
class partsB implements face1{String s=" >>>";}

interface face2{}
class partsC implements face2{String s="[[[ ";}
class partsD implements face2{String s=" ]]]";}
user(Main.class)
public static void main(String[] args){
  factory f1 = new samFactoryA("Hello java");
    System.out.println(f1.create());
  
  factory f2 = new samFactoryB("HELLO JAVA");
    System.out.println(f2.create());
}}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?