scala设计模式
I’m going to deliver a series of Scala Design Patterns in my coming posts. I will pick-up each Java Design Pattern Tutorial explained in JournalDEV and convert it into Scala.
我将在以后的文章中提供一系列Scala设计模式。 我将获取JournalDEV中解释的每个Java设计模式教程,并将其转换为Scala。
We will also discuss about which Language is best: Java Vs Scala to implement them. In this post, we are going to discuss about “Factory Design Pattern” implementation in Scala Language.
我们还将讨论哪种语言最好:Java和Scala来实现它们。 在本文中,我们将讨论Scala语言中“工厂设计模式”的实现。
Before reading this post, please go through Java version of “Factory Design Pattern“.
在阅读本文之前,请阅读Java版本的“ Factory Design Pattern ” 。
POST简要目录 (POST Brief Table Of Content)
- Factory Design Pattern 工厂设计模式
- Advantages of Factory Design Pattern 工厂设计模式的优势
- Factory Design Pattern in Scala Scala中的工厂设计模式
- Improved Factory Design Pattern in Scala Scala中改进的工厂设计模式
- Factory Design Pattern: Java Vs Scala 工厂设计模式:Java与Scala
工厂设计模式 (Factory Design Pattern)
Factory Design Pattern is one of the popular Creational Design Patterns.
工厂设计模式是流行的创作设计模式之一。
The main aim of Factory Design Pattern is that separate objects or instances creation logic from client. We implement Object creation logic in a Factory class without exposing that logic to the client.
Factory Design Pattern的主要目的是将对象或实例创建逻辑与客户端分开。 我们在Factory类中实现对象创建逻辑,而不会将该逻辑暴露给客户端。
Factory Design Pattern is also know as Factory Method Design Pattern. It is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class.
工厂设计模式也称为工厂方法设计模式。 当我们有一个具有多个子类的超类并且基于输入时,需要返回一个子类时使用它。
工厂设计模式的优势 (Advantages of Factory Design Pattern)
Factory Design Pattern have the following benefits:
工厂设计模式具有以下优点:
- Loose Coupling between Object Creation logic and Client. 对象创建逻辑和客户端之间的松散耦合。
- Clear separation of Responsibilities. 明确区分职责。
- Easy to change object creation logic without effecting Client program. 易于更改对象创建逻辑,而不会影响客户端程序。
Scala中的工厂设计模式 (Factory Design Pattern in Scala)
In this section, we will take existing “Factory Design Pattern” implemented in Java Language and convert into Scala Language. We will refine this example in next section.
在本节中,我们将采用Java语言实现的现有“工厂设计模式”并将其转换为Scala语言。 我们将在下一部分中完善此示例。
In Scala, We can implement Factory class using Singleton Object and Pattern Matching. If you are not sure what is Singleton Object, how to implement it in Scala and Pattern Matching, please go through my Scala Tutorial.
在Scala中,我们可以使用Singleton Object和Pattern Matching实现Factory类。 如果不确定什么是Singleton Object,如何在Scala和模式匹配中实现它,请阅读我的Scala教程。
First develop our class hierarchy and Factory class as show below:
首先开发我们的类层次结构和Factory类,如下所示:
ComputerFactory.scala
ComputerFactory.scala
package com.journaldev.designpattern.factory
trait Computer{
def getRAM():String
def getHDD():String
def getCPU():String
override def toString = "RAM= " + getRAM +", HDD=" + getHDD + ", CPU=" + getCPU
}
private class PC(val ram:String, val hdd:String, val cpu:String) extends Computer{
def getRAM():String = ram
def getHDD():String = hdd
def getCPU():String = cpu
}
private class Server(val ram:String, val hdd:String, val cpu:String) extends Computer{
def getRAM():String = ram
def getHDD():String = hdd
def getCPU():String = cpu
}
object ComputerFactory{
def apply(compType:String, ram:String, hdd:String, cpu:String) = compType.toUpperCase match {
case "PC" => new PC(ram,hdd,cpu)
case "SERVER" => new Server(ram,hdd,cpu)
}
}
Scala does not have interface concept. However, it has more powerful concept than interface that is: Trait. We have developed a based trait with all our required functions or methods.
Scala没有界面概念。 但是,它具有比接口更强大的概念:Trait。 我们已经开发了具有所有必需功能或方法的基础特征。
Here ‘ComputerFactory’ object is a Singleton Object. It is used to provide factory method to create our objects. Next develop a client to test this Scala program.
这里的“ ComputerFactory”对象是一个Singleton对象。 它用于提供工厂方法来创建我们的对象。 接下来,开发一个客户来测试此Scala程序。
FactoryMethodDesignPatternClient.scala
FactoryMethodDesignPatternClient.scala
object FactoryMethodDesignPatternClient extends App{
val pc = ComputerFactory("pc","2 GB","500 GB","2.4 GHz");
val server = ComputerFactory("server","16 GB","1 TB","2.9 GHz");
println("Factory PC Config::"+pc);
println("Factory Server Config::"+server);
}
output
输出
Factory PC Config::RAM= 2 GB, HDD=500 GB, CPU=2.4 GHz
Factory Server Config::RAM= 16 GB, HDD=1 TB, CPU=2.9 GHz
Scala中改进的工厂设计模式 (Improved Factory Design Pattern in Scala)
In this section, we are going to improved our previous example by using Scala case classes. If you are new to Case Classes, please read my “Scala Case Classes In Depth” post in my Scala Tutorial.
在本节中,我们将通过使用Scala案例类来改进前面的示例。 如果您不熟悉案例类,请阅读我的《 Scala教程》中的“ Scala深度案例类”一文。
package com.journaldev.designpattern.factory
trait Computer{
def ram:String
def hdd:String
def cpu:String
override def toString = "RAM= " + ram +", HDD=" + hdd + ", CPU=" + cpu
}
private case class PC(ram:String, hdd:String, cpu:String) extends Computer
private case class Server(ram:String, hdd:String, cpu:String) extends Computer
object ComputerFactory{
def apply(compType:String, ram:String, hdd:String, cpu:String) = compType.toUpperCase match {
case "PC" => PC(ram,hdd,cpu)
case "SERVER" => Server(ram,hdd,cpu)
}
}
Use same client to test this program, we will get same output.
使用相同的客户端来测试该程序,我们将获得相同的输出。
工厂设计模式:Java与Scala (Factory Design Pattern: Java Vs Scala)
If we observe both Java and Scala Factory Design Pattern examples, we can observe that Scala is the best language to implement this design pattern. The reason is:
如果我们同时观察Java和Scala Factory Design Pattern示例,则可以观察到Scala是实现此设计模式的最佳语言。 原因是:
- Very concise or less code 非常简洁或更少的代码
- No boilerplate code 没有样板代码
- Very expression code 非常表达代码
That’s it all about “Factory Design Pattern In Scala”. We will discuss some more Design Patterns In Scala Language in my coming posts.
这就是“ Scala中的工厂设计模式”的全部内容。 我们将在我的后续文章中讨论更多的Scala语言设计模式。
Please drop me a comment if you like my post or have any issues/suggestions.
如果您喜欢我的帖子或有任何问题/建议,请给我评论。
翻译自: https://www.journaldev.com/10350/factory-design-pattern-in-scala
scala设计模式