Factory Pattern

Factory Pattern

Abstract Factory-Provide an interface for creating families of related or dependent objects without specifying their concrete class.

Factory Method-Define an interface for creating an object but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to the subclass.

Problem:

Duck duck;

if (picni)
    duck 
= new  MallardDuck();
else   if (hunting)
    duck 
=   new  DecoyDuck();
else
    duck 
=   new  RubberDuck(); 

 

Solution:take the creation code and move it out into another object that is only going to be concerned with creating parts.

Example:

//code

import  java.lang.String;
abstract   class  CPU  {

    
int cache;
    
int Frequency;
    
int Voltage;
    String CPUName;
    
public void DisplayCPUCapability()
    
{
        System.out.println(
"CPU name is:"+CPUName);
        System.out.println(
"cache:"+cache+";Frequency:"+Frequency+";Voltage:"+Voltage);
        
    }

}

class  AMD  extends  CPU
{
    
public AMD(int cache,int Frequency,int Voltage)
    
{
        
this.cache=cache;
        
this.Frequency=Frequency;
        
this.Voltage = Voltage;
        
this.CPUName = "AMD";
    }

}

class  Intel  extends  CPU
{
    
public Intel(int cache,int Frequency,int Voltage)
    
{
        
this.cache=cache;
        
this.Frequency=Frequency;
        
this.Voltage = Voltage;
        
this.CPUName = "Intel";
    }

}

abstract   class  Memory
{
    
int Capacity;
    
int Frequency;
    
int type;
    String Name;
    
public void DisplayMemoryParam()
    
{
        System.out.println(
"Memory name is:"+Name);
        System.out.println(
"Capacity:"+Capacity+";Frequency:"+Frequency+";Type:"+type);
        
    }

}

class  Kingston  extends  Memory
{
    
public Kingston(int cache,int Frequency,int Voltage)
    
{
        
this.Capacity=cache;
        
this.Frequency=Frequency;
        
this.type = Voltage;
        
this.Name = "Kingston";
    }

}

class  ADATA  extends  Memory
{
    
public ADATA(int cache,int Frequency,int Voltage)
    
{
        
this.Capacity=cache;
        
this.Frequency=Frequency;
        
this.type = Voltage;
        
this.Name = "ADATA";
    }

}

abstract   class  Mainboard
{
    
int BusFrequency;
    String Name;
    
public void DisplayMainboard()
    
{
        System.out.println(
"Mainboard name is:"+Name);
        System.out.println(
"Bus frequency is:"+BusFrequency);
    
    }

}

class  JingYingMainboard  extends  Mainboard
{
    
public JingYingMainboard(int BusFrequency)
    
{
        
this.BusFrequency = BusFrequency;
        
this.Name = "JingYingMainboard";
    }

}

 

public   interface  TraditionHouse  {
    
public CPU CreateCPU();
    
public Memory CreateMemory();
    
public Mainboard CreateMainboard();

}

class  QuanZhouHouse  implements  TraditionHouse
{
    
public CPU CreateCPU()
    
{
        
return new Intel(13,14,15);
    }

    
public Memory CreateMemory()
    
{
        
return new Kingston(3,4,5);
    }

    
public Mainboard CreateMainboard()
    
{
        
return new JingYingMainboard(111);
    }

}

class  NanJingHouse  implements  TraditionHouse
{
    
public CPU CreateCPU()
    
{
        
return new AMD(15,14,15);
    }

    
public Memory CreateMemory()
    
{
        
return new ADATA(13,4,5);
    }

    
public Mainboard CreateMainboard()
    
{
        
return new JingYingMainboard(121);
    }

}

 

public   class  Computer  {
    CPU cpu;
    Mainboard mainboard;
    Memory memory;
    TraditionHouse tranHouse;
    
public void SetFactory(TraditionHouse tran)
    
{
        tranHouse
=tran;
    }

    
public void prepareComputer()
    
{
        cpu
=tranHouse.CreateCPU();
        mainboard
=tranHouse.CreateMainboard();
        memory
=tranHouse.CreateMemory();
        
        DisplayComputerParam();
    }

    
private void DisplayComputerParam()
    
{
        cpu.DisplayCPUCapability();
        mainboard.DisplayMainboard();
        memory.DisplayMemoryParam();
    }

}

 

public   class  Main  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        
//declare two computer factory
        TraditionHouse tran1=new QuanZhouHouse();
        TraditionHouse tran2
=new NanJingHouse();
        
//declare computers in setup
        Computer computer1=new Computer();
        Computer computer2
=new Computer();
        
//select Computer factory which user select 
        computer1.SetFactory(tran1);
        computer2.SetFactory(tran2);
        
        
//setup and display results
        computer1.prepareComputer();
        computer2.prepareComputer();
        
    }


}

//if u find sth interesting,pls contact me. qq:95491590

When u see code like this, u know that when it comes time for changes or extensions, u will have to reopen this code and examine what needs to be added( or deleted). Often this kind of code ends up in several parts of the application making maintenance and  update more difficult and error-prone. So, in other words, when u have code that makes use of lots of concrete classes, u are looking for trouble because that code may have to be changed as new concrete classes are added.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
Abstract Factory Pattern是一种设计模式,它是创建对象的工厂模式的变体,允许对象在运行时被替换。 Abstract Factory模式提供了一种方法,可以创建一组相关或相互依赖的对象,而不需要明确指定其具体类。 下面是一个C语言的代码示例,该代码实现了一个抽象工厂模式,该模式创建一组车辆: ```c #include <stdio.h> typedef struct IVehicle IVehicle; struct IVehicle { void (*Drive)(IVehicle *); }; typedef struct Car Car; struct Car { IVehicle base; int wheelCount; }; void Car_Drive(IVehicle *vehicle) { Car *car = (Car *)vehicle; printf("Driving a car with %d wheels\n", car->wheelCount); } typedef struct Bike Bike; struct Bike { IVehicle base; int pedalCount; }; void Bike_Drive(IVehicle *vehicle) { Bike *bike = (Bike *)vehicle; printf("Riding a bike with %d pedals\n", bike->pedalCount); } typedef struct IVehicleFactory IVehicleFactory; struct IVehicleFactory { IVehicle *(*CreateVehicle)(IVehicleFactory *); }; typedef struct CarFactory CarFactory; struct CarFactory { IVehicleFactory base; }; IVehicle *CarFactory_CreateVehicle(IVehicleFactory *factory) { Car *car = (Car *)malloc(sizeof(Car)); car->base.Drive = &Car_Drive; car->wheelCount = 4; return (IVehicle *)car; } typedef struct BikeFactory BikeFactory; struct BikeFactory { IVehicleFactory base; }; IVehicle *BikeFactory_CreateVehicle(IVehicleFactory *factory) { Bike *bike = (Bike *)malloc(sizeof(Bike)); bike->base.Drive = &Bike_Drive; bike->pedalCount = 2; return (IVehicle *)bike; } int main(int argc, char *argv[]) { CarFactory carFactory = { { &CarFactory_CreateVehicle } }; IVehicle *vehicle = carFactory.base.CreateVehicle((IVehicleFactory *)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值