'pattern'에 해당되는 글 23건

  1. 2018.07.18 Abstract Factory Pattern
  2. 2018.07.17 factory pattern
  3. 2018.07.17 Type of Design Pattern
pattern2018. 7. 18. 11:52

1.2 Abstract Factory Pattern


-factory of factories.

-Creational pattern 중 객체를 생성하는 가장 좋은 방법 중 하나이다.

-Abstract Factory pattern은 명시적 객체 지정 없이 관련된 factory를 주는 역할을 하는 interface이다.

-Factory와 마찬가지로 어떤 factory객체를 인자로 넘겨줄지에 대한 고민을 덜어내어 common한 구조로 가는데 도움이 된다.

-이렇게 각각 생성된 factory는 또다시 factory pattern을 따라 객체를 생성한다.




1
2
3
4
5
6
7
8
9
10
11
12
13
AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
Shape s = shapeFactory.getShape("rectangle");
s.draw();
s = shapeFactory.getShape("Circle");
s.draw();
s = shapeFactory.getShape("square");
AbstractFactory colorFactory = FactoryProducer.getFactory("Color");
Color c = colorFactory.getColor("red");
c.fill();
c = colorFactory.getColor("blue");
c.fill();
c = colorFactory.getColor("green");
c.fill();


FactoryProducer.java

1
2
3
4
5
6
7
8
9
10
11
12
     
public class FactoryProducer {
 
    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("shape"))
            return new ShapeFactory();
        else if (choice.equalsIgnoreCase("color"))
            return new ColorFactory();
        else
            return null;
    }
}


AbstractFactory.java

1
2
3
4
public abstract class AbstractFactory {
    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ShapeFactory extends AbstractFactory {
    @Override
    public Shape getShape(String shapeType){
        if (shapeType == null)
            return null;
        if (shapeType.equalsIgnoreCase("circle"))
            return new Circle();
        else if (shapeType.equalsIgnoreCase("rectangle"))
            return new Rectangle();
        else if (shapeType.equalsIgnoreCase("square"))
            return new Square();
        return null;
    }
 
    @Override
    public Color getColor(String color) {
        return null;
    }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ColorFactory extends AbstractFactory {
    @Override
    public Color getColor(String color) {
        if (color.equalsIgnoreCase(("red")))
            return new Red();
        else if (color.equalsIgnoreCase(("blue")))
            return new Blue();
        else if (color.equalsIgnoreCase(("green")))
            return new Green();
        return null;
    }
 
    @Override
    public Shape getShape(String shape) {
        return null;
    }
}



출처 :

https://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm

'pattern' 카테고리의 다른 글

Prototype pattern  (0) 2018.07.18
Builder pattern  (0) 2018.07.18
Singleton pattern  (0) 2018.07.18
factory pattern  (0) 2018.07.17
Type of Design Pattern  (0) 2018.07.17
Posted by easy16
pattern2018. 7. 17. 17:31

Topic:


-객체 생성 로직을 client로 부터 감춘채, common interface를 통하여 새로생성된 객체를 참조한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface Shape {
    void draw();
}
 
public class ShapeFactory {
    public Shape getShape(String shapeType){
        if (shapeType == null)
            return null;
        if (shapeType.equalsIgnoreCase("circle"))
            return new Circle();
        else if (shapeType.equalsIgnoreCase("rectangle"))
            return new Rectangle();
        else if (shapeType.equalsIgnoreCase("square"))
            return new Square();
        return null;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Circle implements Shape {
 
    private final String TAG = Circle.class.getSimpleName();
    @Override
    public void draw() {
        Log.d(TAG, "Circle draw");
    }
}
 
public class Rectangle implements Shape {
    private final String TAG = Rectangle.class.getSimpleName();
    @Override
    public void draw() {
        Log.d(TAG, "Rectangle draw");
    }
}
 
public class Square implements Shape {
    private final String TAG = Square.class.getSimpleName();
    @Override
    public void draw() {
        Log.d(TAG, "Square draw");
    }
}


결과 :


-application(client)가 Factory객체를 통해 객체를 간접적으로 획득, interface를 활용한 method 호출

1
2
3
4
5
6
7
ShapeFactory shapeFactory= new ShapeFactory();
Shape a =shapeFactory.getShape("circle");
a.draw();
a=shapeFactory.getShape("rectangle");
a.draw();
a=shapeFactory.getShape("square");
a.draw();


07-17 17:18:40.666 22057 22057 D Circle  : Circle draw

07-17 17:18:40.667 22057 22057 D Rectangle: Rectangle draw

07-17 17:18:40.667 22057 22057 D Square  : Square draw



출처 : 

https://www.tutorialspoint.com//design_pattern/design_pattern_quick_guide.htm


'pattern' 카테고리의 다른 글

Prototype pattern  (0) 2018.07.18
Builder pattern  (0) 2018.07.18
Singleton pattern  (0) 2018.07.18
Abstract Factory Pattern  (0) 2018.07.18
Type of Design Pattern  (0) 2018.07.17
Posted by easy16
pattern2018. 7. 17. 16:58

Design pattern의 3가지 타입


1 Creational Patterns 

-new를 통해 직접 객체 생성 로직을 감추고, 객체를 생성하는 방법을 제공

-주어진 경우에 대해 어떤 객체가 생성되어야 하는지에 대한 더 많은 flexibility를 준다.


2 Structural Patterns 

-객체의 구성에 대한 패턴, 상속 개념이 interface를 통합하고, 새기능을 가지는 객체를 구성하는 것에 대한 방법과 컨셉을 정의한다.


3 Behavioral Patterns 

-객체들간의 통신에 특화 되어 있는 패턴



'pattern' 카테고리의 다른 글

Prototype pattern  (0) 2018.07.18
Builder pattern  (0) 2018.07.18
Singleton pattern  (0) 2018.07.18
Abstract Factory Pattern  (0) 2018.07.18
factory pattern  (0) 2018.07.17
Posted by easy16