您的位置:首页 > 其它

设计模式系列:(3)Abstract Factory Patterns

2016-07-09 00:22 435 查看
1、GoF Definition
Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

2、Concept
In this pattern, we provide an encapsulation mechanism to a group of individual factories. These factories have a theme in common. In this process, an interface is used to create related objects. Here we do not call their implementer or concrete classes directly. We sometimes refer to this pattern as a factory of factories or a Super factory.
With this pattern, we can interchange the specific implementations without changing the user’s code. But to achieve this, we need to compensate for the complexity of the system. As a result, debugging may be difficult in many scenarios.

3、Real-Life Example
Suppose we are decorating our room. Now suppose we need two different types of almirah (or, say, table)— one must be made of wood and one of steel. For the wooden almirah, we need to visit a carpenter shop and for the other type, we can go to a readymade steel almirah shop. Both of these are almirah (or table) factories. Based on our demand, we decide what kind of factory we need. This scenario can be considered an example of this pattern.

4、代码

IActionMovie.java

public interface IActionMovie
{
String MovieName();
}


IComedyMovie.java

public interface IComedyMovie
{
String MovieName();
}


IMovieFactory.java

public interface IMovieFactory
{
IActionMovie getActionMovie();
IComedyMovie getComedyMovie();
}


BollywoodActionMovie.java
public class BollywoodActionMovie implements IActionMovie
{

@Override
public String MovieName()
{
return "This is Bollywood Action Movie";
}

}


BollywoodComedyMovie.java
public class BollywoodComedyMovie implements IComedyMovie
{

@Override
public String MovieName()
{
return "This is Bollywood Comedy Movie";
}

}


BollywoodMovieFactory.java
public class BollywoodMovieFactory implements IMovieFactory
{

@Override
public IActionMovie getActionMovie()
{
return new BollywoodActionMovie();
}

@Override
public IComedyMovie getComedyMovie()
{
return new BollywoodComedyMovie();
}

}


TollywoodActionMovie.java
public class TollywoodActionMovie implements IActionMovie
{

@Override
public String MovieName()
{
return "This is Tollywood Action Movie";
}

}


TollywoodComedyMovie.java

public class TollywoodComedyMovie implements IComedyMovie
{

@Override
public String MovieName()
{
return "This is Tollywood Comedy Movie";
}

}


TollywoodMovieFactory.java
public class TollywoodMovieFactory implements IMovieFactory
{

@Override
public IActionMovie getActionMovie()
{
return new TollywoodActionMovie();
}

@Override
public IComedyMovie getComedyMovie()
{
return new TollywoodComedyMovie();
}

}


5、Note
1. We use this pattern when our system does not care about how its products will be created or composed.
2. We use this pattern when we need to deal with multiple factories.
3. This pattern separates concrete classes and makes interchanging the products
easier. It can also enhance the reliabilities among products. But, at the same
time, we must acknowledge the fact that creating a new product is difficult with
this pattern (because we need to extend the interface and, as a result, changes
will be required in all of the subclasses that already implemented the interface).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  design pattern