public class Factory {

public static void main(String[] args) {//Client
IFruit fruit=Factorytemp.getFruit("香蕉");//这里为什么要使用接口的类型,因为接口的引用可以接收实现此接口的类的实例
if(fruit!= null){
System.out.println(fruit.get());
}else{
System.out.println("sorry!");
}

}

}

interface IFruit{//可以定义接口或者使用抽象类
public String get();
}

class Factorytemp{//工厂类,根据一定的条件创建某一具体类的实例
public static IFruit getFruit(String name){//公有的静态方法,通过类名.方法名调用
if(name.equals("苹果")){
return new Apple();
}
else if(name.equals("橘子")){
return new Orange();
}
else{
return null;
}
}
}
class Apple implements IFruit{//具体的产品(被实例的类),通常具有共同的接口或者父类
public String get(){
return "采摘苹果";
}
}
class Orange implements IFruit{//具体的产品(被实例的类),通常具有共同的接口或者父类
public String get(){
return "采摘橘子";
}
}

The Factory pattern的更多相关文章

  1. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  2. Net设计模式实例之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...

  3. [Design Pattern] Factory Pattern 简单案例

    Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...

  4. 设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明

    Abstract Factory模式(abstract factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/ ...

  5. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  6. 设计模式 - 出厂模式(factory pattern) 详细说明

    出厂模式(factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27081511 工厂方法模式 ...

  7. 设计模式复习小结一(Strategy Pattern/Observer Pattern/Decorator Patter/Factory Pattern)

    目录: 前言 1. Stratrgy Pattern 2. Observer Pattern 3. Decorator Pattern 4. Factory Pattern 4.1 FactoryPa ...

  8. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  9. 【设计模式】简单工厂模式 Simple Factory Pattern

    简单工厂模式Simple Factory Pattern[Simple Factory Pattern]是设计模式里最简单的一个模式,又叫静态工厂模式[Static Factory Pattern], ...

  10. 设计模式(四) Factory Pattern工厂模式

    核心: 实例化对象,实现创建者和调用者的分离 简单工厂模式 工厂方法模式 抽象工厂模式 面对对象设计的基本原则: ocp(open closed principle) 开闭原则:一个软件的实体应当对拓 ...

随机推荐

  1. GridView控件RowDataBound事件中获取列字段途径

    今天不知道怎么回事怎么也找不到gridview列中的控件,关键是其为编辑时隐藏域中的控件,取值就很成问题了,网上搜了很到,找到这个比较经典的东东了,可能大家都知道,但很少对比整理到一起,有多种方法可以 ...

  2. nodejs require

    The rules of where require finds the files can be a little complex, but a simple rule of thumb is th ...

  3. [Docker] docker 基础学习笔记5(共6篇)

    docker 配置文件的位置: centos : /etc/sysconfig/docker ubuntu: /etc/default/docker   现在比如我自己电脑上已经装好了docker,但 ...

  4. jQuery(window) 和 jQuery(document)的区别

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用

  5. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用Spring Framework开发自己的应用程序

    1.直接基于spring framework开发自己的应用程序: 1.1参考资料: Spring官网spring-framework.4.3.5.RELAESE的Reference Documenta ...

  6. 郑捷《机器学习算法原理与编程实践》学习笔记(第四章 推荐系统原理)(二)kmeans

    (上接第二章) 4.3.1 KMeans 算法流程 算法的过程如下: (1)从N个数据文档随机选取K个文档作为质心 (2)对剩余的每个文档测量其到每个质心的距离,并把它归到最近的质心的类 (3)重新计 ...

  7. LeetCode OJ 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. IOS TableView 去除点击后产生的灰色背景

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...

  9. 集合框架之——迭代器并发修改异常ConcurrentModificationException

    问题: 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 使用普通迭代器出现的异常: ...

  10. VoLTE 注册流程

    1.开关按钮位置:   设置--> 更多--> 移动网络--> 增强型4G LTE模式 2.该设置开关使用了SwitchPreference控件,addEnhanced4GLteSw ...