列表操作中正确的 OOP 方法?
The properly OOP approach in operation of lists?
我有个OOP题,比如有列表(数组)
需要一种机制来添加(保存)和删除每个数组中的选定值。输出应针对其所选元素的每个数组。
数组的添加逻辑可能不同。
例如,对于 arr1
数组,在添加到所选列表之前,满足特定条件,而其他则不满足。
我的想法正确吗?
数组arr1, arr2, arr3
- 数据模型。他们里面有任何类型的数据。
执行器 class 必须负责对数组的操作,它还允许您将项目添加到选定(收藏夹)列表并从那里删除它。
问题 1 - 为每个数组存储选定元素列表的位置?
问题 2 - 如果 class ArrayHandler
的逻辑是正确的,当不同数组的 add 方法的逻辑不同时,我遇到了一个问题.
在这种情况下,我不知道如何检查,是不是那个数组,在添加到选定列表之前需要额外检查。
class ArrayHandler {
add (element) {
// How to distinguish which input array is here to do the check before adding?
if (element is from arr1) {
return;
}
if (element is from arr2) {
// add
}
}
}
我会尽量简单地回答你。如果有什么不清楚的地方,请告诉我。
问题 1 - 将每个数组的选定元素列表存储在哪里?
Answer: If the number of arrays (which is 3 as you mentioned) is fixed, just make three members of the class, each will represent the array, and if not, then make single member have type 2D array and each item of that array contains your data array.
问题 2 - 如果 class ArrayHandler 的逻辑是正确的,当 add 方法的逻辑对于不同的数组不同时,我遇到了一个问题。
如何区分这里输入的是哪个数组做加前校验?
In which array to add, this info must be in the item being added if there's only a single function for add. Otherwise, all the arrays will have separate add function, that implements their logic.
如果每个特定数组的加法逻辑完全不同,那么最好分开,让每个数组有不同的加法函数。
您只能在 class 外部公开一个数组函数,并且可以在公开的单个 add 函数内部调用。同样的条件会调用它们对应的add函数
Example
type Genre = 'horror' | 'romantic' | 'actions';
interface TMovie {
name: string;
genre: Genre;
}
class Movies {
constructor() {
this.romantic = [];
this.horror = [];
this.action = [];
}
romantic: TMovie[];
horror: TMovie[];
action: TMovie[];
add(item:TMovie) {
switch (item.genre) {
case 'actions': this.addToAction(item); break;
case 'horror': this.addToHorror(item); break;
case 'romantic': this.addToRomantic(item); break;
}
}
private addToHorror(movie: TMovie) {
// Some custom logic
this.horror.push(movie);
}
private addToAction(movie: TMovie) {
// Some custom logic
this.action.push(movie);
}
private addToRomantic(movie: TMovie) {
// Some custom logic
this.romantic.push(movie);
}
}
let a = new Movies();
a.add({ genre: 'actions', name: "Parker" });
我有个OOP题,比如有列表(数组)
需要一种机制来添加(保存)和删除每个数组中的选定值。输出应针对其所选元素的每个数组。
数组的添加逻辑可能不同。
例如,对于 arr1
数组,在添加到所选列表之前,满足特定条件,而其他则不满足。
我的想法正确吗?
数组arr1, arr2, arr3
- 数据模型。他们里面有任何类型的数据。
执行器 class 必须负责对数组的操作,它还允许您将项目添加到选定(收藏夹)列表并从那里删除它。
问题 1 - 为每个数组存储选定元素列表的位置?
问题 2 - 如果 class ArrayHandler
的逻辑是正确的,当不同数组的 add 方法的逻辑不同时,我遇到了一个问题.
在这种情况下,我不知道如何检查,是不是那个数组,在添加到选定列表之前需要额外检查。
class ArrayHandler {
add (element) {
// How to distinguish which input array is here to do the check before adding?
if (element is from arr1) {
return;
}
if (element is from arr2) {
// add
}
}
}
我会尽量简单地回答你。如果有什么不清楚的地方,请告诉我。
问题 1 - 将每个数组的选定元素列表存储在哪里?
Answer: If the number of arrays (which is 3 as you mentioned) is fixed, just make three members of the class, each will represent the array, and if not, then make single member have type 2D array and each item of that array contains your data array.
问题 2 - 如果 class ArrayHandler 的逻辑是正确的,当 add 方法的逻辑对于不同的数组不同时,我遇到了一个问题。
如何区分这里输入的是哪个数组做加前校验?
In which array to add, this info must be in the item being added if there's only a single function for add. Otherwise, all the arrays will have separate add function, that implements their logic.
如果每个特定数组的加法逻辑完全不同,那么最好分开,让每个数组有不同的加法函数。
您只能在 class 外部公开一个数组函数,并且可以在公开的单个 add 函数内部调用。同样的条件会调用它们对应的add函数
Example
type Genre = 'horror' | 'romantic' | 'actions';
interface TMovie {
name: string;
genre: Genre;
}
class Movies {
constructor() {
this.romantic = [];
this.horror = [];
this.action = [];
}
romantic: TMovie[];
horror: TMovie[];
action: TMovie[];
add(item:TMovie) {
switch (item.genre) {
case 'actions': this.addToAction(item); break;
case 'horror': this.addToHorror(item); break;
case 'romantic': this.addToRomantic(item); break;
}
}
private addToHorror(movie: TMovie) {
// Some custom logic
this.horror.push(movie);
}
private addToAction(movie: TMovie) {
// Some custom logic
this.action.push(movie);
}
private addToRomantic(movie: TMovie) {
// Some custom logic
this.romantic.push(movie);
}
}
let a = new Movies();
a.add({ genre: 'actions', name: "Parker" });