`
kinglun
  • 浏览: 42058 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

method of factory

阅读更多
package methodOfFactory;

public class People
{
private String name;
private String sex;
public People(){}
public People(String name,String sex)
{
this.name = name;
this.sex = sex;
}
public void show(){}
public String toString()
{
return "Name: "+name+"  Sex: "+sex;
}
}


package methodOfFactory;

public class Boy extends People {
public Boy(){}
public Boy(String name)
{
super(name,"male");
}
public void show(){}
}


package methodOfFactory;

public class ZhangSan extends Boy {
public ZhangSan()
{
super("zhang san");
}
public void show()
{
System.out.println("zhang san is my dota friend!");
}
}

package methodOfFactory;

public class Girl extends People {
public Girl(){}
public Girl(String name)
{
super(name,"male");
}
public void show(){}

}


package methodOfFactory;

public class LiSi extends Girl {
public LiSi()
{
super("li si");
}
public void show()
{
System.out.println("li si is my girl friend!");
}
}



package methodOfFactory;

public class WangWu extends Girl
{
public WangWu()
{
super("wang wu");
}
public void show()
{
System.out.println("wang wu is my wife!");
}
}


package methodOfFactory;

public interface Factory {
public abstract People getPeople(String name);
}


package methodOfFactory;

public class BoyFactory implements Factory {

@Override
public People getPeople(String name) {
if(name.equalsIgnoreCase("zhangsan"))
{
return new ZhangSan();
}
return null;
}

}



package methodOfFactory;

public class GirlFactory implements Factory
{

@Override
public People getPeople(String name)
{
if(name.equalsIgnoreCase("lisi"))
{
return new LiSi();
}else if(name.equalsIgnoreCase("wangwu"))
{
return new WangWu();
}
return null;
}

}


package methodOfFactory;

public class Main
{

public static void main(String[] args)
{
Factory f = new BoyFactory();
Boy b = (Boy) f.getPeople("zhangsan");
System.out.println(b);
b.show();
Factory fa = new GirlFactory();
Girl g = (Girl) fa.getPeople("lisi");
System.out.println(g);
g.show();
Girl gg = (Girl) fa.getPeople("wangwu");
System.out.println(gg);
gg.show();
}

}
/*
*工厂方法模型就要解决的是有多个工厂,每一个工厂生产特定产品,都是在工厂里面产生对象
*以后凡是要增加其他类型的产品的时候就可以新建一个工厂继承父工厂,然后在新建产品类就
*行,这样很好的体现了Java的一个设计原则,开闭原则
* */
分享到:
评论

相关推荐

    Factory Method 工厂方法模式(创建型模式)

    Factory Method(工厂方法)模式是GoF(Gang of Four)设计模式之一,属于创建型模式。此模式的核心在于提供了一种创建对象的方式,而不必将具体的类名硬编码到程序中,从而增强了程序的灵活性和可维护性。 #### ...

    设计模式迷你手册.chm

    Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge Composite Decorator Facade Flyweight Proxy 行为型 Interpreter Template Method Chain of Responsibility ...

    Homework 2.1-1.doc

    在本作业中,我们面临的是一个使用工厂方法模式来设计汽车保险信息系统的任务。工厂方法模式是一种设计模式,它提供了一种将实例化过程推迟到子类中的方式,从而允许在运行时创建对象的特定类型,而不必在编译时就...

    Head First Of Design高清中文版 1/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    design patterns elements of reusable object-oriented software

    ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...

    设计模式C++代码示例-含VC工程

    Factory Method Creates an instance of several derived classes Prototype A fully initialized instance to be copied or cloned Singleton A class of which only a single instance can exist ...

    apache-tomcat-9.0.17.rar

    It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on ...

    Head First Of Design高清中文版 2/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    Head First Of Design中文高清版 3/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    常见设计模式的解析和实现(c++)

    客户端代码通过调用 `Creator` 类的 `AnOperation` 方法间接地创建了 `ConcreateProduct` 对象,而具体的创建逻辑则由 `ConcreateCreator` 类的 `FactoryMethod` 方法完成。这种方式使得系统更加灵活,可以轻松地...

    常见设计模式的解析和实现(C++)看看吧.pdf

    根据提供的文档信息,我们可以深入探讨一下“工厂方法模式”(Factory Method Pattern)这一经典的设计模式。 ### 工厂方法模式概述 #### 定义 工厂方法模式是一种创建型设计模式,它提供了一个创建对象的接口,但...

    测试spring中的org.springframework.beans.factory.InitializingBean

    在Spring框架中,`org.springframework.beans.factory.InitializingBean`接口是一个非常重要的概念,它用于标记那些需要在初始化完成后执行特定逻辑的bean。这个接口只包含一个方法:`afterPropertiesSet()`,当bean...

    day01_eesy_02factory_spring_

    <bean id="exampleBean" factory-bean="myFactory" factory-method="createInstance"/> ``` 在这个例子中,"myFactory"是工厂bean的ID,而`createInstance`是工厂方法的名称。Spring会调用这个方法来创建目标bean。...

    org.springframework.transaction-3.1.2.RELEASE.zip

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...

    Objective C 设计模式(英文版)

    Chapter 4: Factory Method Chapter 5: Abstract Factory Chapter 6: Builder Chapter 7: Singleton Chapter 8: Adapter Chapter 9: Bridge Chapter 9: Bridge Chapter 11: Mediator Chapter 12: Observer Chapter ...

    Programming.in.the.Large.with.Design.Patterns

    It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...

    Object-Oriented Analysis and Design 第六章

    It lets you decouple the creation of objects from their usage by providing a factory method interface for creating objects. For example, suppose you're developing a framework for document editors. ...

Global site tag (gtag.js) - Google Analytics